使用GIDSignIn处理使用其他Google应用程序login时,不能获取Google用户

我正在使用谷歌login为iOS和使用模拟器时,它工作正常,因为没有谷歌应用程序安装和用户提取,但是当使用我的iPhone 6设备打开youtube(与他们内的一些注册帐户)处理login。之后,当回来的应用程序代码不input此function:

-(void)signIn:(GIDSignIn *) signIn didSignInForUser:(GIDGoogleUser *) user withError:(NSError *) error 

任何人都可以帮助我,我不能使用另一个functionlogin,我必须调用[[GIDSignIn sharedIstance] signIn] ,这个function检测是否安装了另一个谷歌应用程序,并自动打开另一个谷歌应用程序或[[GIDSignIn sharedIstance] signIn]

 import UIKit import GoogleSignIn import Google class ViewController: UIViewController,GIDSignInUIDelegate, GIDSignInDelegate { override func viewDidLoad() { super.viewDidLoad() let gidSingIn = GIDSignIn() GIDSignIn.sharedInstance().uiDelegate = self gidSingIn.delegate = self GIDSignIn.sharedInstance().delegate = self var configureError:NSError? GGLContext.sharedInstance().configureWithError(&configureError) assert(configureError == nil, "Error configuring Google services: \(configureError)") let button = GIDSignInButton(frame:CGRectMake(0,0,30, 200)) button.center = self.view.center button.backgroundColor = UIColor.blueColor() self.view.addSubview(button) // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func signInGoogle(sender: AnyObject) { print("pressed") } func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { if (error == nil) { // Perform any operations on signed in user here. print(user.userID) // For client-side use only! print(user.authentication.idToken) // Safe to send to the server print(user.profile.name) print(user.profile.givenName) print(user.profile.familyName) print(user.profile.email) print(user.authentication.accessToken) print(user.profile) } else { print("\(error.localizedDescription)") } } func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!, withError error: NSError!) { } 

} //这是用gmail帐号login的,不适用于googleplus。 只需复制并通过您的控制器。 并在AppDelegate类中添加以下func

 func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication!,UIApplicationOpenURLOptionsAnnotationKey: annotation] return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation) } 

我通过正确设置GIDSignIn实例的一些属性解决了这个问题。 例如:

 GIDSignIn*sigNIn=[GIDSignIn sharedInstance]; [sigNIn setDelegate:self]; [sigNIn setUiDelegate:self]; sigNIn.shouldFetchBasicProfile = YES; sigNIn.allowsSignInWithBrowser = NO; sigNIn.allowsSignInWithWebView = YES; sigNIn.scopes = @[@"https://www.googleapis.com/auth/plus.login",@"https://www.googleapis.com/auth/userinfo.email",@"https://www.googleapis.com/auth/userinfo.profile"]; sigNIn.clientID =@"xxxxxxxxxxxxxxxxxxxxxxxgai.apps.googleusercontent.com"; [sigNIn signIn]; 

我假设你正在使用GIDSignin和你自己的服务器,这需要你在你的GIDSignin中包含一个serverclientID。 这将迫使我的应用程序尝试使用YouTube或谷歌加login,而不是打开一个网页视图甚至浏览器。 这将返回一个GIDSigninError = -1“一个潜在的可恢复的错误..”,并不会允许用户login。

我解决这个问题的方法是在通过覆盖UIApplication的canOpenURL函数打开它们之前,阻止来自Google或YouTube的URL。 我通过inheritanceUIApplication并实现canOpenURL这样做:

 @interface MyApp : UIApplication - (BOOL)canOpenURL:(NSURL *)url; @end @implementation MyApp - (BOOL)canOpenURL:(NSURL *)url { if ([[url scheme] hasPrefix:@"com-google-gidconsent"] || [[url scheme] hasPrefix:@"com.google.gppconsent"]) { return NO; } return [super canOpenURL:url]; } @end int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, NSStringFromClass([MyApp class]), NSStringFromClass([AppDelegate class])); } } 

请注意,通常在argv之后会有一个nil,但是这是放置UIApplication的子类的地方。 这也是你如何使用你自己的AppDelegate的子类。

另一种解决scheme是在UIApplication上创build一个覆盖canOpenURL的类别,并使用swizzling在自定义canOpenURL中调用原始实现。 这是一个很好的文章: https ://blog.newrelic.com/2014/04/16/right-way-to-swizzle/

但是,我必须警告你,这两个解决scheme都是黑客行为,你必须非常小心这样做的副作用。 我甚至不知道苹果可以这样做。

这个问题对我来说是这样的

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!)

已经不存在了,改为使用

public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!)

请注意差异didSignInForUserdidSignInFor 。 很可能在更新SDK到swift 3之后。