You need to enable JavaScript to run this app. 小型Swift应用无法接收Square Point of Sale返回结果问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

小型Swift应用无法接收Square Point of Sale返回结果问题咨询

问题解决方案

1. 回调方法的正确放置位置

  • 仅适配iOS 12及更低版本的项目:application(_:open:options:)方法必须放在AppDelegate.swift的UIApplicationDelegate实现范围内,不能放在ViewController或其他自定义类中。
  • 适配iOS 13及以上版本的项目(Xcode 12.5默认新建项目属于此类):系统会优先触发SceneDelegate的URL回调,需要额外在SceneDelegate.swift中添加对应回调方法,否则AppDelegate的回调永远不会被调用。

2. 常见遗漏配置排查

2.1 修正回调方法的语法错误

你贴出的回调示例代码存在语法错误:guard SCCAPIResponse.isSquareResponse(url) else { return } 缺少Bool类型返回值,会导致编译告警或执行异常,需要修正为return false:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    guard SCCAPIResponse.isSquareResponse(url) else {
        return false
    }
    do {
        let response = try SCCAPIResponse(responseURL: url)
        if let error = response.error {
            print(error.localizedDescription)
        } else {
            print("Transaction successful: \(response)")
        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    return true
}

2.2 添加iOS 13+ Scene回调代码

在SceneDelegate.swift中添加如下代码:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url, SCCAPIResponse.isSquareResponse(url) else {
        return
    }
    do {
        let response = try SCCAPIResponse(responseURL: url)
        if let error = response.error {
            print(error.localizedDescription)
        } else {
            print("Transaction successful: \(response)")
        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

2.3 检查跳转参数配置

你当前代码中returnsAutomaticallyAfterPayment参数为false,付完款后需要用户手动点击Square POS的返回按钮才会触发跳转,若需要自动跳转,将该参数改为true即可。

2.4 校验URL Scheme配置

确认info.plist中URL types -> URL Schemes填写的内容和你代码中callbackURL的前缀完全一致,注意区分大小写,不要携带://后缀。

3. 验证逻辑

完成以上配置后,先尝试手动触发回调:在Safari地址栏输入你配置的URL Scheme(比如myposapp://test),访问后如果能调起你的应用,同时Xcode控制台有对应日志输出,说明回调配置正常,再走Square POS付款流程验证即可。

内容的提问来源于stack exchange,提问作者Martin M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 13:09:03