# iOS SDK 連携

このユーザーガイドでは、ストーリーボードUIにAlli iOS SDKを統合する方法について説明します。 Alli SDKにはiOS 10以降が必要です。<br>

###

## Step1. SDKをダウンロードして解凍します <a href="#step-1-download-and-import-the-sdk" id="step-1-download-and-import-the-sdk"></a>

<figure><img src="https://i0.wp.com/guide.allganize.ai/wp-content/uploads/2022/01/iOS-SDK-1.png?fit=1000%2C367&#x26;ssl=1" alt=""><figcaption></figcaption></figure>

1. Alliダッシュボードにログインして、設定メニューに進みます。 下にスクロールしてiOS SDKのダウンロードを見つけ、SDKをダウンロードします。
2. ファイルを解凍すると、フォルダー内に次のファイルが表示されます。

<figure><img src="https://i0.wp.com/guide.allganize.ai/wp-content/uploads/2022/01/iOS-SDK-Integration-2.png?fit=253%2C118&#x26;ssl=1" alt=""><figcaption></figcaption></figure>

###

## Step 2. SDKをプロジェクトに追加します <a href="#step-2-add-sdk-toproject" id="step-2-add-sdk-toproject"></a>

1. XcodeのProject Navigatorでプロジェクトアイコンを右クリックします。
2. \[アプリにファイルを追加]ボタンをクリックします。 ダイアログが表示されます。
3. AlliFrameworkフォルダーを選択します。
4. ”Destination:\[必要に応じてアイテムをコピー]”の![](https://3246530324-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVKCSdGPopMt2sSDjiJua%2Fuploads%2FF1usJtTZo5ZnKcE1oCYY%2Fimage.png?alt=media\&token=5755aef2-d899-418b-8ca2-9cbe4c041fe8)をオンにします。
5. ダイアログの内容が下記キャプチャーのようになっていることを確認し、\[追加]ボタンをクリックします

<figure><img src="https://i0.wp.com/guide.allganize.ai/wp-content/uploads/2022/01/iOS-SDK-Integration-3.png?fit=600%2C383&#x26;ssl=1" alt=""><figcaption></figcaption></figure>

###

## Step 3. 埋め込みバイナリ、フレームワーク、ライブラリを追加します <a href="#step-3-add-liblary" id="step-3-add-liblary"></a>

1. Project Navigatorでプロジェクトアイコンをクリックします。
2. 全般 > \[フレームワーク、ライブラリ、および埋め込みコンテンツ]に移動し、Alli Framework.frameworkを\[署名なしで埋め込み]に設定します。
3. ビルドフェーズ ＞\[ライブラリとバイナリをリンク]および\[埋め込みフレームワーク]を確認します。 AlliFramwork.frameworkが追加されていることを確認してください。

<figure><img src="https://i0.wp.com/guide.allganize.ai/wp-content/uploads/2022/01/iOS-SDK-Integration-4.png?fit=761%2C407&#x26;ssl=1" alt=""><figcaption></figcaption></figure>

###

## Step 4. Info.plistで権限を指定します <a href="#step-4-infoplist" id="step-4-infoplist"></a>

AlliFramework.frameworkフォルダーのinfo.plistファイルを開き、情報プロパティリストの横にある「+」記号をクリックします。 ドロップダウンリストを\[プライバシー]オプションまでスクロールし、カメラにアクセスするための\[プライバシーカメラ使用法の説明]またはフォトライブラリにアクセスするためのプライバシーフォトライブラリ使用法の説明を選択します。

これにより、ユーザーは領収書ファイルまたは製品画像をチャットにアップロードできます。

&#x20;

選択した後、右側の文字列値を入力して、警告ポップアップが許可を求めたときにユーザーに表示するテキストを含めます。

<figure><img src="https://i0.wp.com/guide.allganize.ai/wp-content/uploads/2022/01/iOS-SDK-Integration-5.png?fit=765%2C379&#x26;ssl=1" alt=""><figcaption></figcaption></figure>

###

## Step 5. Alli SDKを初期化する <a href="#step-5-initialization-sdk" id="step-5-initialization-sdk"></a>

1. アプリケーションのデリゲートクラスファイルViewController.swiftの先頭に「import AlliFramework」を追加します。 これにより、Alliフレームワークにプロジェクトからアクセスできるようになります。
2. ViewControllerに以下を追加して、SDKキーでAlliライブラリを初期化します。

※以下の画像には”YOUR\_API\_Key”と記載がありますが、こちらにSDKキーを記載ください。

{% code lineNumbers="true" %}

```javascript
var alli: Alli?
override func viewDidLoad() {
  // showHeader - optional, default: true, If you want to remove the header, turn off this option.
  // showFooter - optional, default: true, If you want to remove the footer, turn off this option.
  // showBackButton - optional, default: true, If you want to remove the back button on the header, turn off this option.
  alli = Alli("YOUR_API_Key", eventHandler: self, showHeader: true, showFooter: true, showBackButton: false)
}
```

{% endcode %}

AndroidSDK用のJavascripSDKで使用可能なすべてのパラメーターも使用できます。

Dictionary \<String、Any>を使用して、JavascriptSDKの初期化に使用されるパラメーターをinitWithParams引数に送信します。

{% code lineNumbers="true" %}

```javascript
alli = Alli.initWithParams("YOUR_API_Key", eventHandler: self,
                           params: [
                               "header": true,
                               "footer": true,
                               "backButton": true,
                               "styleOptions": ["conversationContainer": [
                                   "right": 50,
                                   "left": 50,
                                   "bottom": 50]]])
```

{% endcode %}

SDKキーは、左側のナビゲーションから\[設定]＞\[全般]をクリックし、プロジェクト設定ページSDKキーを取得してください。

<figure><img src="https://i0.wp.com/guide.allganize.ai/wp-content/uploads/2022/01/iOS-SDK-6.png?fit=1000%2C392&#x26;ssl=1" alt=""><figcaption></figcaption></figure>

ユーザーとAlliが会話することができるダイアログを作成するには、下記を呼び出します。

###

## Step 6. ダイアログを作成する <a href="#step-6-create-dialog" id="step-6-create-dialog"></a>

{% code lineNumbers="true" %}

```javascript
alli!.event(view: parentView)
```

{% endcode %}

特定のユーザIDとPlacementを指定したい場合は以下のように呼び出します。

{% code lineNumbers="true" %}

```javascript
alli!.event(userId:"YOUR_userId", placement:"YOUR_placement", view:parentView)
```

{% endcode %}

サービスにログインして、ユーザが特定されている場合、上記のようにAlliに情報を渡すことができます。あるいはログインしていないユーザーのために、一時的なIDを生成することもできます。

Placement情報は、ユーザーにスキルを表示したいアプリ内の場所を決定するために使用されます。Placementを作成した後、Alliダッシュボードで必要なスキルにPlacementを指定することができます。この機能の利点は、プレースメントに表示するスキルをいつでも決定できることです。

Alliダイアログは、親ビューコントローラで実装する必要があります。このとき、親ビューコントローラを委任属性に割り当てる必要があります。この呼び出しに渡されたViewControllerが最上位のビューであり、他のビューに隠れないようにしてください。そして親ビューは、最小の高さと幅が必要です。\
initWithParamsと同じように、eventWithParamsを用いることで、すべての使用可能なJavascriptパラメーターが使用できます。<br>

{% code lineNumbers="true" %}

```javascript
alli!.eventWithParams(view: innerView,
                      params: [
                          "user": ["id": "YOUR_userId"],
                          "placement": "YOUR_placement",
                          "variables": ["test": "test value",
                                        "AGE": 31,
                                        "BIRTHDAY": "2021-06-30",
                                        "boolean": true]])
```

{% endcode %}

※上記のuserIdの形式にご注意ください。

###

## Step 7. Alliイベントを処理するデリゲートメソッド <a href="#step-7-deligate-method" id="step-7-deligate-method"></a>

次のデリゲートメソッドは、チャットの会話ステータスに関するフィードバックを提供します。<br>

{% code lineNumbers="true" %}

```javascript
import WebKit

public protocol AlliEventHandler {
    // Called when initialized successfully.
    // You may receive NOT_INITIALIZE_YET error if
    // called before this event.
    func onInitialized(_ view:WKWebView!)

    // Called when chat started successfully.
    func onConversationStarted(_ view: WKWebView!, userId: String, placement: String, context: Any?)

    // Called when conversation did not start
    // even when Alli.event was called.
    func onConversationNotStarted(_ view: WKWebView!, userId: String, placement: String, context: Any?)

    // Called when user has closed the chat
    // window or Alli.close() is called.
    func onConversationStopped(_ view: WKWebView!, userId: String, placement: String, context: Any?)

    func onError(_ view: WKWebView!, errorCode: AlliErrorCode, userId:String?, placement:String?, context:Any?)
  }
```

{% endcode %}

## Step 8. 他のアプリとの接続 <a href="#step-8-connect-other-apps" id="step-8-connect-other-apps"></a>

Alliチャットでhttpまたはhttpsにリンクすると、顧客のデフォルトのブラウザーが起動します。 他のアプリケーションへのリンクを追加する場合は、AlliEventHandlerからhandleUrlLoadingメソッドを実装します。

{% code lineNumbers="true" %}

```javascript
func handleUrlLoading(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    let strUrl = navigationAction.request.url!.absoluteString
    if(<condition>) {
        UIApplication.shared.open(URL(string: strUrl)!, options: [:], completionHandler: nil)
        decisionHandler(.allow)
    }
    else {
        decisionHandler(.cancel)
    }
}
```

{% endcode %}

（例）Apple Mapを起動する場合

{% code lineNumbers="true" %}

```javascript
let strUrl = "http://maps.apple.com/?q=seoul"
```

{% endcode %}
