Created
June 8, 2020 09:11
-
-
Save LeeKahSeng/08c7e27a6958c56dc49cc4371dc66684 to your computer and use it in GitHub Desktop.
Full Implementation of AppDelegate.swift for Google sign-in integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import GoogleSignIn | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
func application(_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// Initialize Google sign-in | |
GIDSignIn.sharedInstance().clientID = "[OAuth_Client_ID]" | |
GIDSignIn.sharedInstance().delegate = self | |
// If user already sign in, restore sign-in state. | |
GIDSignIn.sharedInstance()?.restorePreviousSignIn() | |
return true | |
} | |
func application(_ app: UIApplication, | |
open url: URL, | |
options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool { | |
return GIDSignIn.sharedInstance().handle(url) | |
} | |
} | |
extension AppDelegate: GIDSignInDelegate { | |
func sign(_ signIn: GIDSignIn!, | |
didSignInFor user: GIDGoogleUser!, | |
withError error: Error!) { | |
// Check for sign in error | |
if let error = error { | |
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue { | |
print("The user has not signed in before or they have since signed out.") | |
} else { | |
print("\(error.localizedDescription)") | |
} | |
return | |
} | |
// Post notification after user successfully sign in | |
NotificationCenter.default.post(name: .signInGoogleCompleted, object: nil) | |
} | |
} | |
// MARK:- Notification names | |
extension Notification.Name { | |
/// Notification when user successfully sign in using Google | |
static var signInGoogleCompleted: Notification.Name { | |
return .init(rawValue: #function) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment