Google加上iOSGPGManager错误"Mismatched Authentication"

Google Plus iOS GPGManager error "Mismatched Authentication"

我有一个 Unity 应用程序,其中包含一个用于登录 Google Plus 的按钮。在应用程序的 iOS 版本中,我不得不覆盖 Google Plus SDK 的默认行为,以防止它切换到 Safari 来登录并改为使用 WebView。这是由于 Apple's policy of rejecting apps that sign into Google Plus via Safari.

一切正常,登录过程通过 WebView 进行。我添加了一个覆盖 WebView 的 "Cancel" 按钮,以便用户可以决定不登录和 return 应用程序。处理取消按钮的代码是:

- (IBAction)handleCancelClick:(id)sender {
    // Cancel the sign-in process
    [[GPGManager sharedInstance] signOut];
    //Reactivate the sign-in button
    UnitySendMessage("Persistent Services", "cancelSignIn", "");
    // Hide the webview
    [_unityViewController dismissViewControllerAnimated:YES completion:nil];
}

问题是使用取消按钮后的后续登录尝试失败。我在 XCode 中调试时看到以下错误:

2015-04-29 21:33:05.328 [Core] (Error) -[GPGManager finishedWithAuth:error:]:[main]
FAILED LOGGING INTO GOOGLE PLUS GAMES Error Domain=com.google.GooglePlusPlatform
Code=-1 "Mismatched authentication" UserInfo=0x1c163870 
{NSLocalizedDescription=Mismatched authentication}

正如我所说,这 在使用取消按钮后发生。如果登录过程在一个流程中完成,它就可以工作。

按下登录按钮时调用的 Unity 代码是:

public void logIn () {
        Debug.Log ("Attempting to log in");
        signingIn = true;
        Social.localUser.Authenticate ((bool success) =>
        {
            if (success) {
                ...
            } else {
                Debug.Log ("Failed to Log in to Google Play Services");
            }
            signingIn = false;

        });
}

我假设第一次调用 Social.localUser.Authenticate() 时,会设置一些不会在后续调用中被覆盖的状态。当登录过程完成后,它会根据第一次调用 Authenticate() 时设置的内容检查回调授权代码,但它们不匹配。但我可能离题太远了。

您能否建议一种在取消或以其他方式解决此问题时完全重置登录过程的方法?

我通过确保 Social.localuser.Authenticate() 没有被第二次调用来解决这个问题。随后按下 'sign-in' 按钮只会导致 WebView 重新显示。

为此,我在按下 'Cancel' 按钮时从 Objective-C 代码调用的 Unity 方法中设置了一个标志。

#if UNITY_IOS
/**
 * Currently only called from Native iOS code.
 */
public void cancelSignInFromIos() {
    hasCancelledInIos = true;
}
#endif

然后我将登录代码包装在检查此标志值的条件语句中。如果按下 'Cancel' 按钮 ,我会向本机代码发送一条消息,指示它显示 WebView。

if (!hasCancelledInIos)
    Social.localUser.Authenticate ((bool success) =>
    {
        ...
    }
} else {
    #if UNITY_IOS
    hasCancelledInIos = false;
    fromUnityshowWebView ();
    #endif
}

本地方法在Unity端代码中定义如下:

#if UNITY_IOS
System.Runtime.InteropServices.DllImport("__Internal")]
extern static public void fromUnityshowWebView();
#endif

并在本机代码中实现如下:

extern "C" {
    void fromUnityshowWebView() {
        [[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:@"blank"];
    }
}

此处触发的通知由显示 WebView 叠加层的方法处理,例如

- (void) showWebView
{
    [unityViewController presentViewController:self.webViewController animated:YES completion:nil];
}

啰嗦,但有效。