因此,我的应用可以选择使用Google登录。单击Google提供的按钮后,将打开Web视图并让用户输入其凭据。在允许应用访问他们的信息后,应用程序然后签署用户并将SignInViewController更改为TabBarController(他们现在可以相应地进行交互)。
当用户按下“注销”按钮时,他们会按照预期被定向到登录屏幕。但奇怪的是,如果用户再次按下谷歌按钮,他们会自动登录,根本没有进一步的身份验证,也没有选择删除他们的帐户。他们是否有办法清除谷歌帐户凭据以保护用户免遭意外盗窃?
登录功能:
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
if let error = error {
print(error.localizedDescription)
return
}
let authentication = user.authentication
let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
// ...
SignInViewController().signedIn(user)
}
// ...
}
退出功能:
func signOutOverride() {
do {
try! FIRAuth.auth()!.signOut()
CredentialState.sharedInstance.signedIn = false
// Set the view to the login screen after signing out
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = loginVC
} catch let signOutError as NSError {
print ("Error signing out: \(signOutError)")
}
}
迅速
试试GIDSignIn.sharedInstance().signOut()
目标 - c
[[GIDSignIn sharedInstance] signOut];
是的,就像@Rahul所说,下面的代码将是正确的方式。
GIDSignIn.sharedInstance().signOut()
https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift#sign_out_the_user
在使用GoogleSignIn
SDK之后,想要详细说明以前的答案。
我看到了signOut()
和disconnect()
的方法,并想知道差异是什么。
signOut()
是一个同步电话:
// Immediately sets GIDSignIn.sharedInstance()?.currentUser to nil.
// For example, if the user is already signed in:
print(GIDSignIn.sharedInstance()?.currentUser != nil) // true - signed in
GIDSignIn.sharedInstance()?.signOut()
print(GIDSignIn.sharedInstance()?.currentUser != nil) // false - signed out
除了注销外,disconnect()
还允许用户撤消对该应用的访问权限。我认为这意味着如果他们选择再次登录,他们将需要重新授予您应用的任何权限。
根据Google's Developer Docs的说法,如果用户选择与您的应用断开连接,那么您需要删除已存储在您应用中的任何用户的Google数据。
此外,disconnect()
是异步的。断开调用的结果将返回到GIDSignInDelegate.sign(_:didDisconnectWith:withError:)
方法。
// Also sets GIDSignIn.sharedInstance()?.currentUser to nil.
// Asynchronous call. If for example the user was already signed in:
print(GIDSignIn.sharedInstance()?.currentUser != nil) // true - signed in
GIDSignIn.sharedInstance()?.disconnect()
print(GIDSignIn.sharedInstance()?.currentUser != nil) // true - still signed in
// MARK: - GIDSignInDelegate
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
print(GIDSignIn.sharedInstance()?.currentUser != nil) // false - signed out
// Remove any data saved to your app obtained from Google's APIs for this user.
}
public func logOut(on:UIViewController){
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
if let url = NSURL(string: "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://google.com"){
UIApplication.shared.open(url as URL, options: [:]) { (true) in
let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = LoginViewController()
}
}
} catch let signOutError as NSError {
Help.shared.Snack(messageString: "Error signing out: \(signOutError)"
)
print ("Error signing out: %@", signOutError)
}
}
试试我的代码..
@IBAction func onClickSignOut(_ sender: UIButton) {
GIDSignIn.sharedInstance()?.signOut()
// if GIDSignIn.sharedInstance()?.currentUser == nil {//Logged out
// self.navigationController?.popToRootViewController(animated: true)
// } else {//Not logged out
// //Your code here
// }
/* check for user's token */
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
//hasAuthInKeychain() : Checks whether the user has either currently signed in or has previous authentication saved in keychain.
//Not logged out
//Write your code here
//......
} else {
//Logged out
//Write logged out code here
//EX:
self.navigationController?.popToRootViewController(animated: true)
}
}