Ios signensilely()生成一个错误代码=-4

Ios signensilely()生成一个错误代码=-4,ios,swift,google-signin,Ios,Swift,Google Signin,在这种情况下,GIDSignIn.sharedInstance().signizenley()返回一个错误: 错误Domain=com.google.GIDSignIn Code=-4“无法执行该操作 已完成。(com.google.GIDSignIn错误-4)。” 在google signin docs或stackOverflow中,我似乎找不到任何关于此错误的文档 如果我为以前未登录的用户请求静默登录,我预计会发生此错误。 但令我惊讶的是,当我有一个用户之前登录过,我试图在几秒钟后再次静默登

在这种情况下,GIDSignIn.sharedInstance().signizenley()返回一个错误:

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

在google signin docs或stackOverflow中,我似乎找不到任何关于此错误的文档

如果我为以前未登录的用户请求静默登录,我预计会发生此错误。 但令我惊讶的是,当我有一个用户之前登录过,我试图在几秒钟后再次静默登录时,这种情况甚至会发生

我遇到的第二个问题是确定是否有用户使用以下方式登录:

GIDSignIn.sharedInstance().currentUser
它是nil或GIDGoogleUser对象

在此问题上取得进展的任何帮助都将不胜感激


谢谢

以下是来自
GIDSignIn.h
的错误代码。当钥匙链中没有身份验证令牌时,
-4
代码由
发送,这非常重要。见评论

// 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,我发现头文件注释实际上是一个很好的查找位置,通常比任何已发布的文档都要信息丰富。

请参考我在

在不实现这些方法的情况下,您应该符合GIDSignInUIDelegate协议

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

它将修复您的错误-4。

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

[[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.stream.read", @"https://www.googleapis.com/auth/plus.me"]];
您应该在以下时间之前调用它:

[[GIDSignIn sharedInstance] hasAuthInKeychain]    


伊戈尔·罗塔鲁得到了正确的答案。关键是在显著地使用
之前设置范围。它将检查用户以前是否使用您以前设置和登录的作用域登录过。

我在这里遇到了同样的问题,但我最终找到了答案。 我发现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

    }

}

如果您使用的是自定义按钮,则需要在keychain中检查auth

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

    GIDSignIn.sharedInstance().signInSilently()

}
else{

    GIDSignIn.sharedInstance().signIn()

}

检查互联网是否可用,然后与您的员工联系。
当iPad未连接到辅助登录检查时,我出现了这个错误。

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()
    }

感谢@jhabbott的代码片段,你知道如果用户在几秒钟前登录,为什么找不到钥匙链项目吗?不知道-如果你第一次使用
signIn
,它就可以了。但愿我知道更多!我也有同样的问题。我签了名,一切都很顺利。我可以再做一次无声签名,没问题。但是如果我关闭应用程序并再次打开它,沉默登录将失败,错误为-4。比如,如果它没有在钥匙链中保存任何东西,那么@jhabbott有什么好运气吗?不确定是否有其他人对此进行了投票,但这绝对为我解决了问题!!这就是答案。在文档中,不知何故我没有注意到signIn()方法。这真的让我发疯了。。。实际上我必须设置两次范围,是的,两次。我在
调用之前直接打印了作用域,这很明显是
调用,当数组中只有一个条目时,它就不起作用了,但当数组中有两个相同的作用域时,它确实起作用了。。。WTF Google?更新:我之前评论中的解决方案仅适用于iOS 11>。
    // google sign in setup
    GIDSignIn.sharedInstance().uiDelegate = self
    if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{
        GIDSignIn.sharedInstance().signInSilently()
    }
    else{
        GIDSignIn.sharedInstance().signIn()
    }