web-dev-qa-db-ja.com

同じappdelegate.swiftでGoogle+とFacebookの両方のログインを使用する方法

私のアプリはgoogle +サインインを使用していて、私のappdelegate.Swiftで以下を持っています:

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

次に、Facebookのログインも挿入したいのですが、appdelegate.Swiftに次のコードを追加する必要があります。

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

しかし、この「アプリケーション」関数が既に存在するため、このエラーが返されます。Google+とFacebookの両方で同じappdelegate.Swiftを実行するにはどうすればよいですか。

18
Paolo Gdf

アプリケーションはfacebook and googleリンクを処理し、一方がtrue、もう一方が指定のリンクを処理できる場合はorを返します。

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
}

そして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

Swift 3では、このようにする必要があります:

次のメソッドをオーバーライドして実装します。

   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
}

次に:

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

Swift 5では、次のように実行できます。

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