微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在小型 Swift 应用程序中接收来自 Square Point of Sale 的结果

如何解决无法在小型 Swift 应用程序中接收来自 Square Point of Sale 的结果

我按照此处提供的信息和代码示例使用 Xcode 12.5 在 Swift 中编写了一个小应用程序...https://github.com/square/SquarePointOfSaleSDK-iOS

该应用会轮询服务器以查看是否需要收费。服务器的输出采用 JSON 格式。收费时,JSON 结果会提供客户 ID、要收费的金额以及 Square 销售点 SDK 的注释。

使用来自 GitHub 页面的 SCCAPIRequest 示例...

// Replace with your app's URL scheme.
let callbackURL = URL(string: "<#T##Your URL Scheme##String#>://")!

// Your client ID is the same as your Square Application ID.
// Note: You only need to set your client ID once,before creating your first request.
SCCAPIRequest.setApplicationID(<#T##Application ID##String#>)

do {
    // Specify the amount of money to charge.
    let money = try SCCMoney(amountCents: 100,currencyCode: "USD")

    // Create the request.
    let apiRequest =
        try SCCAPIRequest(
                callbackURL: callbackURL,amount: money,userInfoString: nil,locationID: nil,notes: "Coffee",customerID: nil,supportedTenderTypes: .all,clearsDefaultFees: false,returnsAutomaticallyAfterPayment: false,disablesKeyedInCardEntry: false,skipsReceipt: false
        )

    // Open Point of Sale to complete the payment.
    try SCCAPIConnection.perform(apiRequest)

} catch let error as NSError {
    print(error.localizedDescription)
}

该应用程序成功切换到 Square POS,显示应付金额,并知道我要向哪个客户收费(通过客户 ID)。我可以处理付款,Square POS 切换回我的应用就好了。

这就是我遇到麻烦的地方。我还在同一页面上使用 UIApplication 委托方法示例。在评论“处理成功的请求”下......

func application(_ app: UIApplication,open url: URL,options: [UIApplicationopenURLOptionsKey : Any] = [:]) -> Bool {
guard SCCAPIResponse.isSquareResponse(url) else {
    return
}

do {
    let response = try SCCAPIResponse(responseURL: url)

    if let error = response.error {
        // Handle a Failed request.
        print(error.localizedDescription)
    } else {
        // Handle a successful request.
    }

} catch let error as NSError {
    // Handle unexpected errors.
    print(error.localizedDescription)
}

return true

}

添加了以下内容...

print("Transaction successful: \(response)")

据我所知,响应应包括事务 ID 以及在 userInfoString 中传递的任何内容。当 Square POS 返回到我的应用程序时,此代码示例似乎甚至没有触发。我在 Xcode 控制台中看不到任何东西。

我已经使用上面链接中的文档在 Xcode 中分配了一个回调 URL,它也添加到了 Square Developer Portal 的 Point of Sale API 中。

我错过了什么? UIApplication 委托方法应该放在哪里,在 AppDelegate.swift 中还是应该驻留在 ViewController.swift 中,或者其他地方?任何见解将不胜感激。

解决方法

@ElTomato 提示我需要解决我遇到的问题。我需要删除 SceneDelegate.swift,从 Info.plist 中删除应用程序场景清单,并从 AppDelegate.swift 中删除一些代码

我在这个网站上找到了详细的说明......

iOS 13: Swift - 'Set application root view controller programmatically' does not work

非常感谢@ElTomato 的大力帮助

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。