# iOS SDK

## Prerequisites

* iOS 12.0 or higher
* Swift 5.0 or higher
* Xcode 13.4 or higher

## Environment Setup

Klip iOS SDK doesn't require a separate registration process. It works in any environment where HTTP communication is available. Still, users must have KakaoTalk installed to make the requests because the user's consent is received using the Klip application or Klip located in the More\[…] tab in the mobile app KakaoTalk.

### 1. Download Klip SDK

Download Klip iOS SDK in [Download](/a2a-sdk/a2a-sdk-download.md)

### 2. Add Klip SDK Framework

#### Import Klip SDK Framework and add to your project

1. Execute your project with **Xcode**
2. Go to **Xcode** > **Project's TARGETS** > **General Tab** > **Frameworks, Libraries, and Embedded Content Tab** and click on the **+** button
3. Select the **sdk** directory of Klip SDK project downloaded in **Source Directory** and click **Next**
4. Check if the SDK is imported to Frameworks in the Project Navigator and build.

```diff
! Only `KlipLib.xcframework` must be included to register on Apple App Store. Please ensure that `KlipLib-simulator.xcframework` is used for development purposes only.
```

Once the Klip SDK source is copied to your project, you can modify the library code yourself.

### 3. Set Info.plist

#### Register Allowlist for Apps

To execute applications like KakaoTalk using iOS SDK with iOS 9.0 or higher, you have to register a custom scheme in the Info.plist file.

Add an array type key LSApplicationQueriesSchemes in \[Info] > \[Custom iOS Target Properties], and add 'klip' and 'itms-apps' as 'Item' of that key.

```xml
 	<key>LSApplicationQueriesSchemes</key>
 	<array>
        <string>klip</string>
 		<string>kakaotalk</string>
 		<string>itms-apps</string>
 	</array>
```

### 4. Set Source Import

#### v2.1.0 or above

```swift
import KlipLib
```

#### v2.1.0 below

```swift
import KlipSDK
```

## API

### Overview

App2App API requests are made in the order: `prepare`, `request`, and `getResult`.

* `prepare` is the step in which requests (from of a total of five) are defined
* `request` is the step in which the function is called and the signing takes place on Klip
* `getResult` is the step in which the result is returned from the function call

