9

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

But they both are not opening URL from one common method. I have written code like that below 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 the 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.

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

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

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
  • 1
    Please go through this link, may help you [handling-different-url-schemes](http://stackoverflow.com/questions/16226240/handling-different-url-schemes-in-ios-facebook-and-instagram) Thanks – Chandan Jul 11 '16 at 06:46
  • Yes i gone through this links which are available in stack overflow but actually for twitter it is not working. Thank you @ChandanPrajapati – Jigar Tarsariya Jul 11 '16 at 06:53

6 Answers6

27

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.

Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
13

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)
}
Tung Fam
  • 7,899
  • 4
  • 56
  • 63
  • The "Twitter" class in `Twitter.sharedInstance() ...` has been renamed to "TWTRTwitter" as of TwitterKit 3.0 – WongWray Jan 29 '18 at 07:15
  • I have this problem "Cannot invoke 'application' with an argument list of type '(UIApplication, open: URL, sourceApplication: String?, annotation: String)'" because of this method deprecated https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623073-application?language=objc maybe need use return application(app, open: url, options: options) ? – Yurii Petrov Sep 20 '18 at 10:01
2

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)
}
MLBDG
  • 1,357
  • 17
  • 23
1
- (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]];

}
Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. – thewaywewere Jun 26 '17 at 07:20
0

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"
Hideya
  • 158
  • 1
  • 2
  • 12
0

Swift 5

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool
{
    // Check for twitter 
    if TWTRTwitter.sharedInstance().application(app, open: url, options: options)
    {
        return true
    }
    
    // If twitter failed, check for facebook
    return ApplicationDelegate.shared.application(app, open: url, options: options)
}
Essam Fahmi
  • 1,920
  • 24
  • 31