Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用谷歌和x2B;和facebook登录在同一appdelegate.swift中_Ios_Iphone_Facebook_Swift2_Google Plus - Fatal编程技术网

Ios 如何使用谷歌和x2B;和facebook登录在同一appdelegate.swift中

Ios 如何使用谷歌和x2B;和facebook登录在同一appdelegate.swift中,ios,iphone,facebook,swift2,google-plus,Ios,Iphone,Facebook,Swift2,Google Plus,我的应用程序使用google+登录,在我的appdelegate.swift中,我有: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. var conf

我的应用程序使用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)
}
但是这个返回错误是因为函数“application”已经存在,我如何同时执行google+和facebook相同的appdelegate.swift呢
谢谢。

您的应用程序应该处理facebook
谷歌链接,然后返回
true
,如果其中一个
可以处理给定的链接

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
}
didfishLaunching
中:

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)

}

在Swift 3中,需要这样做:

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
}
重写并实现下一个方法:

   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
}
然后:


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