OpenURL for Facebook and Twitter in the delegate app?

I use the SDK for Twitter and the Twitter SDK for login and registration.

But they both do not open url from one common method. As I wrote 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 great, and for twitter I have to comment on this method above and 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 just fine for Twitter.

The problem is that I need to write one common method to open my url in appDelegate. So how do I overcome it?

Thanks in advance.

NOTE. We cannot write both methods in the application delegate.

+5
source share
5 answers

Finally, I found a 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 a method that allows me to write URLs for Facebook and Twitter.

 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 everyone who tried to answer my question.

Thanks.

+18
source

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) } 
+6
source

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) } 
+2
source
 - (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]]; } 
+1
source

Here is my attempt to open OpenURL for Twitter and Facebook using 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 them:

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

All Articles