# Live Chat SDK Swift

### Configuration

To use this SDK connector, you must meet the following requirements:&#x20;

* XCode 10.0+ (recommended to use  XCode 12.1+)
* Swift 4.0, 4.1, 4.2, 5.0

### Installation

{% hint style="info" %}
Recommended to use framework from .xcframework.&#x20;
{% endhint %}

Steps to install using this framework:&#x20;

1. Obtain file `imi_dolphin_livechat_ios_xcframework`
2. Open your **Xcode** project and navigate to  **Project Settings > General**
3. Under **Framework**, **Libraries,** and **Embedded Content**, click button **'+'** and select **Add Other...** > **Add Files..**
4. Locate and add `imi_dolphin_livechat_ios_xcframework` to your project&#x20;
5. Ensure that **Embed & Sign i**s selected to properly bundle the framework with your app
6. Repeat the same steps to add any dependencies like `StompClientLib` dan `CryptoSwift.xcframework` if needed&#x20;

### Import Library&#x20;

Add the following import statement at the top of your Swift file:&#x20;

```swift
import imi_dolphin_livechat_ios
```

### Initialize User Profile&#x20;

Create a user profile using the class`DolphinProfile` :

```swift
let dolphinProfile = DolphinProfile(
name: nameTextField.text!,
email: emailTextField.text!,
phoneNumber: phoneTextField.text!,
customerId: "",
uid: ""
)
```

**Component Explanation:**

<table><thead><tr><th width="316">Name</th><th>Description</th></tr></thead><tbody><tr><td>DolphinProfile</td><td>Represents the identify of the user initiating the chat</td></tr><tr><td>name</td><td>Represents the name of the user </td></tr><tr><td>email</td><td>Represents the user's email </td></tr><tr><td>phoneNumber</td><td>Represents the user's phone number</td></tr><tr><td>uid</td><td>Used to determine whether this user session will create a new ticket or not </td></tr></tbody></table>

### Initialize Connection Variables&#x20;

Define the required notification listeners and connection variables:

```swift
var connector: Connector?
let notificationMessage = "com.connector.notificationMessage"
let notificationConnectionStatus = "com.connector.connectionStatus"
let notificationReadMessage = "com.connector.notificationReadMessage"
let notificationTypingCondition = "com.connector.notificationTypingCondition"
```

### Setup Connection in viewDidLoad

```swift
connector!.setupConnection(baseUrl: baseUrl, clientId: clientId, clientSecrect: clientSecrect)
connector?.enableGetQueue(isEnable: false)
connector!.constructConnector(profile: dolphinProfile!)

NotificationCenter.default.addObserver(self, selector: #selector(doOnReceiveMessage(_:)), name: Notification.Name(rawValue: notificationMessage), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(doUpdateConnectionStatus(_:)), name: Notification.Name(rawValue: notificationConnectionStatus), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(doUpdateStatusMessage(_:)), name: Notification.Name(rawValue: notificationReadMessage), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(doUpdateTypingCondition(_:)), name: Notification.Name(rawValue: notificationTypingCondition), object: nil)

```

### Implement Notification Handlers

```swift
@objc func doOnReceiveMessage(_ notification: NSNotification) {
let newMessage = notification.object as! DolphinMessage
// Handle received message
}
@objc func doUpdateConnectionStatus(_ notification: NSNotification) {
let status: Int = notification.object as! Int
// Handle connection status updates
}
@objc func doUpdateStatusMessage(_ notification: NSNotification){
let updatedMessage = notification.object as! DolphinMessage
// Handle message status updates
}
@objc func doUpdateTypingCondition(_ notification: NSNotification){
// Handle typing condition updates
}
```

### Sending Messages

#### Send Text Message

```swift
connector?.onSendMessage(messages: "TEXT_MESSAGE_STRING", dataUser: sample)
```

#### Send Image&#x20;

Ensure image file meets these conditions:

* Image size must be under 2MB
* Using format PNG or JPEG

```swift
connector!.sendAttachment(fileNsUrl: "IMAGE_FILEPATH", state: state!, dataUser: sample)
```

#### Send Video

Ensure video file meets these conditions:

* Video size must be under 2MB
* Using format MP4

```swift
connector!.sendAttachment(fileNsUrl: "VIDEO_FILEPATH", state: state!, dataUser: sample)
```

#### Send File (Document and Audio as File)&#x20;

Ensure documents file meets these conditions:

* Document/Audio size must be under 2MB
* Memiliki format PDF

```swift
connector?.sendAttachment(fileNsUrl: AUDIO/DOCUMENT_FILEPATH as NSURL, state: state!, dataUser: sampl
```

