(void)signInToGoogle {
[self signOut];
NSString *keychainItemName = nil;
if ([self shouldSaveInKeychain]) {
keychainItemName = kKeychainItemName;
}
// For Google APIs, the scope strings are available
// in the service constant header files.
NSString *scope = @"https://www.googleapis.com/auth/plus.me";
// Typically, applications will hardcode the client ID and client secret
// strings into the source code; they should not be user-editable or visible.
//
// But for this sample code, they are editable.
NSString *clientID = self.clientIDField.text;
NSString *clientSecret = self.clientSecretField.text;
if ([clientID length] == 0 || [clientSecret length] == 0) {
NSString *msg = @"The sample code requires a valid client ID and client secret to sign in.";
[self displayAlertWithMessage:msg];
return;
}
// Note:
// GTMOAuth2ViewControllerTouch is not designed to be reused. Make a new
// one each time you are going to show it.
// Display the autentication view.
SEL finishedSel = @selector(viewController:finishedWithAuth:error:);
GTMOAuth2ViewControllerTouch *viewController;
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:keychainItemName
delegate:self
finishedSelector:finishedSel];
// You can set the title of the navigationItem of the controller here, if you
// want.
// If the keychainItemName is not nil, the user's authorization information
// will be saved to the keychain. By default, it saves with accessibility
// kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, but that may be
// customized here. For example,
//
// viewController.keychainItemAccessibility = kSecAttrAccessibleAlways;
// During display of the sign-in window, loss and regain of network
// connectivity will be reported with the notifications
// kGTMOAuth2NetworkLost/kGTMOAuth2NetworkFound
//
// See the method signInNetworkLostOrFound: for an example of handling
// the notification.
// Optional: Google servers allow specification of the sign-in display
// language as an additional "hl" parameter to the authorization URL,
// using BCP 47 language codes.
//
// For this sample, we'll force English as the display language.
NSDictionary *params = [NSDictionary dictionaryWithObject:@"en"
forKey:@"hl"];
viewController.signIn.additionalAuthorizationParameters = params;
// By default, the controller will fetch the user's email, but not the rest of
// the user's profile. The full profile can be requested from Google's server
// by setting this property before sign-in:
//
// viewController.signIn.shouldFetchGoogleUserProfile = YES;
//
// The profile will be available after sign-in as
//
// NSDictionary *profile = viewController.signIn.userProfile;
// Optional: display some html briefly before the sign-in page loads
NSString *html = @"
Loading sign-in page...
";
viewController.initialHTMLString = html;
[[self navigationController] pushViewController:viewController animated:YES];
// The view controller will be popped before signing in has completed, as
// there are some additional fetches done by the sign-in controller.
// The kGTMOAuth2UserSignedIn notification will be posted to indicate
// that the view has been popped and those additional fetches have begun.
// It may be useful to display a temporary UI when kGTMOAuth2UserSignedIn is
// posted, just until the finished selector is invoked.
}