In addition, `getCardList` is a function provided for the convenience of BApp developers that returns a list of NFTs of a Klip user.\
If you need help with this document or Klip in general, please visit our [Developer Forum](https://forum.klaytn.com/c/klip-api/28).<br>

### KlipSDK.shared

Creates an instance to use Klip SDK.

**Return Value**

| Type   | Description   |
| ------ | ------------- |
| `Klip` | Klip instance |

**Example**

```swift
let klip = KlipSDK.shared
```

### KlipSDK.shared.prepare

`KlipSDK.shared.prepare(request: KlipRequest, bappInfo: BAppInfo, completion: @escaping(KlipCallback<KlipTxResponse>) -> Void)`\
\
Prepares to process an App2App API request and issues a request key.

**Request Objects**

* Connecting Klip(=authentication) `AuthRequest`
* Sending KLAY `KlayTxRequest`
* Sending Token `TokenTxRequest`
* Sending Card `CardTxRequest`
* Executing Contract `ContractTxRequest`

**Parameters**

| Name     | Type                           | Description                                                                                                                               |
| -------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| request  | `KlipRequest`                  | Requested information depending on request type                                                                                           |
| bappInfo | `BAppInfo`                     | Information of the requested BApp                                                                                                         |
| callback | `KlipCallback<KlipTxResponse>` | The callback function to obtain the response. If it's successful, it returns `KlipTxResponse`, and if not it returns `KlipErrorResponse`. |

Requesting consent to Klip users will return the data entered in `request` and `bappInfo`. The request key returned in the response serves as a key when requesting transactions to users, and is used with **KlipSDK.shared.getResult** and **KlipSDK.shared.request**.

**Example 1. Obtaining User Information**

```swift
// Transaction request for obtaining user information
let req: AuthRequest = AuthRequest();

// BApp Information
let bappInfo: BAppInfo = BAppInfo(name : "BApp Name");

// Response Callback
KlipSDK.shared.prepare(request: req, bappInfo: bappInfo) { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

**Example 2. Sending KLAY**

```swift
// Transaction request for sending KLAY
let req: KlayTxRequest = KlayTxRequest(to: "0x..receiver address..", amount: "10")
  
KlipSDK.shared.prepare(request: req, bappInfo: bappInfo) { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

**Example 3. Sending Token**

```swift
// Transaction request for sending tokens
let req: TokenTxRequest = TokenTxRequest(to: "0x..receiver address..", amount: "10", contract: "0x..token contract address..")
  
KlipSDK.shared.prepare(request: req, bappInfo: bappInfo) { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

**Example 4. Sending Card**

```swift
// Transaction request for for sending Card
let req: CardTxRequest = CardTxRequest(to: "0x..receiver address..", contract: "0x..card contract address..", cardId: "9")
  
KlipSDK.shared.prepare(request: req, bappInfo: bappInfo) { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

**Example 5. Execute Contract**

```swift
// Transaction request for sending contract
let req: ContractTxRequest = ContractTxRequest(to: "0x..contract address..", value: "10", abi: "{...}", params: "[{...}]")
  
KlipSDK.shared.prepare(request: req, bappInfo: bappInfo) { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

### KlipSDK.shared.request

`KlipSDK.shared.request(requestKey: String), isKlipAppCall: Bool = false) -> Void`\
Requests authentification or signature using deep link. Under the below circumstances, it would redirect automatically to the download page of Klip or KakaoTalk on the Apple AppStore.

* If the device doesn't have Klip or KakaoTalk installed
* The version of the installed KakaoTalk doesn't support Klip
* To implement the request step using QR code, please refer to [QR Code Tutorial](/rest-api/rest-api-a2a.md#request-qr-code).

**Parameters**

| Name          | Type   | Description                                           |
| ------------- | ------ | ----------------------------------------------------- |
| requestKey    | String | Request Number (Obtained from Klip Server)            |
| isKlipAppCall | Bool   | If the Klip app was called first, then it should true |

**Example**

```swift
KlipSDK.shared.request(requestKey: "request key...", isKlipAppCall: true)
```

### KlipSDK.shared.getResult

`KlipSDK.shared.getResult(requestKey: String, completion: @escaping(KlipCallback<KlipTxResponse>) -> Void)`\
\
Checks the result of an App2App API request

**Parameters**

| Name       | Type                           | Description                                                                                                                               |
| ---------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| requestKey | String                         | Request Number (Obtained from Klip Server)                                                                                                |
| callback   | `KlipCallback<KlipTxResponse>` | The callback function to obtain the response. If it's successful, it returns `KlipTxResponse`, and if not it returns `KlipErrorResponse`. |

**Example**

```swift
KlipSDK.shared.getResult(requestKey: "request key...") { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

### KlipSDK.shared.getCardList

`KlipSDK.shared.getCardList(cardAddress: String, userAddress: String, cursor: String?, completion: @escaping(KlipCallback<CardListResponse>) -> Void)`\
\
Returns a list of certain Cards of a user

**Parameters**

| Name        | Type                             | Description                                                                                                                                 |
| ----------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| cardAddress | String                           | The address of the Card to return                                                                                                           |
| userAddress | String                           | The address of the user to return                                                                                                           |
| cursor      | String                           | (optional) The pointer after which the next request will retrieve the next 100 items if the number of Cards exceeds 100.                    |
| callback    | `KlipCallback<CardListResponse>` | The callback function to obtain the response. If it's successful, it returns `CardListResponse`, and if not it returns `KlipErrorResponse`. |

**Example**

```swift
KlipSDK.shared.getCardList(cardAddress: "0x..card address..", userAddress: "0x..user address..", cursor: nil) { result in
    switch result {
    case .success(let response):
    case .failure(let error):
    }
}
```

## Error Code

| Http Status | Error Code | Description                                              |
| ----------- | ---------- | -------------------------------------------------------- |
| -           | -          | Same as [Klip REST API Error Code](/basics.md)           |
| 500         | 10         | Error in Klip SDK (ex. HTTP c connection failure)        |
| 500         | 21         | Error in Klip SDK (Klip REST API unsupported error code) |
| 500         | 22         | Error in Klip SDK (Klip protocol error)                  |


---

# 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://en.docs.klipwallet.com/a2a-sdk/a2a-sdk-ios.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.
