Sim*_*rao 22 facebook objective-c ios facebook-sdk-4.0
我是Objective-C和iPhone开发环境的新手.
我正在我的应用程序中实现Facebook登录以获取用户名,电子邮件和个人资料图片.我已成功实施登录部分,并已收到该人的姓名和用户ID.
现在我想从Facebook获取用户的电子邮件和个人资料图片.但我没有任何想法如何获得它.我正在使用Facebook IOS SDK v4.0.
当我拥有用户ID时,如何从Facebook获取用户的个人资料图片和电子邮件ID?
Dhe*_*ngh 55
要获取用户电子邮件ID,您必须在登录时询问电子邮件权限.
FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init];
loginView.readPermissions = @[@"email"];
loginView.frame = CGRectMake(100, 150, 100, 40);
[self.view addSubview:loginView];
Run Code Online (Sandbox Code Playgroud)
您可以使用GraphPath在New SDK中获取用户电子邮件ID.
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]);
}
}];
}
Run Code Online (Sandbox Code Playgroud)
结果将为您提供所有用户详细信息和结果[@"email"]将为您提供登录用户的电子邮件.
要获取个人资料照片,您可以使用
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",result[@"id"]]];
NSData *data = [NSData dataWithContentsOfURL:url];
_imageView.image = [UIImage imageWithData:data];
Run Code Online (Sandbox Code Playgroud)
或者您也可以使用FBSDKProfilePictureView通过传递用户个人资料ID获取个人资料图片:
FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
[profilePictureview setProfileID:result[@"id"]];
[self.view addSubview:profilePictureview];
Run Code Online (Sandbox Code Playgroud)
请参阅:https://developers.facebook.com/docs/facebook-login/ios/v2.3#profile_picture_view
或者你也可以通过传递参数来获得两者
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:@{@"fields": @"picture, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *pictureURL = [NSString stringWithFormat:@"%@",[result objectForKey:@"picture"]];
NSLog(@"email is %@", [result objectForKey:@"email"]);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]];
_imageView.image = [UIImage imageWithData:data];
}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];
Run Code Online (Sandbox Code Playgroud)
Mus*_*rci 22
对不起这个凌乱的回答,这是我的第一个回答.您可以使用FBSDK Graph请求获取用户的所有配置文件信息,使用FBSDKProfilePictureView类轻松获取用户的配置文件图片.此代码用于手动Facebook登录UI.
首先,您必须将此代码放在登录进程开始的位置:
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
// There is an error here.
}
else
{
if(result.token) // This means if There is current access token.
{
// Token created successfully and you are ready to get profile info
[self getFacebookProfileInfo];
}
}
}];
Run Code Online (Sandbox Code Playgroud)
如果登录成功,请实施此方法以获取用户的公开个人资料;
-(void)getFacebookProfileInfos {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"me" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(result)
{
if ([result objectForKey:@"email"]) {
NSLog(@"Email: %@",[result objectForKey:@"email"]);
}
if ([result objectForKey:@"first_name"]) {
NSLog(@"First Name : %@",[result objectForKey:@"first_name"]);
}
if ([result objectForKey:@"id"]) {
NSLog(@"User id : %@",[result objectForKey:@"id"]);
}
}
}];
[connection start];
Run Code Online (Sandbox Code Playgroud)
获取当前登录用户的个人资料图片:
FBSDKProfilePictureView *pictureView=[[FBSDKProfilePictureView alloc]init];
[pictureView setProfileID:@"user_id"];
[pictureView setPictureMode:FBSDKProfilePictureModeSquare];
[self.view addSubview:pictureView];
Run Code Online (Sandbox Code Playgroud)
您必须向viewDidLoad方法添加刷新代码:
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES];
Run Code Online (Sandbox Code Playgroud)
小智 6
Hope This could Help You ..
- (IBAction)Loginwithfacebookaction:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];
[login logInWithReadPermissions:@[@"public_profile"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
NSLog(@"Process error");
}
else if (result.isCancelled)
{
NSLog(@"Cancelled");
}
else
{
[self getFacebookProfileInfos];
}
}];
}
- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error {
NSLog(@"Received error %@ and auth object %@",error, auth);
if (!error)
{
email =signIn.userEmail;
[[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"];
NSLog(@"Received error and auth object %@",signIn.userEmail);
NSLog(@"Received error and auth object %@",signIn.userID);
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);
name= person.displayName;
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Name:%@",person.displayName);
[self callWebserviceToUploadImage];
}];
}
}
}
-(void)getFacebookProfileInfos {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/me?fields=first_name, last_name, picture, email" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(result)
{
if ([result objectForKey:@"email"]) {
email = [result objectForKey:@"email"];
[[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"];
}
if ([result objectForKey:@"first_name"]) {
NSLog(@"First Name : %@",[result objectForKey:@"first_name"]);
name = [result objectForKey:@"first_name"];
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"];
}
if ([result objectForKey:@"id"])
{
NSLog(@"User id : %@",[result objectForKey:@"id"]);
}
}
[self callfbloginwebservice];
}];
[connection start];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
41281 次 |
最近记录: |