尝试调用canOpenURL的iOS 9上的Googlelogin崩溃

在iOS 9 SDK上testingGoogle 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新更改,要求应用程序将所有需要查询的scheme列入白名单。

iOS 9 URL模式更改Quick Take上的这篇文章解释了原因。

您至less需要在LSApplicationQueriesSchemes下的Info.plist LSApplicationQueriesSchemes自己的应用程序标识符和您的Google OAuth应用程序ID列入白名单。 您还可以将其他计划的Googlelogin查询列入白名单,以使其他警告无效。

 <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> 

只需按照https://developers.google.com/identity/sign-in/ios/start-integrating#add_url_schemes_to_your_project中的步骤(包括添加计划),它应该工作正常&#x3002;

而且,根据以下评论的要求,我会强调这个问题可能是由于没有添加上面链接中描述的URLscheme。

从上面的链接引用:

Googlelogin需要将两个自定义URL计划添加到您的项目中。

要添加自定义scheme:

  1. 打开你的项目configuration:在左侧树视图中双击项目名称。 从TARGETS部分select您的应用程序,然后selectInfo选项卡,然后展开URL Types部分。
  2. 点击+button,为您的反向客户端ID添加一个URLscheme。 要find这个值,打开Goog​​leService-Info.plistconfiguration文件,然后查找REVERSED_CLIENT_ID键。 复制该密钥的值,并将其粘贴到configuration页面上的URL Schemes框中。 将其他字段留空。
  3. 点击+button,然后添加第二个URLscheme。 这个和你应用程序的包ID一样。 例如,如果您的软件包ID是com.example.app,请在URL Schemes框中键入该值。 您可以在项目configuration的“常规”选项卡中find应用的软件包ID(身份>软件包标识符)。

更新了XCode 7.0和Google plus 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> 

参考从这个链接

Josh的答案对于早期版本的Googlelogin库是正确的。 不过,它看起来像从2.2.0版本开始,这一步不再是必要的(hooray!)。

所以,如果您仍然遇到此问题,我的build议可能是更新您的Googlelogin库。

在将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> 

对于我来说,将LSApplicationQueriesSchemes放在info.plist中是行不通的,但把它放在“Project> Target> Info> URL Types”下面对我来说是个窍门。

添加您的com.googleusercontent.apps。 到URLtypes

点击主项目 – >信息 – > URLtypes