SendKin 0.1.1

SendKin 0.1.1

natanrolnikCorey Werner维护。



SendKin 0.1.1

SendKin

用法

要了解如何实现 SendKin,请在 Example 目录中运行 pod install 并检查示例应用程序,其中包含两个目标:Sender 和 Receiver。

初始设置

在允许您的用户将其钱包中的 Kin 发送到其他应用程序或在其他应用程序中接收 Kin 之前,您需要完成以下两个步骤。

  1. 创建您的 SendKin 实例并保留对该实例的引用。
class AppDelegate: UIResponder, UIApplicationDelegate {
    let sendKin = SendKin()
    //...
}
  1. 如果您的应用程序尚未设置 URL Scheme,请在主应用程序目标的“_info”标签中添加一个。联系 Kin DevX 团队以将您的应用程序信息添加到该模块显示的应用程序列表中。

接收 Kin

现在,要接收来自其他应用程序的 Kin,实现 ReceiveKinFlowDelegate。它有两个简单的方法:(1)提供用户的钱包地址(如果存在);(2)通知您的服务器可能在接下来的几分钟内发生一笔交易。

注意:我们不建议在您的应用的AppDelegate中实现协议,如图样代码所示。在示例应用中这样做是为了帮助理解该模块的关注点分离。

extension AppDelegate: ReceiveKinFlowDelegate {
    func handlePossibleIncomingTransaction(senderAppName: String,
                                           senderAppId: String,
                                           memo: String) {
        //here, your app should let your server know that a possible
        //incoming transaction might happen, so transaction history
        //gets updated accordingly
    }

    func provideUserAddress(addressHandler: @escaping (String?) -> Void) {
        let userAddress = //access the user's public address, if he's created a wallet...
        addressHandler(userAddress)
    }
}

您的AppDelegate应该能够转发它可能接收到的application(_ app:, open url:, options:)调用。您可以在示例应用中进行这种方式。

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if sendKin.canHandleURL(url) {
        let sourceAppBundleId = (options[.sourceApplication] as? String) ?? ""
        sendKin.handleURL(url, from: sourceAppBundleId, receiveDelegate: self)
    }

    return true
}

发送Kin

接下来,因为SendKinKinSDK交易和余额操作无关,它需要一个SendKinFlowDelegate来代表它执行。通过以下方式选择您的类来实现协议:(1)实际发送Kin,(2)返回余额和(3)在KinSDK中配置的应用ID。委托接收执行发送Kin所需的所有信息,例如接收者地址、金额和memo,而它在成功或失败回调时调用完成块的职责。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    sendKin.delegate = self

    return true
}

最后,请求transferButton将其添加到您的UI中。该按钮会自行处理启动流程,因此您不需要为其添加任何操作。如果您希望从自己的UI中初始化流程,直接调用sendKin.start()。在没有设置委托之前使用这些选项中的任何一个都不会起作用。

func setupSendKin() {
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
      return
    }

    let button = appDelegate.sendKin.transferButton
    button.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(button)
    view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
    view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
}

安装

send-kin-module-ios可以通过CocoaPods获得。目前不支持SPM和Carthage。

要安装它,只需将以下行添加到Podfile

pod 'SendKin'

许可协议

SendKin可在MIT许可下获得。有关更多信息,请参阅LICENSE文件。