Skip to content

Commit f98d688

Browse files
authored
Merge pull request #153735 from knvsl/knvsl/swift-quickstart
Update swift quickstart
2 parents e0c889a + 4114aef commit f98d688

File tree

1 file changed

+100
-49
lines changed
  • articles/communication-services/quickstarts/chat/includes

1 file changed

+100
-49
lines changed

articles/communication-services/quickstarts/chat/includes/chat-swift.md

Lines changed: 100 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ From the command line, go inside the root directory of the `ChatQuickstart` iOS
4141
Open the Podfile, and add the following dependencies to the `ChatQuickstart` target:
4242

4343
```
44-
pod 'AzureCommunication', '~> 1.0.0-beta.9'
45-
pod 'AzureCommunicationChat', '~> 1.0.0-beta.9'
44+
pod 'AzureCommunication', '~> 1.0.0-beta.11'
45+
pod 'AzureCommunicationChat', '~> 1.0.0-beta.11'
4646
```
4747

4848
Install the dependencies with the following command: `pod install`. Note that this also creates an Xcode workspace.
@@ -75,16 +75,20 @@ override func viewDidLoad() {
7575
// <CREATE A CHAT CLIENT>
7676
7777
// <CREATE A CHAT THREAD>
78-
79-
// <CREATE A CHAT THREAD CLIENT>
80-
78+
79+
// <LIST ALL CHAT THREADS>
80+
81+
// <GET A CHAT THREAD CLIENT>
82+
8183
// <SEND A MESSAGE>
82-
84+
85+
// <SEND A READ RECEIPT >
86+
87+
// <RECEIVE MESSAGES>
88+
8389
// <ADD A USER>
8490
8591
// <LIST USERS>
86-
87-
// <REMOVE A USER>
8892
} catch {
8993
print("Quickstart failed: \(error.localizedDescription)")
9094
}
@@ -101,17 +105,17 @@ Replace the comment `<CREATE A CHAT CLIENT>` with the following code:
101105

102106
```
103107
let endpoint = "<ACS_RESOURCE_ENDPOINT>"
104-
let credential =
105-
try CommunicationTokenCredential(
106-
token: "<ACCESS_TOKEN>"
107-
)
108-
let options = AzureCommunicationChatClientOptions()
109-
110-
let chatClient = try ChatClient(
111-
endpoint: endpoint,
112-
credential: credential,
113-
withOptions: options
114-
)
108+
let credential =
109+
try CommunicationTokenCredential(
110+
token: "<ACCESS_TOKEN>"
111+
)
112+
let options = AzureCommunicationChatClientOptions()
113+
114+
let chatClient = try ChatClient(
115+
endpoint: endpoint,
116+
credential: credential,
117+
withOptions: options
118+
)
115119
```
116120

117121
Replace `<ACS_RESOURCE_ENDPOINT>` with the endpoint of your Azure Communication Services resource. Replace `<ACCESS_TOKEN>` with a valid Communication Services access token.
@@ -136,10 +140,10 @@ Now you use your `ChatClient` to create a new thread with an initial user.
136140
Replace the comment `<CREATE A CHAT THREAD>` with the following code:
137141

