Google 和 Fb 在同一个应用中登录

Posted

技术标签:

【中文标题】Google 和 Fb 在同一个应用中登录【英文标题】:Google and Fb sign-in in same app 【发布时间】:2015-12-02 23:18:45 【问题描述】:

我正在尝试将 fb 和新的 google 登录功能集成到一个应用中,但遇到了某些问题,有人可以帮忙吗?

实际上我在 AppDelegate.m 中写了这个。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    NSError* configureError;
    [[GGLContext sharedInstance] configureWithError: &configureError];
    NSAssert(!configureError, @"Error configuring Google services: %@", configureError);

    [GIDSignIn sharedInstance].delegate = self;

    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                    didFinishLaunchingWithOptions:launchOptions];


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 


    return [[FBSDKApplicationDelegate sharedInstance] application:application                                                         openURL:url                                              sourceApplication:sourceApplication                                                       annotation:annotation] && [[GIDSignIn sharedInstance] handleURL:url
                                     sourceApplication:sourceApplication
                                            annotation:annotation];

然后我在另一个类中创建谷歌登录按钮

- (void)viewDidLoad 
    [super viewDidLoad];

    fbLogingButton = [[FBSDKLoginButton alloc]init];
    fbLogingButton.delegate = self;
    fbLogingButton.center = self.view.center;
    [self.view addSubview:fbLogingButton];
    fbLogingButton.readPermissions =
    @[@"public_profile", @"email", @"user_friends"];

    googleSignInButton = [[GIDSignInButton alloc]initWithFrame:CGRectMake(120, 370,50, 150)];
    [self.view addSubview:googleSignInButton];


    [GIDSignIn sharedInstance].uiDelegate = self;

    // Do any additional setup after loading the view, typically from a nib.

但应用程序在启动时崩溃

[[GGLContext sharedInstance] configureWithError: &configureError];

如果我评论说如果我点击登录按钮就会崩溃

崩溃日志显示

2015-09-07 17:10:38.272 DemoAppSep[62471:3858047] 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“您 必须指定 |clientID|对于 |GIDSignIn|' 第一次抛出调用堆栈:( 0 CoreFoundation 0x00000001042fbc65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000103f92bb7 objc_exception_throw + 45 2 核心基础 0x00000001042fbb9d +[NSException raise:format:] + 205 3 DemoAppSep 0x00000001017581ef -[GIDSignIn assertValidParameters] + 77 4 DemoAppSep 0x000000010175a589 -[GIDSignIn signInWithOptions:] + 52 5 DemoAppSep 0x000000010175e5c6 -[按下 GIDSignInButton] + 331 6 UIKit 0x00000001046f6da2 -[UIApplication sendAction:to:from:forEvent:] +

【问题讨论】:

错误显示您没有为 google 登录分配 clientID:developers.google.com/+/mobile/ios/sign-in 【参考方案1】:

将此用于 Facebook https://developers.facebook.com/docs/ios/getting-started

将此用于 GooglePlus https://developers.google.com/+/mobile/ios/getting-started

必须遵循所有步骤

最后在 AppDelegate.m

- (BOOL) application:(UIApplication *) application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
BOOL wasHandled=false;;
if ([url.scheme hasPrefix:@"fb"]) 

      wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
    //Facebook callback

else       //Google Plus callback

  wasHandled=  [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];



NSLog ( @"application openURL");
NSLog ( @"URL = %@", url);
NSLog ( @"Application = %@", sourceApplication);

return wasHandled;

它会工作的。谢谢你

【讨论】:

以上是关于Google 和 Fb 在同一个应用中登录的主要内容,如果未能解决你的问题,请参考以下文章

是否可以在同一个 iOS 应用中同时支持 Facebook 登录和 Google Plus 登录?

谷歌钱包应用内付款:未捕获的 ReferenceError:未定义 goog。 google.payments 与 goog.payments

Google+ 登录无法正常工作

尝试在我的网络应用上使用 Google 和登录时,不断收到“超出未经身份验证使用的每日限制。继续使用需要注册”

Google登录和支付

安装了 FB 应用程序的 SwiftUI 自定义 Facebook 登录