2014-10-09 4 views
0

Я хочу интегрировать функцию входа в Google+ для своего приложения. Я использовал Google+ sdk, но перенаправляю сафари для входа в Google, я хочу открыть этот веб-диалог в своем приложении. Может ли кто-нибудь помочь мне в этом?Google + вход в приложение

+0

удалить схему URL в plist –

+0

@ Anbu.Karthik не работает ... он перенаправится на goole +, но не вернется в наше приложение, удалив схему URL – vivek

+0

Это не поддерживается SDK. – Steve

ответ

1

Я сделал это. Наследуя класс UIApplication, а затем обрабатывая URL-адрес в веб-представлении. Вот код:

Добавить Google+ FrameWork в свой проект.

Теперь, при вызове аутентификации, поймайте этот URL, вставив класс UIAPPLICATION.

- (IBAction)btnGooglePlusTap:(id)sender 
{ 
    [[GPPSignIn sharedInstance] authenticate]; 
} 

SubClassUIApplication.h 

#import <UIKit/UIKit.h> 

#define ApplicationOpenGoogleAuthNotification @"GoogleFriendInvitationPosted" 

@interface SubClassUIApplication : UIApplication 

@end 


#import "SubClassUIApplication.h" 

@implementation SubClassUIApplication 

- (BOOL)openURL:(NSURL *)url 
{ 
    if([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"]) 
    { 
     return NO;//This will prevent call to Google Chrome App if installed. 
    } 
    else if([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"]) 
    { 
     [[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url]; 

     return NO;// Here we will pass URL to notification and from notification observer , we will load web view. 
    } 
    else if ([[url absoluteString] hasPrefix:@"com.google"]) 
    { 
     return NO; 
    } 

    return [super openURL:url]; 
} 

@end 

Задайте этот подкласс как основной класс в файле info.plist.

Теперь откройте URL для веб-просмотра

- (void)catchNotificationforGooglePlusSharing:(NSNotification *)notiofication 
{ 
    NSURL *u=(NSURL *)notiofication.object; 

    UINavigationController *navWebView =(UINavigationController *) [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewNavController"]; 

    WebViewController *vcWebView = navWebView.viewControllers[0]; 
    vcWebView.webURL = u; 
    [self.navigationController presentViewController:navWebView animated:YES completion:Nil]; 
} 

Сейчас в webviewcontroller.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSURLRequest *req= [[NSURLRequest alloc]initWithURL:webURL]; 

    [webvGoogle loadRequest:req]; 
} 
#pragma mark - UIWebViewDelegate method 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    BOOL canHandle = [GPPURLHandler handleURL:request.URL sourceApplication:@"com.apple.mobilesafari" annotation:nil]; 

    if(canHandle == YES) 
    { 


     [self dismissViewControllerAnimated:YES completion:^ 
     { 

     }]; 
    } 

    return YES; 
} 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
    NSLog(@"Error in loading : %@",error.description); 
} 

Не забудьте зарегистрировать свой класс для уведомления ApplicationOpenGoogleAuthNotification.

+0

можете ли вы поделиться кодом здесь –

+0

хорошо да..Я поделюсь завтра – vivek

+0

Без обмена кодом, просто примите свой ответ, не очень хорошо. – Dharmik

1

Вы можете использовать GTMOAuth2ViewControllerTouch от Google Toolbox for Mac - OAuth 2 Controllers.

#import <GTMOAuth2ViewControllerTouch.h> 

GTMOAuth2ViewControllerTouch *googleAuthViewController = [[GTMOAuth2ViewControllerTouch alloc] 
         initWithScope:@"https://www.googleapis.com/auth/userinfo#email" 
         clientID:kClientId 
         clientSecret:kSecretId 
         keychainItemName:@"GooglePlus_Sample_App" 
         delegate:self 
         finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 

[self.navigationController pushViewController:googleAuthViewController animated:YES]; 

Это образец здесь http://blog.krzyzanowskim.com/2015/02/22/google-sign-on-with-1password/, где вы можете найти полный образец проекта.

+0

У меня есть авторизация с моей стороны из веб-браузера, такая же концепция, как это ... и она работает как шарм, а также помогает мне принять решение о том, хочу ли я открыть какой-либо URL-адрес в приложении или в сафари. Но спасибо за ваш ответ. – vivek

Смежные вопросы