Dapp Wallet SDK iOS
Dapp 是一个支付平台,专注于用户的安全。该 SDK 允许电子钱包读取 Dapp QR 码。
安装
我们建议使用 cocoapods 集成 Dapp Wallet SDK。
platform :ios, '10.0'
pod 'DappWalletMX'
配置
- 在 AppDelegate 中添加以下导入指令
import DappWalletMX
- 如果需要使用网络功能,请在 application(_:didFinishLaunchingWithOptions:) 方法中初始化 DappWallet 对象,并用您的生产密钥替换 your-dapp-api-key
DappWallet.shared.apiKey = "your-dapp-api-key"
获取 Dapp QR 码信息
如果您的钱包已有自带的 QR 码读取器,请校验并通过读取器接收获取的文字信息作为支付信息。
- 创建一个遵循 DappWalletDelegate 协议的类(在本示例中,我们将使用属于读者的 viewController),并实现其方法以获取与扫描的代码相关联的信息。
import DappWalletMX
class ViewController: UIViewController, DappPaymentDelegate {
//MARK: - DappWalletDelegate
public func dappWalletSuccess(code: DappCode) {
//realice las acciones apropiadas con la información obtenida
}
public func dappWalletFailure(error: DappError) {
//tu código para manejar el error
print(error)
switch error {
default:
break
}
}
- 一旦通过二维码读取器获取到文本,请对其进行验证并获取与代码相关联的信息。
let qrTextFromScanner: String = "https://dapp.mx/c/oW9BYXqJ"
if !DappWallet.shared.isValid(DappCode: qrTextFromScanner) {
//Indique al usuario que no es un código válido
}
DappWallet.shared.readDappCode(code: code, delegate: self) //self == ViewController
- Dapp Wallet SDK iOS 也支持 CoDi。你可以使用两个函数:
DappWallet.shared.isValidCodi(qrTextFromScanner) //true or false
DappWallet.shared.getQRType(qrTextFromScanner) //.codi, .dapp, .codiDapp, .unknown
获取 DAPP 二维码 ID
如果你只想获取 DAPP ID,而不获取其他信息,可以使用以下方法:
DappWallet.shared.getDappId(qrTextFromScanner) // String
使用 DAPP 二维码读取器
读取器的功能可以以两种方式实现:
- 作为视图控制器:更快、更简单。创建一个 DappScannerViewController 并显示它。这个视图控制器负责从 DAPP 二维码中获取信息以及与 UX 相关的所有方面。
- 作为视图:更灵活。创建一个只负责从 DAPP 二维码中获取信息的 DappScannerView。这允许你实现一个更符合你应用的 UX。
不管你选择哪种方式,都需要配置 info.plist 文件。添加 NSCameraUsageDescription 属性,并设置一个描述为什么你的应用需要使用摄像头的字符串值(例如:“扫描二维码”)。当应用第一次请求使用摄像头时,此文本将显示给用户。
将读取器作为视图控制器集成
- 创建一个类(可以是你将显示 DappScannerViewController 的视图控制器),该类遵循 DappScannerViewControllerDelegate 协议并实现其方法以接收有关 QR 码读取的信息。
import DappWalletMX
class ViewController: UIViewController, DappScannerViewControllerDelegate {
//MARK: - DappScannerViewControllerDelegate
func dappScannerViewControllerSuccess(_ viewController: DappScannerViewController, code: DappCode) {
//realice las acciones apropiadas con la información obtenida
print(code.id)
}
func dappScannerViewControllerFailure(_ viewController: DappScannerViewController, error: DappError) {
//tu código para manejar el error
print(error)
switch error {
default:
break
}
}
- 显示 DappScannerViewController。
@IBAction func scanQR(_ sender: Any) {
let vc = DappScannerViewController(delegate: self) //self == ViewController
present(vc, animated: true, completion: nil)
}
将扫码器集成为视图
- 创建一个类(这可以是包含 DappScannerView 的 view controller)并实现 DappScannerViewDelegate 协议,以接收有关 QR 代码读取的信息。
import DappWalletMX
class ViewController: UIViewController, DappPaymentDelegate {
//MARK: - DappScannerViewControllerDelegate
public func dappScannerView(_ dappScannerView: DappScannerView, didChangeStatus status: DappScannerViewStatus) {
switch status {
case .isValidatingQR:
//validando el QR vía https con los servidores Dapp. Realiza las acciones apropiadas en tu aplicación
case .success(let code):
//realice las acciones apropiadas con la información obtenida
case .failed(let e):
//tu código para manejar el error
}
}
- 通过 storyboard 或代码将 DappScannerView 添加到你的 view controller 中,并分配 delegate。
@IBOutlet var scannerView: DappScannerView!
override public func viewDidLoad() {
super.viewDidLoad()
//scannerView = DappScannerView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
//view.addSubview(scannerView)
scannerView.delegate = self //self == ViewController
}
- 要开始扫描,请使用 startScanning 函数。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scannerView.startScanning()
}
- 要停止扫描,请使用 stopScanning 函数。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scannerView.startScanning()
}
- 要检查扫码器是否激活,请使用 isScanning 函数。
scannerView.isScanning()