web-dev-qa-db-fra.com

Comment utiliser à la fois la connexion google + et facebook dans le même appdelegate.swift

Mon application utilise la connexion google + et dans mon appdelegate.Swift j'ai:

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)
}

Maintenant, je voudrais également insérer une connexion Facebook, mais je dois ajouter dans appdelegate.Swift ce 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)
}

Mais cette erreur de retour car les fonctions 'application' existent déjà, comment puis-je effectuer à la fois google + et facebook même appdelegate.Swift Merci.

18
Paolo Gdf

Votre application doit gérer les liens facebook and google, puis renvoyer true si l'un or l'autre peut gérer le lien donné.

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
}

Et dans 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)

}
80
tgyhlsb

Dans Swift 3, devez faire de cette manière:

Remplacez et implémentez la méthode suivante:

   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
}

Ensuite:

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
}
14
hackmajoris

dans Swift 5 vous pouvez le faire de cette façon:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    FBSDKCoreKit.ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

    GIDSignIn.sharedInstance().clientID = "your_client_id"
    GIDSignIn.sharedInstance().delegate = self

    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handledFB = FBSDKCoreKit.ApplicationDelegate.shared.application(app, open: url, options: options)
    let handledGoogle = GIDSignIn.sharedInstance().handle(url)
    return handledFB || handledGoogle
}
0
Max Niagolov