如何在iOS中从Google +获取个人资料图片?
问题描述:
目前在我的应用程序中,我从Google+获取电子邮件,GoogleID,用户名,性别。如何在iOS中从Google +获取个人资料图片?
这是我在viewDidLoad中代码:
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.shouldFetchGooglePlusUser = YES;
signIn.clientID = kClientId;
signIn.scopes = [NSArray arrayWithObjects:
kGTLAuthScopePlusMe,
kGTLAuthScopePlusUserinfoEmail,
kGTLAuthScopePlusLogin ,
nil];
signIn.shouldFetchGoogleUserID = YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.delegate = self;
[signIn trySilentAuthentication];
,这里是我的GPPSignInDelegate
方法:
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{
if (error)
{
} else
{
[self refreshInterfaceBasedOnSignIn];
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
NSLog(@"email %@ ", [NSString stringWithFormat:@"Email: %@",[GPPSignIn sharedInstance].authentication.userEmail]);
NSLog(@"Received error %@ and auth object %@",error, auth);
GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
plusService.retryEnabled = YES;
[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
[plusService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLPlusPerson *person,
NSError *error) {
if (error)
{
} else
{
NSLog(@"Email= %@", [GPPSignIn sharedInstance].authentication.userEmail);
NSLog(@"GoogleID=%@", person.identifier);
NSLog(@"User Name=%@", [person.name.givenName stringByAppendingFormat:@" %@", person.name.familyName]);
NSLog(@"Gender=%@", person.gender);
}
}];
}
}
和
-(void)refreshInterfaceBasedOnSignIn
{
if ([[GPPSignIn sharedInstance] authentication])
{
self.signInButton.hidden = YES;
} else
{
self.signInButton.hidden = NO;
}
}
我的问题是如何获取个人资料图片网址来自Google+? 我提到这个Getting Google+ profile picture url with user_id,但我没有收到准确的答案。
答
这里试试这个
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
我添加完整的代码
GPPSignIn *signIn = [GPPSignIn sharedInstance];
[email protected]_PLUS_Login_ID; // here add your ID
signIn.shouldFetchGoogleUserEmail = YES;
signIn.scopes = @[ @"profile" ];
signIn.delegate = self;
if (![signIn trySilentAuthentication]) // if it is not authenticate it re authenticate once
[signIn authenticate];
-(void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error {
if (error) {
// Do some error handling here.
} else {
// NSLog(@"Received error %@", auth.userEmail);
// NSLog(@"Received error %@", auth.accessToken);
if (auth.userEmail)
{
[[[GPPSignIn sharedInstance] plusService] executeQuery:[GTLQueryPlus queryForPeopleGetWithUserId:@"me"] completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error)
{
// this is for fetch profile image
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
NSLog(@"%@",url);
NSLog(@"Name:%@",person.displayName);
}];
}
}
}
答
这件怎么样?
let user: GIDGoogleUser = ...
if user.profile.hasImage{
let profilePicURL = user.profile.imageURLWithDimension(200).absoluteString
print(profilePicURL)
}
请参阅GIDProfileData类参考。
答
夫特3
let dimension = round(thumbSize.width * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
thumbSize.width需要图像的宽度。
let dimension = round(100 * UIScreen.main.scale)
let pic = userInfo.profile.imageURL(withDimension: dimension)
谢谢!添加了这个NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@“%@”,person.image.url]]; –
有个美好的一天的朋友..... –
大先生@ Anbu.Karthik –