Basically you not call the two delegates into you Viewcontroller.Swift file
Xcode 9.2 Swift 4
Step 1 : Install two pods
pod 'Google'
pod 'Google/SignIn'
Step 2 : Enable Google Sign by giving your bundle identifier of your project. Generate configuration file and import it into your project.
Step 3 : Go to your AppDelegate.swift and import two libraries.
import GoogleSignIn
import Google
Step 4 : Replace you appDelegate function by this code.Client ID is show in your Google GoogleService-Info.plist which you import into your project.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GIDSignIn.sharedInstance().clientID = "CLIENT_ID"
return true
}
Implement this delegate in your AppDelegate file.GIDSignInDelegate
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
} else {
// 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
}
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
let googleDidHandle = GIDSignIn.sharedInstance().handle(url,
sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplicationOpenURLOptionsKey.annotation])
return googleDidHandle
}
}
Step 5 : Go to ViewController.Swift file paste the code and drag the button and connect with button action which is available in code.You only need to connect button with code
import UIKit
import GoogleSignIn
import Google
class ViewController: UIViewController,GIDSignInUIDelegate, GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (error == nil) {
// Perform any operations on signed in user here.
print(user.userID) // For client-side use only!
print(user.authentication.idToken) // Safe to send to the server
print(user.profile.name)
print(user.profile.givenName)
print(user.profile.familyName)
print(user.profile.email)
print(user.authentication.accessToken)
print(user.profile)
} else {
print("\(error.localizedDescription)")
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user:GIDGoogleUser!,
withError error: Error!) {
}
@IBAction func onBtnTapGmailLogin(sender: GIDSignInButton) {
GIDSignIn.sharedInstance().signIn()
}
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().delegate = self
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}