Google 登录在 iOS 9 尝试调用 canOpenURL 时崩溃

Google Sign-In crashes on iOS 9 attempting to call canOpenURL

在 iOS 9 SDK 上测试 Google Sign-In 2.1.0 时,调用 GIDSignIn.sharedInstance().signInSilently() 最终会在内部 Google Sign-In SDK 调用时崩溃至 canOpenURL.

-canOpenURL: failed for URL: "com.googleusercontent.apps.1234567890-abcdefghijklmnopqrstuvwxyz://a" - error: "This app is not allowed to query for scheme com.googleusercontent.apps.1234567890-abcdefghijklmnopqrstuvwxyz"
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Your app must support the following URL schemes: com.example.foo, com.googleusercontent.apps.1234567890-abcdefghijklmnopqrstuvwxyz'

-canOpenURL: failed for URL: "com-google-gidconsent-google://" - error: "This app is not allowed to query for scheme com-google-gidconsent-google"
-canOpenURL: failed for URL: "com-google-gidconsent-youtube://" - error: "This app is not allowed to query for scheme com-google-gidconsent-youtube"
-canOpenURL: failed for URL: "com-google-gidconsent://" - error: "This app is not allowed to query for scheme com-google-gidconsent"
-canOpenURL: failed for URL: "com.google.gppconsent.2.4.1://" - error: "This app is not allowed to query for scheme com.google.gppconsent.2.4.1"
-canOpenURL: failed for URL: "com.google.gppconsent.2.4.0://" - error: "This app is not allowed to query for scheme com.google.gppconsent.2.4.0"
-canOpenURL: failed for URL: "googlechrome:" - error: "This app is not allowed to query for scheme googlechrome"
-canOpenURL: failed for URL: "googlechrome-x-callback:" - error: "This app is not allowed to query for scheme googlechrome-x-callback"
-canOpenURL: failed for URL: "googlechrome-x-callback:" - error: "This app is not allowed to query for scheme googlechrome-x-callback"
-canOpenURL: failed for URL: "googlechrome:" - error: "This app is not allowed to query for scheme googlechrome"

iOS 9 对 canOpenURL 进行了新的更改,要求应用程序将其需要查询的所有方案列入白名单。

Quick Take on iOS 9 URL Scheme Changes 上的 post 解释了原因。

您至少需要在 LSApplicationQueriesSchemes 下的 Info.plist 中将您自己的应用程序标识符和 Google OAuth 应用程序 ID 列入白名单。您还可以将其他方案列入白名单 Google 登录查询以消除其他警告。

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>com.example.foo</string>
    <string>com.googleusercontent.apps.1234567890-abcdefghijklmnopqrstuvwxyz</string>
    <string>com-google-gidconsent-google</string>
    <string>com-google-gidconsent-youtube</string>
    <string>com-google-gidconsent</string>
    <string>com.google.gppconsent.2.4.1</string>
    <string>com.google.gppconsent.2.4.0</string>
    <string>googlechrome</string>
    <string>googlechrome-x-callback</string>
</array>

Josh 的回答对于 Google 登录库的早期版本是正确的。然而,从 2.2.0 版本开始,这一步似乎不再需要(万岁!)。

因此,如果您仍然遇到此问题,我的建议可能是更新您的 Google 登录库。

针对 XCode 7.0 和 Google 以及 SDK 1.7.1 进行了更新

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>com-google-gidconsent-google</string>
        <string>com-google-gidconsent-youtube</string>
        <string>com-google-gidconsent</string>
        <string>com.google.gppconsent.2.4.1</string>
        <string>com.google.gppconsent.2.4.0</string>
        <string>googlechrome</string>
        <string>googlechrome-x-callback</string>
        <string>hasgplus4</string>
        <string>com.google.gppconsent.2.3.0</string>
        <string>com.google.gppconsent.2.2.0</string>
        <string>com.google.gppconsent</string>
    </array>

Reference Taken from this link

对我来说,把 LSApplicationQueriesSchemes 放在 info.plist 里没用,但是把它放在 'Project > Target > Info > URL Types' 下对我来说就成功了。

只需按照以下步骤操作:https://developers.google.com/identity/sign-in/ios/start-integrating#add_url_schemes_to_your_project(包括添加方案),它应该可以正常工作。

并且,根据下面评论的要求,我将强调问题可能是由于未添加上面 link 中所述的 URL 方案而发生的。

引用上面的link:

Google Sign-in requires two custom URL Schemes to be added to your project.

To add the custom schemes:

  1. Open your project configuration: double-click the project name in the left tree view. Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section.
  2. Click the + button, and add a URL scheme for your reversed client ID. To find this value, open the GoogleService-Info.plist configuration file, and look for the REVERSED_CLIENT_ID key. Copy the value of that key, and paste it into the URL Schemes box on the configuration page. Leave the other fields blank.
  3. Click the + button, and add a second URL scheme. This one is the same as your app's bundle ID. For example, if your bundle ID is com.example.app, type that value into the URL Schemes box. You can find your app's bundle ID in the General tab of the project configuration (Identity > Bundle Identifier).

GoogleService-Info.plist 添加到我的项目后,我将以下内容添加到 Info.plist文件,错误消失了。

<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb1413410832299369</string>
            <string>com.my.app</string> <!-- My app bundle ID -->
            <string>com.googleusercontent.apps.1111111111-6jcxxxxxxxikgnso524xxxxxxxx5o9</string> <!-- Reverssed client ID -->
        </array>
    </dict>
</array>

添加您的com.googleusercontent.apps。分为 URL 类型

点击进入主项目 -> 信息 -> URL 类型