Google Plus使用UIWebViewloginiOS

我正在使用Google Plus SDK在我的应用中使用Google +login。 当用户点击loginbutton时,用户被redirect到Safari浏览器。 (标准过程)。

不过,苹果似乎已经改变了这个规则。 我的应用程序因此被拒绝,说明如下

该应用程序启动移动Safari浏览器完成login到Google+之前返回到应用程序。 但是,使用不应该退出应用程序才能使用它。

要解决这个问题,修改您的应用程序以允许用户通过应用程序login,为您的应用程序创build另一个身份validation方法或从使用您的应用程序中删除此要求是适当的

是否有人面临同样的问题,并有解决scheme? 解决方法是使用UIWebView并使用OAuth访问令牌。 但是,这不能为我提供单一loginfunction。 也没有人知道如何使用访问令牌(没有SDKfunction)共享图像。 我已经find了SDK中的GTMOAuth2ViewControllerTouch文件,但仍然没有运气。 任何人都可以帮助我这个class?

更新

Google已经发布了一个新的支持WebViewlogin的iOS SDK ,以及其他一些更新和function。 (感谢John Hjelmstad和Co在这方面的工作)

原始post

我强烈build议不要使用过时的GTMAuth2Controller。 如果你真的想要这个function,请把你的声音添加到官方的Google线程 。 我们添加的声音越多,Google就会引入此function的可能性也越大。 目前,苹果正在拒绝所有迫使用户从Safari浏览器触发您的Google+login应用的应用。 即使苹果知道,这是谷歌SDK如何做到这一点(不要忘了,Facebook做了相同的事实,但不会让你的应用程序被拒绝)。

它的小而跛脚,但不幸的是,我们所能做的就是呼吁谷歌join对此的支持。

这是谷歌有关这个问题(我也抗议使用他们过时的控制器)

大家好 – 在谷歌,我们的目标是让我们的服务尽可能多的用户和开发人员。 我们已经收到了很less的这类问题的报告,但是这些应用程序开发商似乎最终通过苹果的标准stream程获得了批准启动。

同时,我们正在继续努力改进login机制。 我同意丹尼尔,不build议直接使用GTMOauth2ViewControllerTouch,或等效的UIWebView欺骗。 2014年10月16日

我尝试合并示例应用程序的Gmail API和GooglePlus使用UIWebViewlogin,而不是Safari,它似乎工作:

  1. 创build谷歌项目,但设置凭据时安装的应用程序typesselect其他
  2. 添加/启用Google+ API到您的谷歌项目
  3. 为Gmail项目添加/启用Gmail API
  4. 按照Google+的说明,并添加所有的框架,并在项目中拖动一个GoogleOpenSource.framework,GooglePlus.framework和GooglePlus.bundle(你可能不需要这个,因为我们没有使用SignInButton,以防万一)
  5. 添加其他链接器标志-all_load -ObjC,禁用ARC
  6. 将DRM GTMOAuth2ViewTouch.xib拖放到您的项目文件(来自OAuth2)

在这里输入图像说明

我的viewcontroller .h文件我添加了2个button来login和检索数据,textview显示检索到的数据,连接他们在build设者。 还为GTMOAuth2Authentication添加了接口和属性:

#import <UIKit/UIKit.h> #import <GoogleOpenSource/GoogleOpenSource.h> #import <GooglePlus/GooglePlus.h> @interface MyViewController : UIViewController{ IBOutlet UIButton *loginBttn; IBOutlet UIButton *retriveBttn; IBOutlet UITextView *profileTextView; GTMOAuth2Authentication *mAuth; } @property (retain, nonatomic) IBOutlet UIButton *loginBttn; @property (retain, nonatomic) IBOutlet UIButton *retriveBttn; @property (nonatomic, retain) IBOutlet UITextView *profileTextView; @property (nonatomic, retain) GTMOAuth2Authentication *auth; -(IBAction)dologin; -(IBAction)doretrive; @end 

我的viewcontroller .m文件:

 #import "MyViewController.h" #import "AppDelegate.h" #import <GoogleOpenSource/GoogleOpenSource.h> #import <GooglePlus/GooglePlus.h> static NSString *const kKeychainItemName = @"Google OAuth2 For gglplustest"; static NSString *const kClientID = @"your client id"; static NSString *const kClientSecret = @"your client secret"; @interface MyViewController () @end @implementation MyViewController @synthesize loginBttn,retriveBttn; @synthesize auth = mAuth; @synthesize profileTextView; -(IBAction)doretrive{ GTLServicePlus* plusService = [[[GTLServicePlus alloc] init] autorelease]; plusService.retryEnabled = YES; [plusService setAuthorizer:self.auth];//!!!here use our authentication object!!! GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"]; [plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error) { if (error) { GTMLoggerError(@"Error: %@", error); } else { // Retrieve the display name and "about me" text [person retain]; NSString *description = [NSString stringWithFormat: @"%@\n%@\n%@", person.displayName, person.aboutMe,person.emails]; [profileTextView setText:description]; } }]; } - (void)auth:(GTMOAuth2Authentication *)auth finishedRefreshWithFetcher:(GTMHTTPFetcher *)fetcher error:(NSError *)error { [self viewController:nil finishedWithAuth:auth error:error]; if (error != nil) { // Refresh failed NSLog(@"Authentication Error %@", error.localizedDescription); self.auth=nil; return; } self.auth=auth; } - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error { if (error != nil) { // Authentication failed NSLog(@"Authentication Error %@", error.localizedDescription); self.auth=nil; return; } self.auth=auth; [viewController release];//no ARC } -(IBAction)dologin{ NSString *scope = kGTLAuthScopePlusLogin;//Google+ scope GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kClientID clientSecret:kClientSecret]; if ([auth refreshToken] == nil) { GTMOAuth2ViewControllerTouch *authController; authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope clientID:kClientID clientSecret:kClientSecret keychainItemName:kKeychainItemName delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]; [[self navigationController] pushViewController:authController animated:YES]; }else{ [auth beginTokenFetchWithDelegate:self didFinishSelector:@selector(auth:finishedRefreshWithFetcher:error:)]; } } - (void)viewDidLoad { [super viewDidLoad]; } 

所以现在,当视图被加载时,它不会尝试login。

然后在一个视图,当我触摸loginBttn,将运行dologin方法(从GmailMail示例),我设置范围为GooglePlus:1st将检查是否有钥匙串中保存的信息,如果是,它会继续而不要求您login。 如果没有的话,它会做到这一点,直接在我的应用程序内的UIWebView的Gmaillogin。

在这里输入图像说明

这里如何看起来后login:

在这里输入图像说明

然后,我可以触摸retriveBttn运行检索方法(例如获取名称,关于和电子邮件),使用GooglePlus示例但是,我设置Authorizer到我login成功后login的validation码,如:[plusService setAuthorizer:self.auth];

这里是检索完成前后的视图:

在这里输入图像说明

在这里输入图像说明

似乎是在做什么是应该的。

要解决这个问题,你可以使用Google库 。 它内部有一个G + SDK,并有GTMAuth2ControllerTouch ,它可以让你login到所有的Google服务。 看看这里如何获得这个库, 这里是一个如何使用它的例子。 这很容易。 您也可以在YouTube上find一些video,其中介绍了如何安装此库。

您可以将此库用于Google+ SDK,但您需要做一些工作来合并这两个API。