> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kleep.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 移动端 SDK - iOS

**Github 仓库：** [https://github.com/KlipFit/kleep-spm](https://github.com/KlipFit/kleep-spm)

## 安装

***

此流程仅需执行一次，即首次在项目中安装 SDK 时。后续更新请参阅 **Update** 部分的说明。

1. 添加以下软件包：[https://github.com/KlipFit/kleep-spm](https://github.com/KlipFit/kleep-spm)
2. 将依赖规则更改为：up to next major version

## 权限

***

在项目的 info.plist 中添加以下权限：**NSCameraUsageDescription**

## 使用方法

***

### 方法 1：`requestSize`

此方法用于实现 CTA 的逻辑。

| parameter        | priority | description    |
| ---------------- | -------- | -------------- |
| **productID**    | *必填*     | 您的产品 ID        |
| **retailerName** | 必填       | 零售商名称          |
| **customerID**   | *可选*     | CRM 标识符，用于分析   |
| **trackingID**   | *可选*     | 外部追踪方案使用的用户 ID |

**逻辑图示**

此图示说明如何更新打开 Kleep 的 CTA。

[https://www.figma.com/board/BlurZ01lR3JBQZeTUU98TE/Mobile---CTA-Logic?node-id=0-1\&t=ccXWciNziIdfhgew-1](https://www.figma.com/board/BlurZ01lR3JBQZeTUU98TE/Mobile---CTA-Logic?node-id=0-1\&t=ccXWciNziIdfhgew-1)

**实现示例**

```swift theme={null}
import Klipfit

struct ContentView: View {
    @State var isOpenView = false
        
    var body: some View {
        VStack {
            Spacer()
            Button("Open Kleep") {
                isOpenView = true
            }
            Spacer(minLength: 40)
            
            Button("obtenir la taille") {
                Klipfit.shared.requestSize(productID: String, retailerName: String, trackingID: String, customerID: String) { kleepResult in
                    switch kleepResult {
                    case .notSupported:
	                    // Hide CTA
                        print("NotSupported")
                    case .noRecommendationYet:
	                    // Display CTA with neutral label
                        print("NoRecommendationYet")
                    case .recommendationFound(let result):
	                    // Display CTA with size reco
                        print("RecommendationFound: ", result)
                    }
                }
            }
	            Spacer()
        }
        .sheet(isPresented: $isOpenView) {
            KlipFitFindSizeView { result, variantID in 
								// addToCart(variantID)
	          }
        }
    }
}
```

### 方法 2：`loadView`

此方法在点击 CTA 时调用，用于加载各个界面。**集成 SDK 的开发者应将其嵌入底部弹窗（bottom sheet）中。**

| parameter        | priority | description  |
| ---------------- | -------- | ------------ |
| **productID**    | *必填*     | 您的产品 ID      |
| **retailerName** | *必填*     | 零售商名称        |
| **customerID**   | *可选*     | CRM 标识符，用于分析 |
| **trackingID**   | *可选*     | 外部追踪服务使用的 ID |

**实现示例**

```swift theme={null}
struct KlipFitFindSizeView: UIViewControllerRepresentable {
		
		public var onResult: KleepOnResultCompletion

    func makeUIViewController(context: Context) -> KlipftNavigationController {
        guard let viewController =
        KlipftNavigationController.loadView(
                productID: String,
                retailerName: String,
                language: KleepLanguageCode, // .fr .en .es .it .pt .nl 
                trackingID: "",
                customerID: "",
                stocks: [String : Bool]?,
                onResult: onResult
        )
        else {
            fatalError("ViewController not implemented in storyboard")
        }
        viewController.navigationBar.isHidden = true
        return viewController
    }
    func updateUIViewController(_ uiViewController: KlipftNavigationController, context: Context) {
    }
    typealias UIViewControllerType = KlipftNavigationController
}
```

### 方法 3：`track`

此方法用于追踪自定义事件。

| parameter | priority | description |
| --------- | -------- | ----------- |
| eventName | *必填*     | 要追踪的事件名称    |
| params    | *可选*     | 与事件关联的信息    |

```swift theme={null}
import Klipfit

Klipfit.shared.track(eventName: String, params: [String: Any]? = nil)
```

我们需要追踪 3 个事件：

| eventName               | 触发时机         |
| ----------------------- | ------------ |
| `product_viewed`        | 查看商品详情页时     |
| `product_added_to_cart` | 商品加入购物车时     |
| `checkout_completed`    | 支付完成后收到订单确认时 |

**`product_viewed`**

[参数（CSV）](/images/ios/Untitled%202c68bfd3e84981cabf11f24acd542d96.csv)

**示例**

```jsx theme={null}
{
		productId: str
}
```

**`product_added_to_cart`**

[参数（CSV）](/images/ios/Untitled%202c68bfd3e84981858563ca1319b138e8.csv)

**示例**

```jsx theme={null}
{
    productId: str,
    variantId: str,
    cart: [
	    {
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
				sku: "XYZ",
		    size: "S",
		    quantity: 2,
		    price: {
						amount: "50",
				    currencyCode: "EUR"		    
		    }
	    },
	    {
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
		    sku: "XYZ",
		    size: "S",
		    quantity: 1,
		    price: {
				    amount: "40",
				    currencyCode: "USD"
				}
	    }
	    ...
    ],
}
```

**`checkout_completed`**

[参数（CSV）](/images/ios/Untitled%202c68bfd3e84981e7ad20d59d320148e0.csv)

**示例**

```jsx theme={null}
{
    orderId: "000001",
    cart: [
	    {
		    lineItemId: "000001#1",
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
				sku: "XYZ",
		    size: "S",
		    quantity: 2,
		    price: {
						amount: "50",
				    currencyCode: "EUR"		    
		    }
	    },
	    {
			  lineItemId: "000000#2",
		    productId: "123ABC456",
		    variantId: "123ABC456-00R",
		    sku: "XYZ",
		    size: "S",
		    quantity: 1,
		    price: {
				    amount: "40",
				    currencyCode: "USD"
				}
	    }
	    ...
    ],
}
```
