2015-08-28 132 views
3

我正在开发一个iOS Swift应用程序,它可以让Google Sing In和Google App Invite。要做到这一点我使用GoogleSignIn V2.2.0如下:GIDSignIn验证钥匙串未保存

func loginRequest(onSuccess successBlock:() -> Void, onError errorBlock: (String?) -> Void) { 

    var signIn = GIDSignIn.sharedInstance() 
    signIn.shouldFetchBasicProfile = true 
    signIn.allowsSignInWithWebView = false 
    signIn.allowsSignInWithBrowser = true 
    signIn.scopes = ["https://www.googleapis.com/auth/plus.login"] 
    signIn.delegate = self 

    if signIn.hasAuthInKeychain(){ 
     signIn.signInSilently() 
    }else{ 
     signIn.signIn() 
    } 

} 

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { 
    if (error == nil) { 
     // managing login data here    
     self.successBlock() 
    } else { 
     self.errorBlock(error.localizedDescription) 
    }  
} 

到这里一切都按预期工作,我能够成功登录我需要什么未来,如上所述,是使用需要先登录的Google应用邀请,因此我需要用户保持登录状态,这就是为什么我不会拨打GIDSignIn.sharedInstance().signOut()。如果我刚刚在登录后调用我的邀请方法,它将按预期打开邀请对话框,但是我遇到的问题仅在我的应用重新启动并且GIDSignIn.sharedInstance().hasAuthInKeychain()始终为false时才会发生。所以我的问题是:在应用程序从内存中释放并在以后使用时,是否可以在钥匙串中保留auth。如果不是真的让我困惑的是signInSilently()这种看起来完全没有意义的方法

回答

2

我有同样的问题。不过,我发现有关Google Sign-In的一点。
我写[[GIDSignIn sharedInstance] signIn];并在不同的原始方法[[GIDSignIn sharedInstance] signInSilently];

例如,

- (void)checkOAuth { 
    [GIDSignIn sharedInstance].uiDelegate = self; 
    if([GIDSignIn sharedInstance].hasAuthInKeychain) { 
     GIDGoogleUser *user = [GIDSignIn sharedInstance].currentUser; 
     if(!user) { 
      [[GIDSignIn sharedInstance] signInSilently]; 
     } 
    } 
} 

- (void)didReceivedLogin { 

    [GIDSignIn sharedInstance].uiDelegate = self; 
    [GIDSignIn sharedInstance].scopes = @[@https://www.googleapis.com/auth/drive"]; 
    // Google SignInを実行 
    [[GIDSignIn sharedInstance] signIn]; 

} 

不过,我可以通过固定代码hasAuthInKeychain得到真实的。
首先,我改变了在ViewDidLoad方法中编写GIDSignIn的新方法。 它在我的应用程序中统一了有关GIDSignIn的状态。

我希望这会帮助你。