如何在同一个appdelegate.swift中同时使用google +和facebooklogin
我的应用程序使用谷歌+login和我的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) }
现在我想插入Facebooklogin,但我必须添加在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) }
但是,这个返回错误,因为function的'应用程序'已经存在,我怎么能同时执行谷歌+和facebook同样appdelegate.swift谢谢。
你的应用程序应该处理Facebook and
谷歌的链接,然后返回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 }
而在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) }
在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 }