signInSilently()生成错误代码= -4

时间:2015-07-16 17:47:16

标签: ios swift google-signin

我遇到这种情况,GIDSignIn.sharedInstance()。signInSilently()返回错误:

  

错误Domain = com.google.GIDSignIn Code = -4“操作无法执行   完成。 (com.google.GIDSignIn错误-4。)“

我似乎无法在google signin docs或stackOverflow下找到此错误的任何文档。

如果我为之前没有签名的用户请求静默signIn,我希望这会发生错误。 但令我惊讶的是,当我有一个用户之前签名并且我试图在几秒钟后第二次无声地登录时,它甚至会发生。

我遇到困难的第二个问题是确定是否有用户登录使用:

GIDSignIn.sharedInstance().currentUser

是nil或GIDGoogleUser对象。

非常感谢在这个问题上取得进展的任何帮助。

由于

8 个答案:

答案 0 :(得分:10)

以下是GIDSignIn.h的错误代码。当钥匙串中没有身份验证令牌时,-4代码由signInSilently发送。见评论。

// A list of potential error codes returned from the Google Identity SDK.
typedef NS_ENUM(NSInteger, GIDSignInErrorCode) {
  // Indicates an unknown error has occured.
  kGIDSignInErrorCodeUnknown = -1,
  // Indicates a problem reading or writing to the application keychain.
  kGIDSignInErrorCodeKeychain = -2,
  // Indicates no appropriate applications are installed on the user's device which can handle
  // sign-in. This code will only ever be returned if using webview and switching to browser have
  // both been disabled.
  kGIDSignInErrorCodeNoSignInHandlersInstalled = -3,
  // Indicates there are no auth tokens in the keychain. This error code will be returned by
  // signInSilently if the user has never signed in before with the given scopes, or if they have
  // since signed out.
  kGIDSignInErrorCodeHasNoAuthInKeychain = -4,
  // Indicates the user canceled the sign in request.
  kGIDSignInErrorCodeCanceled = -5,
};

对于Google SDK,我发现头文件注释实际上是一个非常好看的地方,通常比任何已发布的文档都更具信息性。

答案 1 :(得分:8)

我遇到了同样的问题。问题在于方法:

[[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.stream.read", @"https://www.googleapis.com/auth/plus.me"]];

你应该在之前调用它:

[[GIDSignIn sharedInstance] hasAuthInKeychain]    

[[GIDSignIn sharedInstance] signIn]   

答案 2 :(得分:4)

我在这里遇到了同样的问题,但我终于找到了答案。 我发现GoogleSignIn采用UserDefault来保持之前的登录状态。请检查您是否使用UserDefault来开发您的应用程序。如果您这样做,请确保如果您想保留以前的登录状态,则不会删除UserDefault中的所有数据。

就我而言,

public func resetUserDafault() {

   let userDefaults = UserDefaults.standard

   let dict = UserDefaults.standard.dictionaryRepresentation()

   for key in dict.keys {

       //GoogleSignIn take this key to check previous signin status

       if key == "GID_AppHasRunBefore"{

           continue

       }

       userDefaults.removeObject(forKey: key);

  }

  UserDefaults.standard.synchronize()

}

override func viewDidLoad() {

    super.viewDidLoad()

    //After doing it, my application is working properly now.

    if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{

        GIDSignIn.sharedInstance().signInSilently()

    }
    else{

        //not sign in

    }

}

答案 3 :(得分:2)

Igor Rotaru有正确的答案。关键是在使用signInSilently之前设置范围。它将检查用户之前是否曾使用您之前设置并使用的登录范围登录。

答案 4 :(得分:2)

如果您使用的是自定义按钮,则需要检查钥匙串中的身份验证。

if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{

    GIDSignIn.sharedInstance().signInSilently()

}
else{

    GIDSignIn.sharedInstance().signIn()

}

答案 5 :(得分:0)

请参考Saving the current GIDGoogleUser instead of signing in on every launch

的回答

你应该遵守GIDSignInUIDelegate协议而不实施这些方法。

signInWillDispatch:error: 
signIn:presentViewController:
signIn:dismissViewController:

它会修复你的错误-4。

答案 6 :(得分:0)

检查互联网是否可用,然后继续与您的员工联系。 当iPad未在辅助签名检查中连接时,我收到此错误。

答案 7 :(得分:0)

Igor和Spydy都为我工作

伊戈尔帖子的快速版本

    GIDSignIn.sharedInstance().uiDelegate = self

    GIDSignIn.sharedInstance()?.hasAuthInKeychain()
    GIDSignIn.sharedInstance()?.signIn()

    // Uncomment to automatically sign in the user.
    GIDSignIn.sharedInstance().signInSilently()

或来自Spydy。

    // google sign in setup
    GIDSignIn.sharedInstance().uiDelegate = self
    if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{
        GIDSignIn.sharedInstance().signInSilently()
    }
    else{
        GIDSignIn.sharedInstance().signIn()
    }