You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 3中如何解决Invalid redeclaration错误?Braintree与Google SignIn集成冲突

解决「Invalid redeclaration of 'application(_:open:options:)'」错误

这个问题在集成多个需要处理URL跳转的第三方SDK时非常常见——Braintree和Google SignIn都需要通过application(_:open:options:)方法处理回调,而Swift不允许同一个类中存在两个签名完全一致的方法。我们只需要把两个SDK的处理逻辑合并到同一个方法里即可解决,具体实现如下:

1. 合并回调逻辑到单个方法

删除你代码中重复的application(_:open:options:)方法,只保留一个,并在其中依次处理两个SDK的回调:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // 先尝试让Google SignIn处理当前URL
    let isHandledByGoogle = GIDSignIn.sharedInstance().handle(url)
    
    // 如果Google未处理该URL,再交给Braintree处理
    if isHandledByGoogle {
        return true
    } else {
        return BTAppSwitch.handleOpen(url, options: options)
    }
}

2. 确保SDK初始化配置正确

在application(_:didFinishLaunchingWithOptions:)中完成两个SDK的基础初始化,避免回调失效:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // 初始化Google SignIn
    GIDSignIn.sharedInstance().clientID = "你的Google Client ID" // 替换为你的实际Client ID
    GIDSignIn.sharedInstance().delegate = self
    
    // 初始化Braintree的跳转配置
    BTAppSwitch.setReturnURLScheme("com.your.app.payments") // 替换为你在Info.plist中配置的Braintree专属URL Scheme
    
    return true
}

3. 检查URL Scheme配置

最后确认Info.plist中已正确配置两个SDK所需的URL Scheme:

  • Google SignIn:需添加反向Client ID(格式为com.googleusercontent.apps.xxxxxx)到CFBundleURLTypes数组
  • Braintree:需添加自定义Scheme(如上述示例的com.your.app.payments)到CFBundleURLTypes,同时将com.paypal.ppclient.touch.v1等PayPal相关Scheme添加到LSApplicationQueriesSchemes数组

这样修改后,两个SDK的回调都能被正确处理,重复声明的错误也会消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:46:19