# Walletconnect - iOS

We recommend downloading the demo client app and follow the usage

## 1. Initial the configuration

* ### Install the packages required

Use Cocoapods

`pod 'WalletConnectSwift'`

Or add the <https://github.com/WalletConnect/WalletConnectSwift.git> by SPM

* ### Add scheme

Open the info.plist, add the scheme "io.iotex.iopay" to LSApplicationQueriesSchemes![](https://iotex.larksuite.com/space/api/box/stream/download/asynccode/?code=MjQ0OTcxMWQ5OTY5YTJhZWRlNjVjNWE4ZjNmNDZjMjRfaG8xSVFJU2x6UzIyOXlCbW5RREV2RUx3UEdjb1JiTWRfVG9rZW46Ym94dXM1QldFblczWThwdjRFWjBnaWd5eGNjXzE2NTM1Mjc2ODQ6MTY1MzUzMTI4NF9WNA)

* ### Add WalletConnect.swift

Drag the WalletConnect.swift located in the Demo to your project, the copy option must be selected

## 2. Build the connection

initialize the `WalletConnect` instance first and conform all the delegate, then build the connection with ioPay app

```
var walletConnect: WalletConnect!
// initialize the WalletConnect instance
walletConnect = WalletConnect(delegate: self)
walletConnect.reconnectIfNeeded()
        
//open the ioPay and ask for the connect
let connectionUrl = walletConnect.connect()
let deepLinkUrl = "io.iotex.iopay://wc?uri=\(connectionUrl)"
if let url = URL(string: deepLinkUrl), UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
 }
```

## 3. Send the common action

Now the ioPay supports these actions:`personal_sign`, `eth_signTypedData` ,`eth_signTransaction`,`eth_sendTransaction` and custom contract action.You can read the relative codes in "ActionsViewController.swift"

```
    //eth_signTypedData
    @IBAction func eth_signTypedData(_ sender: Any) {
        try? client.eth_signTypedData(url: session.url,
                                      account: session.walletInfo!.accounts[0],
                                      message: Stub.typedData) {
            [weak self] response in
            self?.handleReponse(response, expecting: "Signature") }
    }
    //eth_signTransaction
    @IBAction func eth_signTransaction(_ sender: Any) {
        let transaction = Stub.transaction(from: self.walletAccount, nonce: "0x0")
        try? self.client.eth_signTransaction(url: session.url, transaction: transaction) { [weak self] response in
            self?.handleReponse(response, expecting: "Signature")
        }
    }
```

## 4. Send the custom action

```
//send the custom contract action
try? client.send(.customContract(url: session.url,
                                     wcMethod: wcMethod,
                                     requirement:[abi, method, contractAddress],
                                     params: params)) { [weak self] response in
        self?.handleReponse(response)
 }
    
    
extension Request {

    ///
    /// - Parameters:
    ///   - url: the walletconnect url
    ///   - wcMethod: the method communicating between client and ioPay, you can customize the name
    ///   - requirement:[abi, method, contractAddress]. this method is a contract method needs to be called
    ///   - params: the params passed into the contract method
    /// - Returns: 
    static func customContract(url: WCURL, wcMethod: String, requirement: [String], params: [String]) -> Request {
        return try! Request(url: url, method: wcMethod, params: [requirement, params])
    }
}
    
```

## 5. Tip

Because of the restriction of iOS background, some behaviors just like presenting viewcontroller won't work perhaps, so you need to jump the iOPay after sending actions.

```
        let deepLinkUrl = "io.iotex.iopay://wc"
        if let url = URL(string: deepLinkUrl), UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
```
