OpenURL for Facebook and Twitter in app delegate?

I am using Facebook SDK and Twitter SDK for login and signup.

But they both are not opening url from one common method. Like i have written like below code for Facebook,

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
    if(url.scheme == "fb1651015905222312")
    {
        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
    }
    return true
}

This works fine and for twitter i have to comment this above method and have to write it like,

 func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    if Twitter.sharedInstance().application(app, openURL:url, options: options) {
        return true
    }
    return true
}

This works fine for twitter only.

Issue is that i need to write one common method to open their URL in appDelegate. So how do i overcome it?

Thanks in advance.

NOTE : We can't write both method in app delegate.

Answers


Finally i found solution for this question.

Swift 3

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    if Twitter.sharedInstance().application(app, open:url, options: options) {
        return true
    }


    let appId = SDKSettings.appId
    if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host ==  "authorize" { // facebook
        return SDKApplicationDelegate.shared.application(app, open: url, options: options)
    }
    return false
}

For Swift < 3

Here is the method which allows me to write url for Facebook and Twitter both.

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

    if Twitter.sharedInstance().application(app, openURL:url, options: options) {
        return true
    }

    let sourceApplication: String? = options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String
    return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: sourceApplication, annotation: nil)
}

Thanks to all who had tried to answer my question.

Thanks.


This Works for Swift 3 and Swift 4. Just copy and paste:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if Twitter.sharedInstance().application(app, open: url, options: options) {
        return true
    }

    let sourceApplication: String? = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String
    return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: sourceApplication, annotation: nil)
}

UPDATED SWIFT 3

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let sourceApplication: String? = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String
    return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: sourceApplication, annotation: nil)
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{

  if ([Twitter.sharedInstance application:app openURL:url options:options])
{

return YES;

}

return [self application:app openURL:url sourceApplication:[options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey] annotation:[options objectForKey:UIApplicationOpenURLOptionsAnnotationKey]];

}

Here is my try for an openURL for Twitter and Facebook with Objective

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {

if ([[url scheme] isEqualToString:FACEBOOK_SCHEME])
return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                      openURL:url
                                            sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                   annotation:nil];
if ([[url scheme] isEqualToString:TWITTER_SCHEME])
return [[Twitter sharedInstance] application:application openURL:url options:options];

return NO;
}

and of course with these:

#define TWITTER_SCHEME @"Twitter-key-from-plistfile"
#define FACEBOOK_SCHEME  @"fb-key-from-plistfile"

Need Your Help

Converting country codes in .NET

c# .net locale globalization iso

In .NET is there any way to convert from three letter country codes (defined in ISO 3166-1 alpha-3) to two letter language codes (defined in ISO 3166-1 alpha-2) eg. convert BEL to BE?

How Clojure's agents compare to Scala's actors?

scala concurrency clojure actor agents

I wrote a simulation of the Ring network topology in Scala (source here) (Scala 2.8 RC7) and Clojure (source here) (Clojure 1.1) for a comparison of Actors and Agents.