簡體   English   中英

Google + iPhone API登錄並分享,無需離開應用

[英]Google + iPhone API sign in and share without leaving app

我最近在我的應用程序中集成了Google + API,這是一件輕而易舉的事,我唯一的問題是,所有內容都要求您離開應用程序然后返回(它使用URL方案)。 這不是我想要的行為,有沒有辦法直接調用他們的服務,並像LinkedIn API一樣做任何我想要的響應?

我真的想避免在safari和我的app之間來回走動。 任何建議/文件表示贊賞。

謝謝,

奧斯卡

來自GOOGLE的更新

今天,我們發布了一個新的Google登錄iOS SDK,內置支持通過WebView登錄:developers.google.com/identity/sign-in/ios SDK支持發送到處理Sign的眾多Google應用中的任何一個在目前,隨着WebView后退。 在所有情況下,都避免使用Safari開關,我們認為這是避免應用拒絕的關鍵因素。 我們期待獲得使用新SDK的人們的反饋,並希望它的使用可以取代人們在此期間實施的(巧妙和勤奮)解決方案。


方法BELLOW不再需要

這個方法用一個自定義的UIWebView處理內部登錄這個工作並且由APPLE批准

我的應用程序因審查原因而被踢了

"The app opens a web page in mobile Safari for logging in to Google plus, 
then returns the user to the app. The user should be able log in without opening 
Safari first."

請參閱此鏈接https://code.google.com/p/google-plus-platform/issues/detail?id=900我通過以下步驟解決了這個問題

1)創建UIApplication的子類,它覆蓋openURL:

。H

#import <UIKit/UIKit.h>

#define ApplicationOpenGoogleAuthNotification @"ApplicationOpenGoogleAuthNotification"

@interface Application : UIApplication

@end

.M

#import "Application.h"

@implementation Application

- (BOOL)openURL:(NSURL*)url {

    if ([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"]) {

        return NO;

    } else if ([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"]) {

        [[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];
        return NO;

    }

    return [super openURL:url];
}

@end
  • 這基本上可以防止iOS上的Chrome打開任何內容
  • 我們捕獲auth調用並將其重定向到我們的內部UIWebView

2)到info.plist,添加Principal類,並為它應用程序(或任何你命名的類)

添加plist鍵“NSPrincipalClass”,並將值作為主應用程序的類(擴展UIApplication的類,在本例中為Application(參見上面的代碼))

3)捕獲通知並打開內部webview

當您的自定義Application類發送ApplicationOpenGoogleAuthNotification時,可以在某處(在AppDelegate中)監聽它,當您捕獲此通知時,打開UIWebView(使用通知傳遞的URL作為webview的URL)(在我的情況下,LoginViewController偵聽對於此通知,當收到時,它會打開一個視圖控制器,其中只包含一個連接到委托的webview)

4)在webview中

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if ([[[request URL] absoluteString] hasPrefix:@"com.XXX.XXX:/oauth2callback"]) {
        [GPPURLHandler handleURL:url sourceApplication:@"com.google.chrome.ios"n annotation:nil];

        // Looks like we did log in (onhand of the url), we are logged in, the Google APi handles the rest
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}
  • 或者用於處理響應的simmilar代碼
  • 來自上面代碼的com.XXX.XXX:/oauth2callback,替換為您的公司和應用程序標識符,例如“com.company.appname:/ oauth2callback”
  • 您可能希望使用@“com.apple.mobilesafari”作為sourceApplication參數

所以,這取決於你想做什么。

登錄 :這將始終呼叫另一個應用程序。 如果Google+應用程序已安裝,它將會調用它,否則它將退回到Chrome和Safari。

分享/互動帖子 :現在,它總是使用Chrome或Mobile Safari。

檢索朋友,編寫應用程序活動,檢索配置文件信息 :所有這些都是在登錄后檢索的訪問令牌完成的,因此不需要離開應用程序。

雖然相當不受支持,但可以跳過SDK並彈出UIWebView,動態構建OAuth鏈接並將用戶發送到該鏈接(請查看SDK附帶的開源庫中的GTMOAuth2ViewControllerTouch)。 下面是一個非常粗略的示例,您可以將其檢索回GPPSignIn實例。

但是,您可以保證用戶必須輸入用戶名和密碼(可能是第二個因素)。 使用Google+應用,您幾乎可以保證已經登錄,並且使用Chrome / Safari路線,用戶可能已經登錄(特別是如果他們使用其他應用與Google+登錄)。

這也不涉及共享,所以我強烈建議盡可能使用現有的SDK。 以您希望的方式提交功能請求也是一件好事: 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]一旦填充完畢,並保持與主庫相同的基於回調的流程。

如果未安裝Google Plus應用程序,@ PeterLapisu方法效果很好。 然后來自app的傳出網址前綴如下:

但是,如果安裝了Google App,則會有一個外發網址,前綴列表如下所示:

因此,如果安裝了谷歌應用程序,它將與我們的包含webview的應用程序UIViewController同時啟動。然后,如果用戶成功登錄谷歌應用程序,他將被定向回我們的應用程序,ViewController將可見。

為了防止這種情況,應該允許Google App登錄用戶並將其引導回我們的應用程序。 根據此討論: https//code.google.com/p/google-plus-platform/issues/detail?id = 900,Apple允許這樣做。

所以在我的實現中,首先我要檢查是否安裝了Google App:

- (BOOL)openURL:(NSURL*)url {

NSURL *googlePlusURL = [[NSURL alloc] initWithString:@"gplus://plus.google.com/"];

BOOL hasGPPlusAppInstalled = [[UIApplication sharedApplication] canOpenURL:googlePlusURL];


if(!hasGPPlusAppInstalled)
{
    if ([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"]) {

        return NO;

    } else if ([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"]) {

        [[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];
        return NO;

    }
}


return [super openURL:url];
}

編輯:

現在我可以確認我的應用程序最終獲得了此解決方案的批准。

我希望它會對某人有所幫助。 它合並了Google+和Gmail示例,完全避免使用Google SignIn Button,即您不會離開應用。

將Google +和Gmail API添加到Google項目中,在您的應用中登錄google,就像使用來自OAuth2的GTMOAuth2ViewControllerTouch.xib gmail並將范圍設置為Google+:

-(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:)];
    }
}

如果成功登錄,則保留身份驗證對象,然后在使用google plus服務時使用該身份驗證對象:

GTLServicePlus* plusService = [[[GTLServicePlus alloc] init] autorelease];
[plusService setAuthorizer:self.auth];//!!!here use our authentication object!!!

不需要GPPSignIn。

完整的文章在這里: 這是另一種解決方案

使用(新) Google登錄iOS SDK

當沒有Google應用程序來完成登錄過程時,SDK本身支持通過WebView登錄。 它還支持為此目的分發幾個Google應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM