经过一番努力终于找到了解决办法。
我的环境是
斯威夫特 3,
iOS 10.3.x,
Xcode 8.3.2
在这里发布完整的解决方案
import UIKit
import GoogleSignIn
class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
@IBOutlet weak var signInButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// set delegates
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
let buttonFrame : CGRect = CGRect.init(x: 0, y: 0, width: 100, height: 50)
let gdSignInButton = GIDSignInButton.init(frame:buttonFrame)
gdSignInButton.center = view.center
view.addSubview(gdSignInButton)
// gdSignInButton.addTarget(self, action: #selector(self.didTapSignOut(sender: self.gdSignInButton)), for: UIControlEvents.touchUpInside)
}
@IBAction func didTapSignOut(sender: GIDSignInButton) {
GIDSignIn.sharedInstance().signOut()
}
func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
guard error == nil else {
print("Error while trying to redirect : \(error)")
return
}
print("Successful Redirection")
}
//MARK: GIDSignIn Delegate
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)
{
if (error == nil) {
// Perform any operations on signed in user here.
let userId = user.userID // For client-side use only!
print("User id is \(userId)")
let idToken = user.authentication.idToken // Safe to send to the server
print("Authentication idToken is \(idToken)")
let fullName = user.profile.name
print("User full name is \(fullName)")
let givenName = user.profile.givenName
print("User given profile name is \(givenName)")
let familyName = user.profile.familyName
print("User family name is \(familyName)")
let email = user.profile.email
print("User email address is \(email)")
// ...
} else {
print("ERROR ::\(error.localizedDescription)")
}
}
// Finished disconnecting |user| from the app successfully if |error| is |nil|.
public func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!)
{
}
}
我的 AppDelegate 看起来像这样。
import UIKit
import Google
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize sign-in
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url as URL!,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
}