Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use both google+ and facebook login in same appdelegate.swift

My app use google+ signin and in my appdelegate.swift i have:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(configureError)")

    GIDSignIn.sharedInstance().delegate = self

    return true

}


func application(application: UIApplication,
    openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        return GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: sourceApplication,
            annotation: annotation)
}

Now i would like to insert also facebook login, but i have to add in appdelegate.swift this code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

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

But this return error because the functions 'application' already exist, how can I perform both google+ and facebook same appdelegate.swift Thank you.

like image 791
Paolo Gdf Avatar asked Feb 19 '16 16:02

Paolo Gdf


2 Answers

Your application should handle facebook and google links, then return true if one or the other can handle the given link.

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

        let googleDidHandle = GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: sourceApplication,
            annotation: annotation)

        let facebookDidHandle = FBSDKApplicationDelegate.sharedInstance().application(
            application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)

        return googleDidHandle || facebookDidHandle
}

And in didFinishLaunching :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(configureError)")

    GIDSignIn.sharedInstance().delegate = self

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

}
like image 169
tgyhlsb Avatar answered Oct 14 '22 09:10

tgyhlsb


In Swift 3, need to do in this way:

Override and implement the next method:

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

    let googleHandler = GIDSignIn.sharedInstance().handle(
        url,
        sourceApplication: sourceApplication,
        annotation: annotation )

    let facebookHandler = FBSDKApplicationDelegate.sharedInstance().application (
        app,
        open: url,
        sourceApplication: sourceApplication,
        annotation: annotation )

    return googleHandler || facebookHandler
}

Then:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)
    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

    var configureError: NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(String(describing: configureError))")

    return true
}
like image 22
hackmajoris Avatar answered Oct 14 '22 10:10

hackmajoris