我想获取用户在我的应用中使用Google+登录的所有信息。
-(void)googleSignInButtonMethod{
[[UIApplication sharedApplication] delegate];
signIn = [GPPSignIn sharedInstance];
signIn.clientID = kClientId;
[signIn authenticate];
signIn.shouldFetchGoogleUserID=YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.scopes = @[ kGTLAuthScopePlusLogin ];
signIn.delegate = self;
[signIn authenticate];
}
之后,
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
NSLog(@"Received Error %@ and auth object==%@",error,auth);
if(!error) {
NSLog(@"%@", signIn.authentication.userEmail);
}
}
我按照谷歌+开发者网站提供的所有说明进行操作。我设置了所有代理并导入了所有重要文件。
答案 0 :(得分:1)
在.h文件中
@property (weak, nonatomic) IBOutlet GIDSignInButton *gmailSignInButton;
-(IBAction)gmailBtn:(GIDSignIn *)sender;
在.m文件中
- (IBAction)gmailBtn:(GIDSignIn *)sender {
[[AFNetworkReachabilityManager sharedManager]startMonitoring];
//Checking the Internet connection...
[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
[GIDSignIn sharedInstance].uiDelegate = self;
[[GIDSignIn sharedInstance] signIn];
}else{
NSLog(@"No Network");
}
}];
}
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
// Perform any operations on signed in user here.
NSLog(@"user ID: %@",user.userID);
NSLog(@"user Token: %@",user.authentication.idToken);
NSLog(@"user name: %@",user.profile.name);
NSLog(@"user givenName: %@",user.profile.givenName);
NSLog(@"user family Name: %@",user.profile.familyName);
NSLog(@"user email: %@",user.profile.email);
NSURL *profileURL;
if (user.profile.hasImage)
{
profileURL = [user.profile imageURLWithDimension:100];
}
NSMutableDictionary *userData = [NSMutableDictionary dictionary];
[userData setValue:user.userID forKey:@"id"];
[userData setValue:user.profile.givenName forKey:@"first_name"];
[userData setValue:user.profile.familyName forKey:@"last_name"];
[userData setValue:@"" forKey:@"gender"];
[userData setValue:user.profile.email forKey:@"email"];
[userData setValue:profileURL forKey:@"profileImg"];
}
- (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error{
// [self presentViewController:viewController animated:YES completion:nil];
}
- (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
[self presentViewController:viewController animated:YES completion:nil];
}
// Dismiss the "Sign in with Google" view
- (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
*别忘了申报gmail委托GIDSignInUIDelegate,GIDSignInDelegate