Google + iPhone API sign in and share without leaving app

UPDATE FROM GOOGLE

today, we released a new Google Sign In iOS SDK with full built-in
support for Sign In via WebView:
developers.google.com/identity/sign-in/ios The SDK supports dispatch
to any of a number of Google apps handling Sign In when present, with
WebView fallback after. In all cases, the Safari switch is avoided,
which we’ve seen to be the key element in avoiding app rejection.
We’re looking forward to getting feedback from people using the new
SDK, and hope its use can replace the (ingenious and diligent)
workarounds people have implemented in the meantime.


THE METHOD BELLOW IS NO LONGER NEEDED

THIS METHOD HANDLES THE LOGIN INTERNAL WITH A CUSTOM UIWebView
THIS WORKS AND WAS APPROVED BY APPLE

My app got kicked from review cause of this

"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."

See this link https://code.google.com/p/google-plus-platform/issues/detail?id=900
I did solved it by following steps

1) create a subclass of UIApplication, which overrides 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
  • this will basically prevent anything to be opened from Chrome on iOS
  • we catch the auth call and redirect it to our internal UIWebView

2) to info.plist, add the Principal class, and for it Application (or whatever you named the class)

Add plist key “NSPrincipalClass” and as the value the class of your main application (class which extends UIApplication, in this case Application (see code above))

3) catch the notification and open an internal webview

When your custom Application class sends ApplicationOpenGoogleAuthNotification, listen for it somewhere (in the AppDelegate maybe) and when you catch this notification, open a UIWebView (use the URL passed by the notification as the url for the webview) (in my case the LoginViewController listens for this notification and when received, it opens a view controller containing only a webview hooked up to delegate)

4) inside the 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;
}
  • or simmilar code, that handles the response
  • com.XXX.XXX:/oauth2callback from code above, replace with your company and app identifier, like “com.company.appname:/oauth2callback”
  • you might want to use @”com.apple.mobilesafari” as sourceApplication parameter

Leave a Comment