Handling openURL: with Facebook and Google

后端 未结 10 1600
长情又很酷
长情又很酷 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:14

    The method "application:openURL:sourceApplication:annotation:" is deprecated from iOS9. so now you can use like.

    - (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary *)options {
           // For Google sign in SDK
           if ([[GIDSignIn sharedInstance] handleURL:url
                                   sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                          annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]) {
              return YES;
           // For Facebook SDK
           }else if ( [[FBSDKApplicationDelegate sharedInstance] application:app
                                                              openURL:url
                                                    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                           annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]){
              return YES;
          //For Google plus SDK
          }else if ([GPPURLHandler handleURL:url
                           sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                  annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]){
              return YES;
          }
    
         return NO;
    }
    
    0 讨论(0)
  • 2021-01-01 10:15

    We don't need to Explicitly need to check the URL, below code does it :-

    - (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation
    {
    
        if ([GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation]) {
            return YES;
        }else if([FBAppCall handleOpenURL:url sourceApplication:sourceApplication]){
            return YES;
        }
    
        return NO;
    }
    
    0 讨论(0)
  • 2021-01-01 10:18

    You can Try the following :

    if ([[url absoluteString] rangeOfString:@"<Your Google Bundle ID>"].location ==            NSNotFound)
    {
        // Call FBAppCall's handleOpenURL:sourceApplication to handle Facebook app responses
        BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
        // You can add your app-specific url handling code here if needed
        return wasHandled;
    }
    else
    {
        return [GPPURLHandler handleURL:url
                      sourceApplication:sourceApplication
                             annotation:annotation];
    }
    return YES;
    

    Call the above method in

    (BOOL)application:(UIApplication *)application
              openURL:(NSURL *)url
    sourceApplication:(NSString *)sourceApplication
           annotation:(id)annotation 
    

    in your appDelegeate.m

    Basically what this is going to do is examine the url prefix and then call the google+ method if url prefix is ur google+ bundle id , and if not , it'll call the fb method . Hope this helps

    0 讨论(0)
  • 2021-01-01 10:18
    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
       sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation
    {
    
    
       if( [GPPURLHandler handleURL:url
                      sourceApplication:sourceApplication
                         annotation:annotation])
       {
           return [GPPURLHandler handleURL:url
                         sourceApplication:sourceApplication
                                annotation:annotation];
       }
        else if([[FBSDKApplicationDelegate sharedInstance] application:application
                                                              openURL:url
                                                    sourceApplication:sourceApplication
                                                           annotation:annotation])
        {
            return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                  openURL:url
                                                        sourceApplication:sourceApplication
                                                               annotation:annotation];
        }
        return NO;
    }
    
    0 讨论(0)
  • 2021-01-01 10:21

    You can try this for a Swift 4.2 version:

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool 
    {
        if (url.scheme?.hasPrefix("fb"))! {
            return SDKApplicationDelegate.shared.application(app, open: url, options: options)
        } 
        else
        {
            return GIDSignIn.sharedInstance().handle(url as URL?, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
                                                                     annotation: options[UIApplicationOpenURLOptionsKey.annotation])
        }           
    }
    
    0 讨论(0)
  • 2021-01-01 10:31

    You can just let either Google SDK or Facebook SDK attempt handling and if the SDK does not handle then allow the other SDK to try:

        @available(iOS 9.0, *)
    private func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any])
        -> Bool {
            let handled: Bool = SDKApplicationDelegate.shared.application(application, open: url, options: options)
    
            if handled { return handled }
    
            return GIDSignIn.sharedInstance().handle(url,
                                                     sourceApplication:options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
                                                     annotation: [:])
    }
    
    //deprecated method iOS 8 and older
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let handled: Bool =  SDKApplicationDelegate.shared.application(application,
                                                                       open: url,
                                                                       sourceApplication: sourceApplication,
                                                                       annotation: annotation)
    
        if handled { return handled }
    
        return GIDSignIn.sharedInstance().handle(url,
                                                 sourceApplication: sourceApplication,
                                                 annotation: annotation)
    }
    
    0 讨论(0)
提交回复
热议问题