138142
```
139-
let request = CreateThreadRequest(
143+
let request = CreateChatThreadRequest(
140144
topic: "Quickstart",
141145
participants: [
142-
Participant(
146+
ChatParticipant(
143147
id: CommunicationUserIdentifier("<USER_ID>"),
144148
displayName: "Jack"
145149
)
@@ -150,7 +154,7 @@ var threadId: String?
150154
chatClient.create(thread: request) { result, _ in
151155
switch result {
152156
case let .success(result):
153-
threadId = result.thread?.id
157+
threadId = result.chatThread?.id
154158
155159
case .failure:
156160
fatalError("Failed to create thread.")
@@ -164,11 +168,31 @@ Replace `<USER_ID>` with a valid Communication Services user ID.
164168

165169
You're using a semaphore here to wait for the completion handler before continuing. In later steps, you'll use the `threadId` from the response returned to the completion handler.
166170

171+
## List all chat threads
172+
173+
After creating a chat thread we can list all chat threads by calling the `listChatThreads` method on `ChatClient`. Replace the comment `<LIST ALL CHAT THREADS>` with the following code:
174+
175+
```
176+
chatClient.listThreads { result, _ in
177+
switch result {
178+
case let .success(chatThreadItems):
179+
var iterator = chatThreadItems.syncIterator
180+
while let chatThreadItem = iterator.next() {
181+
print("Thread id: \(chatThreadItem.id)")
182+
}
183+
case .failure:
184+
print("Failed to list threads")
185+
}
186+
semaphore.signal()
187+
}
188+
semaphore.wait()
189+
```
190+
167191
## Get a chat thread client
168192

169193
Now that you've created a chat thread, you can obtain a `ChatThreadClient` to perform operations within the thread.
170194

171-
Replace the comment `<CREATE A CHAT THREAD CLIENT>` with the following code:
195+
Replace the comment `<GET A CHAT THREAD CLIENT>` with the following code:
172196

173197
```
174198
let chatThreadClient = try chatClient.createClient(forThread: threadId!)
@@ -184,10 +208,13 @@ let message = SendChatMessageRequest(
184208
senderDisplayName: "Jack"
185209
)
186210
211+
var messageId: String?
212+
187213
chatThreadClient.send(message: message) { result, _ in
188214
switch result {
189215
case let .success(result):
190216
print("Message sent, message id: \(result.id)")
217+
messageId = result.id
191218
case .failure:
192219
print("Failed to send message")
193220
}
@@ -198,22 +225,67 @@ semaphore.wait()
198225

199226
First, you construct the `SendChatMessageRequest`, which contains the content and sender's display name. This request can also contain the share history time, if you want to include it. The response returned to the completion handler contains the ID of the message that was sent.
200227

228+
229+
## Send a read receipt
230+
231+
You can send a read receipt for a particular message by calling `ChatThreadClients` `sendReadReceipt` method. Replace the comment `<SEND A READ RECEIPT>` with the following code:
232+
233+
```
234+
if let id = messageId {
235+
chatThreadClient.sendReadReceipt(forMessage: id) { result, _ in
236+
switch result {
237+
case .success:
238+
print("Read receipt sent")
239+
case .failure:
240+
print("Failed to send read receipt")
241+
}
242+
semaphore.signal()
243+
}
244+
semaphore.wait()
245+
} else {
246+
print("Cannot send read receipt without a message id")
247+
}
248+
```
249+
250+
## Receive chat messages from a chat thread
251+
252+
You can receive messages from a chat thread by calling the `listMessages()` method from `ChatThreadClient`. List messages includes system messages as well as user sent messages. For more information on the types of messages you can receive see [Message Types](https://docs.microsoft.com/azure/communication-services/concepts/chat/concepts#message-types)
253+
254+
Replace the comment `<RECEIVE MESSAGES>` with the following code:
255+
256+
```
257+
chatThreadClient.listMessages { result, _ in
258+
switch result {
259+
case let .success(messages):
260+
var iterator = messages.syncIterator
261+
while let message = iterator.next() {
262+
print("Received message of type \(message.type)")
263+
}
264+
265+
case .failure:
266+
print("Failed to receive messages")
267+
}
268+
semaphore.signal()
269+
}
270+
semaphore.wait()
271+
```
272+
201273
## Add a user as a participant to the chat thread
202274

203275
Replace the comment `<ADD A USER>` with the following code:
204276

205277
```
206-
let user = Participant(
278+
let user = ChatParticipant(
207279
id: CommunicationUserIdentifier("<USER_ID>"),
208280
displayName: "Jane"
209281
)
210282
211283
chatThreadClient.add(participants: [user]) { result, _ in
212284
switch result {
213285
case let .success(result):
214-
(result.errors != nil) ? print("Added participant") : print("Error adding participant")
286+
(result.invalidParticipants != nil) ? print("Added participant") : print("Error adding participant")
215287
case .failure:
216-
print("Failed to list participants")
288+
print("Failed to add the participant")
217289
}
218290
semaphore.signal()
219291
}
@@ -235,7 +307,7 @@ chatThreadClient.listParticipants { result, _ in
235307
var iterator = participants.syncIterator
236308
while let participant = iterator.next() {
237309
let user = participant.id as! CommunicationUserIdentifier
238-
print(user.identifier)
310+
print("User with id: \(user.identifier)")
239311
}
240312
case .failure:
241313
print("Failed to list participants")
@@ -245,28 +317,7 @@ chatThreadClient.listParticipants { result, _ in
245317
semaphore.wait()
246318
```
247319

248-
249-
## Remove user from a chat thread
250-
251-
Replace the `<REMOVE A USER>` comment with the following code:
252-
253-
```
254-
chatThreadClient
255-
.remove(
256-
participant: CommunicationUserIdentifier("<USER_ID>")
257-
) { result, _ in
258-
switch result {
259-
case .success:
260-
print("Removed user from the thread.")
261-
case .failure:
262-
print("Failed to remove user from the thread.")
263-
}
264-
}
265-
```
266-
267-
Replace `<USER ID>` with the Communication Services user ID of the participant being removed.
268-
269320
## Run the code
270321

271-
In Xcode, select **Run** to build and run the project. In the console, you can view the output from the code and the logger output from the chat client.
322+
In Xcode hit the Run button to build and run the project. In the console you can view the output from the code and the logger output from the ChatClient.
272323

0 commit comments

Comments
 (0)