試試下面的代碼工作....它可能將幫助你我的朋友......先在ViewDidLoad中編寫下面的代碼。
- (void)viewDidLoad
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *clientID = [defaults stringForKey:kGoogleClientIDKey];
NSString *clientSecret = [defaults stringForKey:kGoogleClientSecretKey];
GTMOAuth2Authentication *auth = nil;
if (clientID && clientSecret) {
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
}
// Save the authentication object, which holds the auth tokens and
// the scope string used to obtain the token. For Google services,
// the auth object also holds the user's email address.
self.auth = auth;
// Update the client ID value text fields to match the radio button selection
[self loadClientIDValues];
}
- (void)loadClientIDValues {
// Load the client ID and secret from the prefs into the text fields
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
mClientIDField.text = [defaults stringForKey:kGoogleClientIDKey];
mClientSecretField.text = [defaults stringForKey:kGoogleClientSecretKey];
}
然後在下面的代碼寫的Gmail登入動作......
-(IBAction)gmailLogin
{
[self saveClientIDValues];//this is the method for saving the credentials of gmail account of particular user.
if (![self isSignedIn]) {
// Sign in
[self signInToGoogle];
} else {
// Sign out
[self signOut];
}
}
- (void)saveClientIDValues {
// Save the client ID and secret from the text fields into the prefs
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *clientID = mClientIDField.text;
NSString *clientSecret = mClientSecretField.text;
[defaults setObject:clientID forKey:kGoogleClientIDKey];
[defaults setObject:clientSecret forKey:kGoogleClientSecretKey];
}
要檢查用戶是否logined與否!!!!
- (BOOL)isSignedIn {
BOOL isSignedIn = self.auth.canAuthorize;
return isSignedIn;
}
那麼如果用戶沒有登錄,所以做出登錄...
- (void)signInToGoogle {
[self signOut];
NSString *keychainItemName = nil;
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 = @"Your Id";
NSString *clientSecret = @"Your Secret";
if ([clientID length] == 0 || [clientSecret length] == 0) {
// NSString *msg = @"The sample code requires a valid client ID and client secret to sign in.";
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];
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;
NSDictionary *profile = viewController.signIn.userProfile;
NSLog(@"%@",profile);
// Optional: display some html briefly before the sign-in page loads
NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
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.
}
如果每一件事情是正確的,那麼你會得到導致本以下方法...
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error != nil) {
// Authentication failed (perhaps the user denied access, or closed the
// window before granting access)
NSLog(@"Authentication error: %@", error);
NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey
if ([responseData length] > 0) {
// show the body of the server's authentication failure response
NSString *str = [[[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", str);
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Eye News" message:@"Gmail Authentication Fail. Please try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
self.auth = nil;
}
else
{
viewController.signIn.shouldFetchGoogleUserProfile = YES;
NSDictionary *profile = viewController.signIn.userProfile;
NSLog(@"%@",profile);
}
}
讓我知道它工作與否!
快樂編碼!
controllerWithScope:這種方法不GTMOAuth2ViewControllerTouch – user3101600
你有一個名爲GTMOAuth2ViewControllerTouch.h和GTMOAuth2ViewControllerTouch.m這些類文件類???? – NiravPatel