尝试使用Google登录时出现“必须为| GIDSignIn |指定| clientID |”错误。

17
谷歌登录在使用Xcode 7时工作正常。更新到Xcode 8后,我开始收到错误消息:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You must specify |clientID| for |GIDSignIn|'。我有一个包含CLIENT_ID的GoogleService-Info.plist文件。
我通过添加以下行来解决它:
GIDSignIn.sharedInstance().clientID = "<CLIENT_ID>"

看起来 CLIENT_ID 没有从 GoogleService-Info.plist 中获取。我已经确认它在复制的捆绑资源中。

enter image description here

我不应该在代码中指定客户端id。 我如何修复代码以从GoogleService-Info.plist文件获取信息?
8个回答

20

你可以这样做。

Swift:

GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID

Objective-C

GIDSignIn.sharedInstance.clientID = FIRApp.defaultApp.options.clientID;

3
在 FirebaseApp.configure() 后在 AppDelegate 文件中编写此内容。 - hussain

8
请确保您按顺序放置这些代码。
FirebaseApp.configure()

GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self

FirebaseApp.app()?.options.clientID只有在调用FirebaseApp.configure()之后才能从GoogleService-Info.plist获取数据。


1
我遇到了同样的问题。实际上,在我的情况下已经更新了GoogleServices-Info.plist。 我重新下载了GoogleServices-Info.plist,并将其更新为旧版本,这解决了我的问题。

1
我通过向AppDelegate类添加遗漏的代码来解决它:
#import "AppDelegate.h"
#import <Google/SignIn.h>    

@interface AppDelegate ()<GIDSignInDelegate>

@end

@implementation AppDelegate

#pragma mark - UIApplicationDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Google sign-in setup
    NSError* configureError;
    [[GGLContext sharedInstance] configureWithError: &configureError];
    if (configureError) {
        NSLog(@"Error configuring Google services: %@", configureError.localizedDescription);
    }

    [GIDSignIn sharedInstance].delegate = self;

    return YES;
}


#pragma mark - GIDSignInDelegate

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
    //add your code here
}

- (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error {
    //add your code here
}

@end

0

如果您在这里是因为您正在使用这个Unity资产,并且在iOS上崩溃但在Android上没有:请确保在尝试登录之前调用LCGoogleLoginBridge.InitWithClientID()


0

如果您是使用Xamarin在iOS中从Google SignIn登录的,那么似乎需要在共享的SignIn实例上特别设置客户端ID,以便其余过程正常工作。我有一个用于管理iOS Google身份验证回调的类,在其中执行以下代码:

SignIn.SharedInstance.ClientID = "[Client Id Here].apps.googleusercontent.com"; 
SignIn.SharedInstance.Delegate = this;
SignIn.SharedInstance.UIDelegate = this;
SignIn.SharedInstance.SignInUser();

我建议将此客户端ID显然放置在某个配置设置中,尽管我原以为它会从GoogleServices-Info.plist中获取客户端ID,但实际上并没有。

我还将GoogleServices-Info.plist放在我的“资源”文件夹中,该文件夹设置为BundledResource进行构建,并且我从Google开发者控制台为我的应用程序下载了该文件,然后将其重命名为“GoogleServices-Info.plist”,以替换其过长的名称。最后,值得注意的是,在iOS Bundle Signing页面下的Custom Entitlements中特别设置“Entitlements.plist”设置,这可以让您避免不可避免地出现的Key Chain错误。不要忘记在Entitlements本身中启用Keychain。


0

尽管我已经做了一切,但我的应用程序在点击 GIDSignInButton 时仍然崩溃。在我的情况下,我将设置 clientID 的代码从 Appdelegate 移动到了 GIDSignInButton 所在的位置(例如 LoginViewController)。

public override func viewDidLoad() {
    super.viewDidLoad()
    configureViews()
    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().clientID = "Your_ClientID"

}

0

Swift 3 在AppDelegate中添加:

var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \
(String(describing: configureError))")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接