简体   繁体   English

Firebase Google SignIn每次都会询问权限

[英]Firebase Google SignIn asks every time for permissions

I have implemented a Google SignIn with Firebase in my iOS app, following the Firebase guide. 我已根据Firebase指南在我的iOS应用中使用Firebase实施了Google SignIn。 The problem is that every time I test the login, it always asks for permissions. 问题是每次我测试登录时,它总是要求权限。
在此输入图像描述

Here is the code of AppDelegate involved in 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 ...

    }
}

And here the code of View Controller with GIDSignInButton: 这里是带有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
    }


}

I've searched a lot on the web but found nothing... So, how can I prevent to ask for permission every time? 我在网上搜索了很多但没有发现任何东西......那么,我怎样才能阻止每次都获得许可?

I believe this is working as intended. 我相信这是按预期工作的。 The issue is that you're signing out the user when you're testing your app, and the Google Sign-in library makes the assumption that if a user has explicitly signed out of your app, it should ask that user for permission again before signing that user in. 问题是您在测试应用时注销了用户,并且Google登录库假设如果用户已明确退出您的应用,则应该再次要求该用户获得许可签署该用户。

If you don't explicitly sign out the user, both signIn() calls and signInSilently() calls should succeed without displaying any kind of sign-in screen. 如果您没有明确注销用户,则signIn()调用和signInSilently()调用都应该成功,而不显示任何类型的登录屏幕。

I'm not entirely sure what you're asking. 我不完全确定你在问什么。 Do you want to check if the user is already signed in with their Google account? 是否要检查用户是否已使用其Google帐户登录?

if (GIDSignIn.sharedInstance().hasAuthInKeychain()) {
    // user is signed in
} else {
    // show login
}

You can override the problem in this way: You have to store in your internal database your currentID and when you open your app you get currentID and consequently you get all info for your user. 您可以通过以下方式覆盖问题:您必须在内部数据库中存储currentID ,当您打开应用程序时,您将获得currentID ,从而获得用户的所有信息。 I had the same problem with the Facebook Login and I solved with this issue. 我在Facebook登录时遇到了同样的问题,我解决了这个问题。 Good job 做得好

You can try signing in the user silently, But you will have to make sure wether or not if GIDAuthentication properties are already available to you or not such as - idToken , accessToken ..etc 您可以尝试以静默方式登录用户,但是如果您已经可以使用GIDAuthentication属性,则必须确定是否存在,例如 - idTokenaccessToken ..etc

Then just call for 然后请致电

signIn.signInSilently()

assuming that signIn is the parameter of type GIDSignIn 假设signInGIDSignIn类型的参数

look for - (void) signInSilently in :- Google Docs 寻找- (无效)signInSilently in: - Google Docs

On successful google sign-in, Authentication firebase user worked for me, I used following code, 在成功登录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


        }

    }
}

According to Firebase official documentation, you should add the following function in AppDelegate: 根据Firebase官方文档,您应该在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: 而且,如果你想让它在iOS 8及更低版本中运行,你应该添加你已经拥有的功能:

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. 您可以尝试添加这两个函数(如Firebsae建议的那样),看看是否可以修复错误。 I'm not sure if this will gonna solve it, but at least is an improvement to the GID feature. 我不确定这是否会解决它,但至少是对GID功能的改进。

EDIT: Source: https://firebase.google.com/docs/auth/ios/google-signin 编辑:来源: https//firebase.google.com/docs/auth/ios/google-signin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM