Handling different URL Schemes in iOS (Facebook and Instagram)

…衆ロ難τιáo~ 提交于 2019-12-19 02:53:10

问题


I am not even sure how to define the problem but here it goes.

I have an application that uses Facebook SDK for user login. I followed the Facebook authorization tutorial. I am not 100% sure how it works but this part in my "AppDelegate.m" seems important.

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

    return [FBSession.activeSession handleOpenURL:url];
}

So far so good. Now I want to implement a similar login for instagram so that the user can access their photos. I run this example without a problem (https://github.com/crino/instagram-ios-sdk). When I tried to import this into my project I got stuck. Because in instagram project there is also a function in the AppDelegate (IGAppDelegate)

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

    return [self.instagram handleOpenURL:url];
}

Now I cant use this function (since it is a duplicate of Facebook one) Is there a way to combine these two function for facebook and instagram (maybe with an "if" for different URLs). Or am I lost

PS: I noticed that when I call my facebook login app the url is something like

fb4333597123414933://authorize/#access_token=BAAGKI2vHLxUBANbDegkrdoc4GJWUZC2clqLAzxz8IxEBZBdEyjrD2oTaGZA0g2AbSGWgvEhONKM6xJWzLCALGUBguqUpor6kXu9ZBewusNZCUe6BOXYvX&expires_in=5166254

in instagram it is like:

igfd725621c5e44198a5b8ad3f7a0ffa09://authorize#access_token=354172840.fd72562.bf6b3611632d4d00b6cef660ea9d9b6f


回答1:


    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        NSLog(@"url: %@", [url scheme]);

        BOOL callBack;
        // Facebook Call back checking.
        if ([[url scheme] isEqualToString:@"facebook_url_schema"])
        {
            callBack = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
        }
        // Instagram call back checking.
        else if ([[url scheme] isEqualToString:@"instagram_url_schema"])
        {
            callBack = [self.instagram handleOpenURL:url];
        }
        return callBack;![enter image description here][1]
    }




回答2:


Swift :-

Example for use both Google+ and Facebook in swift app, both of them require the OpenURL method in the appDelegate.

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?,
    annotation: AnyObject?) -> Bool {

   println("URL : \(url)")
   if(url.scheme!.isEqual("fb1627825840806667")) {
        println("Facebook url scheme")

    return FBSDKApplicationDelegate.sharedInstance().application(
            application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)

   } else {

        println("Google+ url scheme")

       return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)

   }
}



回答3:


For Swift 2.0

//MARK: FACEBOOK , GOOGLE CALLBACK URL METHOD
    //google-Firebase signin - IOS 8
    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
    {
        let googleDidHandle = GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication!, annotation: annotation)

        let facebookDidHandle = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)

        return facebookDidHandle || googleDidHandle
    }

    //google-Firebase signin - IOS 9
    @available(iOS 9.0, *)
    func application(application: UIApplication,
                     openURL url: NSURL, options: [String: AnyObject]) -> Bool {

        let facebookDidHandle = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])

        let googleDidHandle = GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])

        return facebookDidHandle || googleDidHandle
    }



回答4:


I think I found

[url scheme] gives those specific URL schemes

Xcode - multiple URL Schemes



来源:https://stackoverflow.com/questions/16226240/handling-different-url-schemes-in-ios-facebook-and-instagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!