Google登录API

首先需要说我不使用CocoaPods。 这是我第一次使用Google API。
在Google指南中说我需要在application:didFinishLaunchingWithOptions:配置GIDSignIn application:didFinishLaunchingWithOptions:方法,但我也使用在此方法中配置的Facebook API。 此外,当我尝试在此方法中配置G API时,我收到错误: Type 'AppDelegate' does not conform to protocol 'GIDSignInDelegate' Value of type 'GIDSignIn' has no member 'configureWithError'
如何在AppDelegate中配置GIDSignIn

 Bridging Header #ifndef Bridging_Header_h #define Bridging_Header_h #import  #import  #import  #import  #endif AppDelegate @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // 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) } func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application( application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) } func applicationDidBecomeActive(application: UIApplication) { FBSDKAppEvents.activateApp() } } ViewController func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { if (error == nil) { // Perform any operations on signed in user here. let userId = user.userID // For client-side use only! let idToken = user.authentication.idToken // Safe to send to the server let fullName = user.profile.name let givenName = user.profile.givenName let familyName = user.profile.familyName let email = user.profile.email print(userId) print(idToken) print(fullName) print(givenName) print(familyName) print(email) } else { print("\(error.localizedDescription)") } } @IBAction func gPlusLoginButtonPressed(sender: AnyObject) { var googleSignIn: GIDSignIn! googleSignIn = GIDSignIn.sharedInstance(); googleSignIn.delegate = self googleSignIn.uiDelegate = self googleSignIn.shouldFetchBasicProfile = true; googleSignIn.clientID = "24189713900-d5i1fokf9eubmb03thavk7ht371210ji.apps.googleusercontent.com" googleSignIn.scopes.append("https://www.googleapis.com/auth/plus.login") googleSignIn.scopes.append("https://www.googleapis.com/auth/plus.me") googleSignIn.scopes.append("profile") // googleSignIn.signInSilently() googleSignIn.signIn(); } 

从didFinishLaunch GIDSignIn.sharedInstance()。delegate = self中删除此行

并在您的视图中控制器类实现GIDSignInDelegate,GIDSignInUIDelegate协议

并在您的视图控制器viewDidload方法中写这个

 func viewDidLoad() { GIDSignIn.sharedInstance().delegate = self GIDSignIn.sharedInstance().uiDelegate = self } 

并且不要忘记在app delegate中处理url。

 func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { var flag: Bool = false // handle Facebook url scheme if let wasHandled:Bool = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) { flag = wasHandled } if let googlePlusFlag: Bool = GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication!, annotation: annotation) { flag = googlePlusFlag } return flag } 

GGLContext是Google的一部分,因此仅导入GoogleSignIn会给您带来错误。 您需要导入Google库。

链接到Google Library 2.0.3 https://www.gstatic.com/cpdc/a96d915a636d0afb-Google-2.0.3.tar.gz

使用Google登录。 您需要符合GIDSignInDelegate和GIDSignInUIDelegate以及实现委托方法。

 class LoginViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate { func viewDidLoad() { GIDSignIn.sharedInstance().clientID = Resources.googlePlusClientId() GIDSignIn.sharedInstance().shouldFetchBasicProfile = true GIDSignIn.sharedInstance().scopes = ["profile", "email"] GIDSignIn.sharedInstance().delegate = self GIDSignIn.sharedInstance().uiDelegate = self } func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { } } 

并在AppDelegate

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