Handling openURL: with Facebook and Google

后端 未结 10 1601
长情又很酷
长情又很酷 2021-01-01 09:39

user in my app can login using 2 services : Facebook or Google

everything works fine, however, in the :

- (BOOL)application:(UIApplication *)applicat         


        
相关标签:
10条回答
  • 2021-01-01 10:34

    The Swift version for iOS 9.0 and above of the accepted answer could be something like this:

    import FacebookCore
    
    [...]
    
        func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            if (GIDSignIn.sharedInstance().handle(url)) {
                return true
            } else if (ApplicationDelegate.shared.application(app, open: url, options: options)) {
                return true
            }
    
            return false
        }
    

    Try to handle the URL with each SDK, the first one that recognizes it ends execution returning true. If no SDK could handle the URL, return false.

    I hope it helps someone,

    Xavi

    0 讨论(0)
  • 2021-01-01 10:34

    This might be the easiest solution,

    import GoogleSignIn
    import FBSDKCoreKit
    
        func application(_ application: UIApplication,
                     open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        if GIDSignIn.sharedInstance().handle(url) {
            return true
        } else if ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
            return true
        }
        
        return false
    }
    
    @available(iOS 9.0, *)
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        if GIDSignIn.sharedInstance().handle(url) {
            return true
        } else if ApplicationDelegate.shared.application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation]
            ) {
            return true
        }
        return false
    }
    
    0 讨论(0)
  • 2021-01-01 10:34

    You'll want to use [[UIApplication sharedApplication] canOpenURL:]

    0 讨论(0)
  • 2021-01-01 10:35

    For Swift users, (The idea from user2144179)

    Import below frameworks

    import Firebase
    import GoogleSignIn
    import FacebookCore // (FBSDKCore's alternative for swift)
    

    and in your delegate methods

    // when your target is under iOS 9.0
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let isFBOpenUrl = SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
        let isGoogleOpenUrl = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)
        if isFBOpenUrl { return true }
        if isGoogleOpenUrl { return true }
        return false
    }
    

    or you can use another 'open url:' method if your target is 9.0+. SDKs include a method for it also.

    Firebase Reference : https://firebase.google.com/docs/auth/ios/google-signin

    Facebook Reference : https://developers.facebook.com/docs/swift/reference/classes/applicationdelegate.html

    0 讨论(0)
提交回复
热议问题