OpenURL for Facebook and Twitter in app delegate?
Asked Answered
P

6

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.

Portemonnaie answered 11/7, 2016 at 6:36 Comment(2)
Please go through this link, may help you handling-different-url-schemes ThanksPena
Yes i gone through this links which are available in stack overflow but actually for twitter it is not working. Thank you @ChandanPrajapatiPortemonnaie
P
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.

Portemonnaie answered 11/7, 2016 at 10:35 Comment(0)
P
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)
}
Paraphrast answered 3/9, 2017 at 18:17 Comment(2)
The "Twitter" class in Twitter.sharedInstance() ... has been renamed to "TWTRTwitter" as of TwitterKit 3.0Dowery
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 developer.apple.com/documentation/uikit/uiapplicationdelegate/… maybe need use return application(app, open: url, options: options) ?Puto
P
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)
}
Perfect answered 27/1, 2017 at 16:54 Comment(0)
U
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]];

}
Upandcoming answered 26/6, 2017 at 7:0 Comment(1)
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 for providing quality answer.Dowitcher
C
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"
Chook answered 7/8, 2017 at 17:8 Comment(0)
D
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)
}
Dachi answered 27/5, 2021 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.