24

I have an app that will be using both Facebook and Instagram API's, both of which require me to use this delegate method:

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

    return [PFFacebookUtils handleOpenURL:url];

}

and this is the code provided by instagram:

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

    return [self.instagram handleOpenURL:url];

}

As you can see, this is a problem because I can only return one URL handler, but I need to be able to decide which one.

In my app delegate, I have commented out the Instagram option above and left only Facebook. As expected, Facebook will work just fine, but I get this error when I tap authorize on Instagram:

Instagram URL error

Obviously, Facebook is trying to handle the return URL from Instagram so it will error.

Is it plausible to check the URL to see if it contains the app ID for each service I am trying? So if URL contains the instagram ID, then return the instagram handler, if it contains the Facebook ID, then return the Facebook Handler.

Any help would be great, thanks!

Kyle Begeman
  • 7,019
  • 9
  • 37
  • 57

1 Answers1

67

Facebook and Instagram both had you setup a custom URL scheme for their services, so we will use that to filter the URL.

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

    if ([[url scheme] isEqualToString:INSTAGRAM_SCHEME])
        return [self.instagram handleOpenURL:url];

    if ([[url scheme] isEqualToString:FACEBOOK_SCHEME])
        return [PFFacebookUtils handleOpenURL:url];

    return NO;

}

Where you define your variables as

#define INSTAGRAM_SCHEME @"ig12345678910"
#define FACEBOOK_SCHEME  @"fb12345678910"

exactly as you did in your Info.plist file

coneybeare
  • 33,248
  • 21
  • 128
  • 182
  • 1
    Perfect, I should have figured this out but thats what happens when you stare at an obvious solution for too long. This works perfectly, thank you! – Kyle Begeman Dec 30 '13 at 23:06
  • Most people probably already know but it should be INSTAGRAM_SCHEME @"ig12345678910", just incase anyone reads the answer and gets confused! – Kyle Begeman Dec 30 '13 at 23:10
  • 20
    Why compare schemes? Just do: `return [self.instagram handleOpenURL:url] || [PFFacebookUtils handleOpenURL:url];`. This assumes both will return `NO` if the URL isn't appropriate. – rmaddy Dec 30 '13 at 23:41
  • 5
    You can't really make assumptions when dealing with non-open-source code – coneybeare Dec 30 '13 at 23:43
  • 2
    @rmaddy you should add your comment as answer, Thank you it worked for me – M.Alatrash Oct 19 '14 at 09:43