Search code examples
iosios7google-plus

Google + sign in within app


I want to integrate Google+ sign in functionality to my app. I have used Google+ sdk , but it redirect to safari to sign in to google , I want to open that web dialog within my application. Can anybody help me on that thing?


Solution

  • I did it. By inheriting UIApplication class and then handling url in web view. Here is code:

    Add Google+ FrameWork to your project.

    Now, on authentication call, catch that URL by inhereting UIAPPLICATION Class.

    - (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
    

    Set this subclass as principal class in info.plist file.

    Now open URL to web view

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

    Now in 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);
    }
    

    Don't forget to register your class for ApplicationOpenGoogleAuthNotification notification.