通过Google Plus iOS登录时应用崩溃

时间:2018-11-20 10:18:55

标签: ios swift xcode swift4 google-plus

尝试集成google plus登录名。

但是当我写let gpSignin = GIDSignIn.sharedInstance()

应用崩溃并显示错误

  

由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ ***-[__ NSArrayM objectAtIndex:]:索引0超出了空数组的范围”

这是我按钮操作中的代码

  @IBAction func googleClicked(_ sender: Any)
{

    print(isConnectedToNetwork())
    if isConnectedToNetwork()
    {
        let gpSignin = GIDSignIn.sharedInstance()
        gpSignin?.scopes.append("https://www.googleapis.com/auth/plus.login")
        gpSignin?.delegate = self
        gpSignin?.uiDelegate = self

        gpSignin?.clientID = "my client ID"
        gpSignin?.shouldFetchBasicProfile = true
        gpSignin?.shouldGroupAccessibilityChildren = true

        if (gpSignin?.hasAuthInKeychain())!{
            print("Signed In")
           // GIDSignIn.sharedInstance().signInSilently()
            gpSignin?.signIn()
            // logInToBackendServerWithAuthIdToken()
        } else{
            print("Not Signed In")
            gpSignin?.signOut()
            gpSignin?.signIn()
            //  region.show(googleLoginView())
        }

    }
    else
    {
        networkError()

    }
} 

请帮助我解决此问题。谢谢

1 个答案:

答案 0 :(得分:1)

您不能编辑/追加范围数组,因为它是NSArray。

尝试将其更改为以下

@IBAction func googleClicked(_ sender: Any)
{

print(isConnectedToNetwork())
if isConnectedToNetwork()
{
    if let gpSignin = GIDSignIn.sharedInstance() {

    if gpSignin.scopes != nil {
                let scopes = NSMutableArray(array: gpSignin.scopes)
                scopes.add("https://www.googleapis.com/auth/plus.login")
                if let convertedScopes = scopes as? [Any] {
                    gpSignin.scopes = convertedScopes
                }
            }
    gpSignin.delegate = self
    gpSignin.uiDelegate = self

    gpSignin.clientID = "my client ID"
    gpSignin.shouldFetchBasicProfile = true
    gpSignin.shouldGroupAccessibilityChildren = true

    if (gpSignin.hasAuthInKeychain())!{
        print("Signed In")
       // GIDSignIn.sharedInstance().signInSilently()
        gpSignin.signIn()
        // logInToBackendServerWithAuthIdToken()
    } else{
        print("Not Signed In")
        gpSignin.signOut()
        gpSignin.signIn()
        //  region.show(googleLoginView())
    }


    }
}
else
{
    networkError()

}
}