sar*_*rah 1 ios swift firebase-authentication google-signin
我已经成功地在我使用 Firebase 的应用中实现了一个谷歌登录。当我第一次使用谷歌账户登录时,会出现一个选择谷歌账户的对话框。
但是在我成功登录然后注销后,当我再次尝试登录时,选择谷歌帐户的对话框不再出现,它只是直接将我发送到主菜单。
每次用户通过谷歌登录时,我都希望始终显示该对话框。这是我使用的代码,在我的 VC 中有谷歌登录按钮
import UIKit
import Firebase
import GoogleSignIn
import SVProgressHUD
class AuthenticationVC : UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
var userID : String?
override func viewDidLoad() {
super.viewDidLoad()
// delegate declaration
GIDSignIn.sharedInstance().uiDelegate = self
// For Google SignIn Using Firebase
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
}
@IBAction func googleButtonDidPressed(_ sender: Any) {
GIDSignIn.sharedInstance().signIn()
}
}
extension AuthenticationVC {
// MARK: - Firebase Google Sign In Authentication Methods
// The methods below will be triggered if the user want to login via google account
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:])
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication,annotation: annotation)
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
SVProgressHUD.show(withStatus: "Harap Tunggu")
if let error = error {
SVProgressHUD.dismiss()
print("failed to login into google")
print("\(error.localizedDescription)")
return
}
print("user successfully signin into google")
guard let authentication = user.authentication else {
SVProgressHUD.dismiss()
return
}
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {
SVProgressHUD.dismiss()
print("failed to create firebase user using google account")
print("\(error.localizedDescription)")
return
}
print("user successfully logged into firebase")
guard let userKMFromGoogleSignIn = user else {
SVProgressHUD.dismiss()
return
}
self.userID = userKMFromGoogleSignIn.uid
// if the user sign Up for the very first time using google account, then user Basic Information stored in the firestore will be nil, therefore we create user basic data to firestore
// if user has signed up before via google account, then directly send to mainTabBar (it means he/she has picked a city before), otherwise pickCity first
self.checkUserBasicInformationInFirestore(userKMFromGoogleSignIn)
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
这是用户点击注销按钮时的代码
@IBAction func logOutButtonDidPressed(_ sender: Any) {
do {
try Auth.auth().signOut()
print("User Did Log Out")
performSegue(withIdentifier: "Back", sender: nil)
} catch {
showAlert(alertTitle: "Sorry", alertMessage: "\(error)", actionTitle: "Back")
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
在输入您使用谷歌登录的视图控制器时尝试退出。注销将使当前令牌过期,您将被要求使用帐户列表再次登录。
override func viewDidLoad() {
//Configure and set delegate
...
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
}
@IBAction func logOutButtonDidPressed(_ sender: Any) {
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2272 次 |
| 最近记录: |