Skip to content

Commit b3e855e

Browse files
Add get-started-chat
1 parent fae98d9 commit b3e855e

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
author: leahxia
3+
ms.author: leahxia
4+
ms.date: 01/04/2023
5+
ms.topic: include
6+
ms.service: azure-communication-services
7+
---
8+
9+
Get the sample iOS application for this [quickstart](https://github.com/Azure-Samples/communication-services-ios-quickstarts/tree/main/ui-library-quick-start/Chat) in the open source Azure Communication Services [UI Library for iOS](https://github.com/Azure/communication-ui-library-ios).
10+
11+
## Prerequisites
12+
13+
- An Azure account and an active Azure subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
14+
- A Mac running [Xcode](https://go.microsoft.com/fwLink/p/?LinkID=266532) 13 or later and a valid developer certificate installed in your keychain. [CocoaPods](https://cocoapods.org/) must also be installed to fetch dependencies.
15+
- A deployed [Azure Communication Services resource](../../../create-communication-resource.md), note the endpoint URL.
16+
- An Azure Communication Services [access token](../../../identity/quick-create-identity.md) and user identifier.
17+
- An Azure Communication Services [chat thread](../../../chat/get-started.md) and now add the user you created in the last step to this chat thread.
18+
19+
## Set up the project
20+
21+
Complete the following sections to set up the quickstart project.
22+
23+
### Create a new Xcode project
24+
25+
In Xcode, create a new project:
26+
27+
1. In the **File** menu, select **New** > **Project**.
28+
29+
1. In **Choose a template for your new project**, select the **iOS** platform and select the **App** application template. The quickstart uses the UIKit storyboards.
30+
31+
:::image type="content" source="../../media/xcode-new-project-template-select.png" alt-text="Screenshot that shows the Xcode new project dialog, with iOS and the App template selected.":::
32+
33+
1. In **Choose options for your new project**, for the product name, enter **Chat**. For the interface, select **Storyboard**. The quickstart doesn't create tests, so you can clear the **Include Tests** checkbox.
34+
35+
:::image type="content" source="../../media/xcode-new-project-details.png" alt-text="Screenshot that shows setting new project options in Xcode.":::
36+
37+
### Install the package and dependencies
38+
39+
1. (Optional) For MacBook with M1, install and enable [Rosetta](https://support.apple.com/en-us/HT211861) in Xcode.
40+
41+
1. In your project root directory, run `pod init` to create a Podfile. If you encounter an error, update [CocoaPods](https://guides.cocoapods.org/using/getting-started.html) to the current version.
42+
43+
1. Add the following code to your Podfile. Replace `Chat` with your project name.
44+
45+
```ruby
46+
platform :ios, '14.0'
47+
48+
target 'Chat' do
49+
use_frameworks!
50+
pod 'AzureCommunicationUIChat', '0.1.0-beta.1'
51+
end
52+
```
53+
54+
1. Run `pod install --repo-update`.
55+
56+
1. In Xcode, open the generated *.xcworkspace* file.
57+
58+
### Turn off Bitcode
59+
60+
In the Xcode project, under **Build Settings**, set the **Enable Bitcode** option to **No**. To find the setting, change the filter from **Basic** to **All** or use the search bar.
61+
62+
:::image type="content" source="../../media/xcode-bitcode-option.png" alt-text="Screenshot that shows the Build Settings option to turn off Bitcode.":::
63+
64+
## Initialize the composite
65+
66+
To initialize the composite:
67+
68+
1. Go to `ViewController`.
69+
70+
1. Add the following code to initialize your composite components for a chat. Replace `<USER_ID>` with user identifier. Replace `<USER_ACCESS_TOKEN>` with your access token. Replace `<ENDPOINT_URL>` with your endpoint URL. Replace `<DISPLAY_NAME>` with your name. (The string length limit for `<DISPLAY_NAME>` is 256 characters). Replace `<THREAD_ID>` with your chat thread ID.
71+
72+
```swift
73+
import UIKit
74+
import AzureCommunicationCommon
75+
import AzureCommunicationUIChat
76+
77+
class ViewController: UIViewController {
78+
79+
override func viewDidLoad() {
80+
super.viewDidLoad()
81+
82+
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
83+
button.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 20.0, bottom: 10.0, right: 20.0)
84+
button.layer.cornerRadius = 10
85+
button.backgroundColor = .systemBlue
86+
button.setTitle("Start Experience", for: .normal)
87+
button.addTarget(self, action: #selector(startChatComposite), for: .touchUpInside)
88+
89+
button.translatesAutoresizingMaskIntoConstraints = false
90+
self.view.addSubview(button)
91+
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
92+
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
93+
}
94+
95+
@objc private func startChatComposite() {
96+
let communicationIdentifier = CommunicationUserIdentifier("<USER_ID>")
97+
guard let communicationTokenCredential = try? CommunicationTokenCredential(
98+
token: "<USER_ACCESS_TOKEN>") else {
99+
return
100+
}
101+
102+
let chatAdapter = ChatAdapter(
103+
identifier: communicationIdentifier,
104+
credential: communicationTokenCredential,
105+
endpoint: "<ENDPOINT_URL>",
106+
displayName: "<DISPLAY_NAME>")
107+
108+
chatAdapter.connect(threadId: "<THREAD_ID>") { [weak self] _ in
109+
print("Chat connect completionHandler called")
110+
DispatchQueue.main.async {
111+
let chatCompositeViewController = ChatCompositeViewController(
112+
with: chatAdapter)
113+
chatCompositeViewController.title = "Chat"
114+
let closeItem = UIBarButtonItem(
115+
barButtonSystemItem: .close,
116+
target: nil,
117+
action: #selector(self?.onBackBtnPressed))
118+
chatCompositeViewController.navigationItem.leftBarButtonItem = closeItem
119+
120+
let navController = UINavigationController(rootViewController: chatCompositeViewController)
121+
navController.modalPresentationStyle = .fullScreen
122+
self?.present(navController, animated: true, completion: nil)
123+
}
124+
}
125+
}
126+
127+
@objc func onBackBtnPressed() {
128+
self.dismiss(animated: true, completion: nil)
129+
}
130+
}
131+
```
132+
133+
## Run the code
134+
135+
To build and run your app on the iOS simulator, select **Product** > **Run** or use the (&#8984;-R) keyboard shortcut. Then, try out the chat experience on the simulator:
136+
137+
1. Select **Start Experience**.
138+
2. The chat client will join the chat thread and you can start typing and sending messages.
139+
3. If the client isn't able to join the thread, and you see `chatJoin` failed errors, verify that your user's access token is valid and that the user has been added to the chat thread by REST API call, or by using the az command line interface.
140+
141+
:::image type="content" source="../../media/quick-start-chat-composite-running-ios.gif" alt-text="GIF animation that demonstrates the final look and feel of the quickstart iOS app.":::
120 KB
Loading

0 commit comments

Comments
 (0)