Create Login With Google Swift 4

1. Start Integrating With Google



* use this link : https://developers.google.com/identity/sign-in/ios/start-integrating









2. After download follow this step



copy REVERSED_CLIENT_ID to below




3 . AppDelegate file

* link: https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift

import UIKit
import GoogleSignIn

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
    
    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        GIDSignIn.sharedInstance().clientID = "93121905680-k05ou2b4k9ju68pdlogn31g7s7u7im3d.apps.googleusercontent.com"
        GIDSignIn.sharedInstance().delegate = self
        
        return true
    }
    
    //Google
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        print("Google Not Connect") // func untuk mengecek koneksi dengan google
    }
    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])
    }
    
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        // google
        let googleDidHandle = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)
        
        return googleDidHandle
    }
    
    
    //other

    func applicationWillResignActive(_ application: UIApplication) {
    }
    func applicationDidEnterBackground(_ application: UIApplication) {
    }
    func applicationWillEnterForeground(_ application: UIApplication) {
    }
    func applicationDidBecomeActive(_ application: UIApplication) {
    }
    func applicationWillTerminate(_ application: UIApplication) {
    }

}
4 . ViewController File



   
import UIKit
import GoogleSignIn

class LoginViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {

    var dict: [String: AnyObject]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
         GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().delegate = self
        // for auto sign in
         GIDSignIn.sharedInstance().signInSilently()
     
    }

    @IBAction func gAction(_ sender: Any) {
        GIDSignIn.sharedInstance().signIn()
    }
    
    // func Google SignIn
    
    func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
        dismiss(animated: true, completion: nil)
    }
    
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if GIDSignIn.sharedInstance().hasAuthInKeychain() {
            print("test Jaringan")
            navigationController?.setViewControllers([HomeViewController()], animated: true)
        
        } else {
            dismiss(animated: true, completion: nil)
        }
        
        if let error = error {
            print("\(error.localizedDescription)")
            NotificationCenter.default.post(
                name: Notification.Name(rawValue: "ToggleAuthUINotification"),
                object: nil,
                userInfo: nil
            )
        } else {
            let idToken = user.authentication.idToken
            print("token Google\(idToken ?? "")")
        }
    }
   
}

5.  LogOut File

import UIKit
import GoogleSignIn

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

     
    }

    @IBAction func keluarAction(_ sender: Any) {
        GIDSignIn.sharedInstance().signOut()
        self.navigationController?.setViewControllers([LoginViewController()], animated: true)
    }
}



No comments: