[英]How to fetch profile picture from Google + in iOS?
目前在我的應用程序中,我從Google+獲取電子郵件,GoogleID,用戶名,性別。
這是我在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+獲取個人資料圖片網址? 我用user_id提到了這個獲取Google+個人資料圖片網址,但我沒有收到准確答案。
試試這個
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
在這里我添加完整的代碼
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID=@GOOGLE_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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.