user2419998
user2419998

Reputation: 131

Example for Gmail Integration in ios

I am doing an app in which , i have a button called Login, when the user click on the button the gmail login page should be displayed, once the user provides his credentials, instead of logging in to the mail home page it should call the registration page of the app with the details filled that are fetched from the gmail profile of the user.. (Details like user first and last name, email so on).... Search provided me below site "https://code.google.com/p/gtm-oauth/wiki/GTMOAuthIntroduction" But i need an example to know how exactly the gmail integration works... Thanks in advance

Upvotes: 1

Views: 3932

Answers (1)

Shaik Riyaz
Shaik Riyaz

Reputation: 11462

Finally i found the solution . . . .i think this will help

Follow the below steps to integrate gmail with your application .

1.Add following classes to you project .

GTMHTTPFetcher.h , GTMHTTPFetcher.m

GTMOAuth2Authentication.h, GTMOAuth2Authentication.m

GTMOAuth2SignIn.h,GTMOAuth2SignIn.m

GTMOAuth2ViewControllerTouch.h,GTMOAuth2ViewControllerTouch.m,

GTMOAuth2ViewTouch.xib

SBJSON.h , SBJSON.m

you will get these classes here : https://github.com/jonmountjoy/Force.com-iOS-oAuth-2.0-Example

Note : if you are working under ARC Environment then you have to disable the ARC for following files :

GTMHTTPFetcher.m , GTMOAuth2Authentication.m , GTMOAuth2SignIn.m, GTMOAuth2ViewControllerTouch.m

To disable ARC for source files in Xcode 4, select the project and the target in Xcode. Under the target "Build Phases" tab, expand the Compile Sources build phase, select the library source files, then press Enter to open an edit field, and type -fno-objc-arc as the compiler flag for those files.

  1. add the following frameworks

    security.framework , systemConfiguration.framework

  2. Register your app to google api console …. here : https://code.google.com/apis/console

    Then go to ApiAccess section , create client id for iOS app . then you will get clientID, ClientSecret and RedirectUrl

  3. Now it's time for coding . . . .create a signIn button in your controller and set the action for that . Here when the user click the button SignInGoogleButtonClicked method gets called

code

 #define GoogleAuthURL   @"https://accounts.google.com/o/oauth2/auth"
 #define GoogleTokenURL  @"https://accounts.google.com/o/oauth2/token"
 #define GoogleClientID @"paste your client id"
 #define GoogleClientSecret @"paste your client secret"

-(void) SignInGoogleButtonClicked{

    NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL];

    NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob";

    GTMOAuth2Authentication * auth= [GTMOAuth2Authentication    authenticationWithServiceProvider:@"google"
  tokenURL:tokenURL
  redirectURI:redirectURI
  clientID:GoogleClientID
  clientSecret:GoogleClientSecret];

    auth.scope = @"https://www.googleapis.com/auth/plus.me";

    GTMOAuth2ViewControllerTouch * viewcontroller = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth
authorizationURL:[NSURL URLWithString:GoogleAuthURL] 
keychainItemName:@"GoogleKeychainName" delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [self.navigationController pushViewController:viewcontroller animated:YES];
    }

//this method is called when authentication finished
- (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController finishedWithAuth:(GTMOAuth2Authentication * )auth error:(NSError * )error{
    if (error != nil){
                     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error Authorizing with Google" message:[error localizedDescription]
                                delegate:nil
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil];
                    [alert show]
     }
    else{            
                     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert !" message:@"success"
                                delegate:nil
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil];
[alert show]

    }
}

Upvotes: 1

Related Questions