Google + iPhone API登录并分享,无需离开应用 [英] Google + iPhone API sign in and share without leaving app

查看:113
本文介绍了Google + iPhone API登录并分享,无需离开应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的应用中集成了Google + API,这是一件轻而易举的事,我唯一的问题是,一切都要求你离开应用然后再回来(它使用URL方案)。这不是我想要的行为,有没有办法直接调用他们的服务,并像LinkedIn API一样做任何我想要的响应?。

I recently integrated the Google + API in my App, it was a breeze, my only problem with it, is that everything requires you to leave the app and then come back (it uses URL schemes for this). This is not the behavior I would like, is there a way to directly call their services and do whatever I want with the responses just like in LinkedIn API?.

我真的我想避免在safari和我的应用之间来回走动。任何建议/文件都表示赞赏。

I really want to avoid going back and forth between safari and my app. Any suggestions/documentation is appreciated.

谢谢,

奥斯卡

推荐答案

所以,这取决于你想做什么。

So, it depends what you want to do.

登录:这将始终呼叫另一个应用程序。如果Google+应用程序已安装,则会调出该应用程序,否则它将退回到Chrome和Safari。

Sign-In: this will always call out to another application. If the Google+ application is installed it will call out to that, else it will fall back to Chrome and Safari.

分享/互动帖子:现在,它始终使用Chrome或Mobile Safari。

Sharing/Interactive Posts: right now this always uses Chrome or Mobile Safari.

检索朋友,撰写应用活动,检索个人资料信息:所有这一切都是在登录后检索到的访问令牌完成的,因此不需要离开应用程序。

Retrieving friends, writing app activities, retrieving profile information: All this is done with the access token retrieved after sign in, so does not require leaving the application.

虽然不支持,但可以跳过SDK并弹出UIWebView,动态构建OAuth链接并将用户发送到该(看一下SDK附带的开源库中的GTMOAuth2ViewControllerTouch。下面是一个非常粗略的例子,您可以将其转换回GPPSignIn实例。

It is possible, though rather unsupported, to skip the SDK and pop up a UIWebView, construct the OAuth link dynamically and send the user to that (take a look at GTMOAuth2ViewControllerTouch in the open source libraries that ship with the SDK). Below is the a very rough example of the kind of thing you could do to plumb it back into the GPPSignIn instance.

但是,您可以保证用户必须输入用户名和密码(可能是第二个因素)。使用Google+应用,您几乎可以保证已经登录,并且使用Chrome / Safari路线,用户可能已经登录(特别是如果他们使用其他应用与Google+登录)。

However, you would be guaranteeing that the user has to enter their username and password (and maybe 2nd factor). With the Google+ app you're pretty much guaranteed to be already signed in, and with the Chrome/Safari route, there is a chance the user is already signed in (particularly if they're using other apps with Google+ Sign-In).

这也不涉及共享,所以我强烈建议尽可能使用现有的SDK。以您希望的方式提交功能请求也是一件好事: https://code.google.com/p/google-plus-platform/issues/list

This also doesn't address sharing, so I would strongly recommend using the existing SDK as far as possible. Filing a feature request for the way you would prefer it to work would be a good thing to do as well: https://code.google.com/p/google-plus-platform/issues/list

@interface ViewController() {
  GTMOAuth2ViewControllerTouch *controller;
}
@end;

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GPPSignIn *signIn = [GPPSignIn sharedInstance];
  signIn.clientID = @""; // YOUR CLIENT ID HERE.
  signIn.delegate = self;
}

- (IBAction)didTapSignIn:(id)sender {
  void (^handler)(id, id, id) =
      ^(GTMOAuth2ViewControllerTouch *viewController,
        GTMOAuth2Authentication *auth,
        NSError *error) {
        [self dismissViewControllerAnimated:YES completion:^{
            [controller release];
        }];
        if (error) {
          NSLog(@"%@", error);
          return;
        } else {
          BOOL signedIn = [[GPPSignIn sharedInstance] trySilentAuthentication];
          if(!signedIn) {
            NSLog(@"Sign In failed");
          }
        }
  };
  controller = [[GTMOAuth2ViewControllerTouch
      controllerWithScope:kGTLAuthScopePlusLogin
                 clientID:[GPPSignIn sharedInstance].clientID
             clientSecret:nil
         keychainItemName:[GPPSignIn sharedInstance].keychainName
        completionHandler:handler] retain];
  [self presentViewController:controller animated:YES completion:nil];
}

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
                   error:(NSError *)error {
  if (!error) {
    UIAlertView * al = [[UIAlertView alloc] initWithTitle:@"Authorised"
                                                   message:@"Authorised!"
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
    [al show];
    [al release];
  }
}

此代码的唯一真正技巧是它使用[GPPSignIn sharedInstance] .keychainName - 这意味着auth令牌存储在与GPPSignIn按钮相同的keychain条目中,这反过来意味着我们可以在填充后使用[[GPPSignIn sharedInstance] trySilentAuthentication],并保留基于回调的流程作为主库。

The only real trick to this code is that it uses the [GPPSignIn sharedInstance].keychainName - this means that the auth tokens get stored in the same keychain entry as the GPPSignIn button would, which in turn means we can use [[GPPSignIn sharedInstance] trySilentAuthentication] once it has been populated, and keep the same callback based flow as the main library.

这篇关于Google + iPhone API登录并分享,无需离开应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