# Live Chat SDK Swift

### Configuration

Untuk dapat menggunakan SDK connector ini, perlu untuk mencapai persyaratan berikut:&#x20;

* XCode 10.0+ (rekomendasi menggunakan XCode 12.1+)
* Swift 4.0, 4.1, 4.2, 5.0

### Installation

{% hint style="info" %}
Rekomendasi untuk menggunakan framework dari .xcframework.&#x20;
{% endhint %}

Berikut merupakan cara untuk melakukan instalasi menggunakan framework tersebut:&#x20;

1. Dapatkan file `imi_dolphin_livechat_ios_xcframework`
2. Buka **Xcode** project Anda dan masuk ke **Project Settings > General**
3. Pada bagian bawah **Framework**, **Libraries,** dan **Embedded Content**, klik tombol **'+'** dan pilih **Add Other...** > **Add Files..**
4. Tempatkan dan tambahkan `imi_dolphin_livechat_ios_xcframework` pada project Anda.&#x20;
5. Pastikan bahwa **Embed & Sign** sudah dipilih untuk menggabungkan framework dengan aplikasi Anda dengan benar
6. Ulangi langkah yang sama untuk menambahkan dependensi seperti `StompClientLib` dan `CryptoSwift.xcframework` jika dibutuhkan

### Import Library&#x20;

Tambahkan script import berikut di bagian atas file Swift Anda:

```swift
import imi_dolphin_livechat_ios
```

### Initialize User Profile&#x20;

Buat profile user menggunakan kelas dari `DolphinProfile` :

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

**Penjelasan Komponen:**

<table><thead><tr><th width="316">Nama</th><th>Deskripsi</th></tr></thead><tbody><tr><td>DolphinProfile</td><td>Merupakan identitas dari user yang melakukan chat </td></tr><tr><td>name</td><td>Merupakan nama dari user tersebut </td></tr><tr><td>email</td><td>Merupakan email dari user tersebut </td></tr><tr><td>phoneNumber</td><td>Merupakan nomor teleponn dari user tersebut</td></tr><tr><td>uid</td><td>Berguna untuk menentukan sesi user ini akan membuat tiket baru atau tidak</td></tr></tbody></table>

### Initialize Connection Variables&#x20;

Tentukan notification listener dan variabel yang dibutuhkan:

```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;

Pastikan gambar memenuhi persyaratan berikut:

* Ukuran gambar dibawah 2MB
* Memiliki format PNG atau JPEG

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

#### Send Video

Pastikan video memenuhi persyaratan berikut:

* Ukuran video dibawah 2MB
* Memiliki format MP4

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

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

* Ukuran Dokumen/Audio dibawah 2MB
* Memiliki format PDF

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.3dolphins.ai/5.3.0-id/integration/chanel-connector/live-chat/live-chat-sdk-swift.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
