我已根据Firebase指南在我的iOS应用中使用Firebase实施了Google SignIn。问题是每次我测试登录时,它总是要求权限
以下是Google SignIn中涉及的AppDelegate代码:
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
print("Logged in with Google successfull")
// ... Firebase authentication ...
}
}
这里是带有GIDSignInButton的View Controller代码:
import UIKit
class IntroViewController: UIViewController, GIDSignInUIDelegate {
@IBOutlet weak var signInButton: GIDSignInButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
GIDSignIn.sharedInstance().uiDelegate = self
signInButton.style = .wide
}
}
我在网上搜索了很多但却一无所获......所以,我怎样才能阻止每次都获得许可?
答案 0 :(得分:3)
我相信这是按预期工作的。问题是您在测试应用时注销了用户,并且Google登录库假设如果用户已明确退出您的应用,则应该再次要求该用户获得许可在。中签署该用户。
如果您不明确退出用户,则signIn()
来电和signInSilently()
来电都应成功,而不会显示任何类型的登录屏幕。
答案 1 :(得分:1)
我不完全确定你在问什么。您想检查用户是否已使用其Google帐户登录吗?
if (GIDSignIn.sharedInstance().hasAuthInKeychain()) {
// user is signed in
} else {
// show login
}
答案 2 :(得分:1)
您可以通过以下方式覆盖问题:您必须在内部数据库中存储 currentID ,当您打开应用时,您会获得 currentID ,从而获得所有您的用户的信息。 我在Facebook登录时遇到了同样的问题,我解决了这个问题。 干得好
答案 3 :(得分:1)
您可以尝试以静默方式登录用户,但是如果您已经可以使用GIDAuthentication
属性,则必须确定是否有 - 例如 - idToken ,的accessToken 强> ..等
然后请致电
signIn.signInSilently()
假设signIn
是GIDSignIn
在以下位置查找 - (void)signInSilently : - Google Docs
答案 4 :(得分:1)
成功登录Google登录后,身份验证firebase用户为我工作,我使用了以下代码,
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError?) {
if error != nil {
return
}
let authentication = user.authentication
let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken,
accessToken: authentication.accessToken)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
}
}
}
答案 5 :(得分:0)
According to Firebase official documentation, you should add the following function in AppDelegate:
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
And, if you want this to work in iOS 8 and less, you should add the function you already have:
func application(application: UIApplication,
openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication,
UIApplicationOpenURLOptionsAnnotationKey: annotation]
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
You can try adding both functions (As suggested by Firebsae), and see if this fixes the error. I'm not sure if this will gonna solve it, but at least is an improvement to the GID feature.
EDIT: Source: https://firebase.google.com/docs/auth/ios/google-signin