使用 Google 登录 iOS 时未捕获 NSInvalidArgumentException

Uncaught NSInvalidArgumentException when using Google Sign In for iOS

我今天尝试为 iOS 升级到 Google 登录(我们有一个针对 Google+ 登录构建的现有应用程序)。

我按照入门指南构建了配置,并添加了适当的 URL 方案(例如,应用程序的客户端 ID)。

启动应用程序并尝试使用 Google 登录后,我收到以下未捕获的异常:

2015-06-09 03:43:23.800 <appname>[238:7171] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Your app must support the following URL schemes: (null)'

异常发生在我们的控制之外,在 GIDSignIn.signIn 和 GIDSignIn.delegate.signIn 之间。

我能做些什么来解决这个问题吗?

发生的事情是 Google SDK 正在从 GoogleService-Info.plist 中的字典中检索您的客户端配置,但它很可能找不到任何配置或无法找到该文件。

当您拨打[[GGLContext sharedInstance] configureWithError: ..时,例如:

NSError* configureError;
[[GGLContext sharedInstance] configureWithError: &configureError];

该代码从 Google-ServiceInfo.plist 文件解析您的客户端 ID,您应该将其添加到 supporting files 或其他共享资源文件夹,然后检查您是否还为反向添加了 URI客户端 ID,从 plist 字典中提取。

首先,如果您没有此文件,请按照新 Enable Google services for your app 页面上的向导生成它。

接下来,确保将 GoogleService-Info.plist 文件作为资源复制到 Xcode 项目的 build phases 部分。当您成功将它添加到您的项目(通常最好添加到共享资源部分)时,错误消息将不再指示需要空 URI。

最后,确保您已在应用程序的 URL 中添加了包 ID 和反向客户端 ID - 这应该很容易做到,因为错误消息会告诉您此时缺少什么。

如果这一切仍然给您带来麻烦,您可能需要 try Google Sign-In for iOS using the quickstart sample by 运行:

pod try Google

然后从 SignInExample.xcodeproject 样本开始...

等等!更新iOS 9+

在 iOS 9 中,您必须将您的应用程序想要在 Info.plist 中查询的任何 URL 方案列入白名单,在 LSApplicationQueriesSchemes 键(字符串数组)

因此您需要在 LSApplicationQueriesSchemes 下添加您的包 ID 和 Google-ServiceInfo 中的反向 ID。

我找了很久 google 文档还没有提到...

我的应用程序每次都在 appDelegate 中崩溃,直到我在 info.plist...

中添加这些行

完成后: Here you'll get all about URL Scheme problem

将配置文件添加到您的项目

将刚刚下载的 GoogleService-Info.plist 文件拖到 Xcode 项目的根目录中,并将其添加到所有目标中。 将 URL 个方案添加到您的项目

Google 登录需要将两个自定义 URL 方案添加到您的项目中。 要添加自定义方案:

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

从 iOS9 开始 您必须为 info.plist 文件设置 4(四)个值

URL 类型

  • Select 您在项目设置中的目标应用程序
  • Select 顶部栏上的信息
  • 向下滚动到 URL 类型
  • 添加一个URL类型
  • 使用 GoogleService-Info.plist 文件中的 REVERSED_CLIENT_ID 键填写 URL 方案,其他字段保持原样
  • 添加第二个 URL 类型,这次使用“常规”选项卡中的应用程序包 ID

LSApplicationQueriesSchemes

  • 添加一个名为 LSApplicationQueriesSchemes 的新目标 属性。应该是数组类型。
  • 添加一项等于 REVERSED_CLIENT_ID 值
  • 再次使用您的应用程序包 ID 添加第二个项目

编译并享受。