---
title: "iOS GoogleSignIn接入"
author: "贝壳水母"
date: "2015-11-28T10:44:05Z"
tags: []
categories: ["移动开发"]
slug: "ios-googlesignin"
draft: false
lastmod: "2015-12-03T16:04:34Z"
id: 2259910
url: "https://shineonly.iteye.com/blog/2259910"
---

<https://developers.google.com/identity/sign-in/ios/start-integrating>
Google官方文档在这里，以下主要介绍不使用CocoaPods而进行手动配置需要留意的地方，以及怎么使用自定义的按钮

1.导入需要的资源和框架

下载玩SDK包后，会得到
·GoogleSignIn.framework
·GoogleSignIn.bundle
其中GoogleSignIn.bundle是google登录按钮的资源包，如果自己的应用里已经做了自定义的登录按钮，这个是用不到的，可以不加。

添加必须的依赖框架：
·AddressBook.framework
·StoreKit.framework
·SystemConfiguration.framework
还有一些框架没有列出来但可能会用到，比如遇到过一个报错

报错：

```Console
_CTFontManagerRegisterGraphicsFont Undefined symbols for architecture x86_64
```

解决：加CoreText.framework


2.项目配置

文档里提到的configuration file，在仅使用googleSignIn的时候不是必须的，而且后台下载对中文命名是拒绝的……（sad……）
所谓的GoogleService-Info.plist，其实作用只是在初始化的时候配置clientID，所以我们可以手动在自己target的Info.plist里加入一个，比如我就起名叫G_CLIENT_ID，甚至你在代码里hardcode都可以……
![](http://dl2.iteye.com/upload/attachment/0113/3863/28efced1-f124-3006-9549-514f5beb3d47.png)


接着加URL Types，里面白纸黑字加截图，只填了Url Schemes的值
![](http://dl2.iteye.com/upload/attachment/0113/3865/a423e625-2082-301e-8d72-38a99d462fc7.png)

但其实Identifier也要填上的，这个可以在SDK附带的例子里可以看到
![](http://dl2.iteye.com/upload/attachment/0113/3867/edf50744-aff9-3d3b-bf1e-1e1f51f917e2.png)

3.编码
参考这里：<https://developers.google.com/identity/sign-in/ios/sign-in>

文档中建议是把view controller也作为GIDSignInUIDelegate，这个是最简单的做法，但有时候我们自己关于googleSignIn，可能会有专门一个管理的类，mvc里叫controller吧，而且想把相关的代码都集中在这里，那么就需要曲折一点，实现：

```Objc
signInWillDispatch:error:
signIn:presentViewController:
signIn:dismissViewController:

```
3个方法，
重点是 signIn:presentViewController: 和
signIn:dismissViewController:
举个例子，如果是cocos2d-x开发的话，可以在AppController里提供一个RootViewController的getter，然后在 signIn:presentViewController: 中，

```Objc
RootViewController* ctrl = (RootViewController*)[appcontroller getrootviewctrl];
[ctrl presentviewcontroller:viewcontroller animated:yes completion:nil];

```

signIn:dismissViewController:则是

```Objc
RootViewController* ctrl = (RootViewController*)[appcontroller getrootviewctrl];
[ctrl dismissviewcontrolleranimated:yes completion:nil];

```


初始化，参考例子可以这样：

```Objc
NSString *clientID = @"";
clientID = [[[nsbundle mainbundle] infoDictionary] objectForKey:@"G_CLIENT_ID"];
NSLog(@"clientID = %@", clientID);
GIDSignIn* signIn = [gidsignin sharedinstance];
signIn.clientID = clientID;
signIn.shouldFetchBasicProfile = YES;
signIn.delegate = self;
signIn.uiDelegate = self;
```


登录的调用则最为简单，不需要导入GIDSignInButton什么的，在自己按钮的事件回调里直接：

```Objc
[[gidsignin sharedinstance] signIn];
```

登出则是：

```Objc
[[gidsignin sharedinstance] signOut];
```


以上是接入的关键以及文档可能没写好的地方，另外附上的是可能遇到的问题及解决方案

======================
报错：

```console
-[__nsdictionarym gtm_httpargumentsstring]: unrecognized selector sent to instance 0x1e887ea0'
```

解决：Other Linker Flags: -ObjC

======================
报错：

```console
’When |allowsSignInWithWebView| is enabled, uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.
```

解决：implement GIDSignInUIDelegate

======================
报错：

```console
LaunchServices: ERROR: There is no registered handler for URL scheme com-google-gidconsent-google
LaunchServices: ERROR: There is no registered handler for URL scheme com-google-gidconsent
LaunchServices: ERROR: There is no registered handler for URL scheme com.google.gppconsent.2.4.1
LaunchServices: ERROR: There is no registered handler for URL scheme com.google.gppconsent.2.4.0

```
解决：
无视，千万不要处理，如果把这几个加到了URL Type，会导致iOS9以下无法弹出web登录页面