Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS restorePreviousSignIn: restorePreviousSignIn is not triggered in v6.0.1 when reopen the app. #40

Closed
Haibo-Zhou opened this issue Aug 4, 2021 · 3 comments

Comments

@Haibo-Zhou
Copy link

Haibo-Zhou commented Aug 4, 2021

Hello, my iOS app can't restorePreviousSignIn in v6.0.1.

Before, I used signIn:didSignInForUser:withError: to restore user signIn state, and it works fine.
Now, I follow your migration guide, to Move the logic from signIn:didSignInForUser:withError: to the callback block of signInWithConfiguration:presentingViewController:callback:.

Now, I put signInWithConfiguration:presentingViewController:callback: into Google signIn button, I could sign in but when I close and reopen my app, signInWithConfiguration:presentingViewController:callback: is not triggered, then I couldn't restore the previous user signIn state.

Is this the correct position it should be, how could I fix this issue? This version is quite new, I couldn't find related issue in internet, please help, guys.

@objc func signInButtonTapped(_ sender: Any) {
        GIDSignIn.sharedInstance.signIn(with: modelController.signInConfig, presenting: self) { [weak self] user, error in
            
            if let error = error {
                print("SignIn failed, \(error), \(error.localizedDescription)")
            } else {
                print("Authenticate successfully")
                let driveScope = "https://www.googleapis.com/auth/drive.readonly"
                guard let user = user else { return }
    
                let grantedScopes = user.grantedScopes
                print("scopes: \(String(describing: grantedScopes))")
                
                if grantedScopes == nil || !grantedScopes!.contains(driveScope) {
                    // Request additional Drive scope if it doesn't exist in scope.
                    GIDSignIn.sharedInstance.addScopes([driveScope], presenting: self!) { user, error in
                        if let error = error {
                            print("add scope failed, \(error), \(error.localizedDescription)")
                        }

                        guard let user = user else { return }

                        // Check if the user granted access to the scopes you requested.
                        if let scopes = user.grantedScopes,
                           scopes.contains(driveScope) {
                            print("Scope added")
                            print("NEW scopes: \(String(describing: scopes))")
                            self?.createGoogleDriveService(user: user)
                        }
                    }
                }
            }
        }
    }

func createGoogleDriveService(user: GIDGoogleUser) {
        let service = GTLRDriveService()
        service.authorizer = user.authentication.fetcherAuthorizer()

        // dependency inject
        modelController.googleAPIs = GoogleDriveAPI(service: service)

        // Post notification after user successfully sign in
        NotificationCenter.default.post(name: .signInGoogleCompleted, object: nil)
        
        user.authentication.do { [weak self] authentication, error in
            guard error == nil else { return }
            guard let authentication = authentication else { return }
            
            // Or, get an object that conforms to GTMFetcherAuthorizationProtocol for
            // use with GTMAppAuth and the Google APIs client library.
            let service = GTLRDriveService()
            service.authorizer = authentication.fetcherAuthorizer()
            
            // dependency inject
            self?.modelController.googleAPIs = GoogleDriveAPI(service: service)
            
            // Post notification after user successfully sign in
            NotificationCenter.default.post(name: .signInGoogleCompleted, object: nil)
        }
    }

previous restore logic code

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if let error = error {
            print("SignIn failed, \(error), \(error.localizedDescription)")
        } else {
            print("Authenticate successfully")
            let service = GTLRDriveService()
            service.authorizer = user.authentication.fetcherAuthorizer()

            // dependency inject
            modelController.googleAPIs = GoogleDriveAPI(service: service)

            // Post notification after user successfully sign in
            NotificationCenter.default.post(name: .signInGoogleCompleted, object: nil)
        }
    }
@petea
Copy link
Contributor

petea commented Aug 4, 2021

Are you making a call to restorePreviousSignInWithCallback:, passing a GIDSignInCallback block to be called on completion?

@Haibo-Zhou
Copy link
Author

Yes, I make that call. And I found the reason why I cannot restore the login yesterday. I need to put code which creates google api service into restorePreviousSignInWithCallback: 's call back.

Before I only need to create google api service once in the delegate sign(didSignInFor) method, however now I need to put it in two places, one in signIn button and another in restorePreviousSignInWithCallback.

@Haibo-Zhou
Copy link
Author

Close as not an issue on API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants