JoseMartinFit JoseMartinFit - 2 months ago 15
Swift Question

I have to call this two functions depending on which social network I come from in App Delegate

I'm creating an app that uses social networks to sign in. My issue is that google calls

func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) ->Bool
and Facebook that calls
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
.

The thing is that having that two functions in AppDelegate, Facebook doesn't take me back to app but Google does. I think there's a conflict between these two functions.

I've tried to set a flag var but I can't set an if/else statement outside of any function.

What can I do?

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}

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

return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])

}

Answer

Each of those third party handleURL functions returns a Bool to let you know whether that particular library actually handled the URL in question. You can combine the checks into one individual delegate method and return true if either one of them returns true. I modified your code to show you what that might look like. Notice I am calling the iOS 8 deprecated function in the newer one because I am assuming you are trying to support iOS 8 as well. If that's not the case, you can get rid of that one entirely and move its body into the iOS 9 function.

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool 
{
        if FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
        {
            return true
        }
        else if GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
        {
            return true
        }
        else
        {
            // put more logic here as you need to but for now just return false if you didn't handle the URL
            return false
        }
}

func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool 
{
        return self.application(application, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey]!)
}

edit: As a side note, the reason why your Facebook function wasn't getting called is because on an iOS 9+ system, if it detects you have implemented the newer function, it will never call the older one. If you removed the newer one entirely, you would notice the old one would still get called (and your Facebook handling would work).