在iOS facebooks SDK中,我想获取facebook用户ID,朋友姓名和图片网址(用户个人资料图片)。
问题:
我使用过FBFriendPickerViewController,它显示了朋友的名字,但是我有办法获取图片网址和脸书ID。
也是FBFriendPickerViewController实际列出好友列表的正确方法。或者我应该使用不同的API。
答案 0 :(得分:4)
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error)
{
NSArray* friends = [result objectForKey:@"data"];
for (NSDictionary<FBGraphUser>* friend in friends)
{
NSLog(@"friend id = %@", friend.id);
NSLog(@"friend name = %@", friend.username);
NSLog(@"friend pic url = %@", [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?", friend.id]);
}
}];
参考 - How to get the list of friend without opening FBFriendPickerViewController iOS
答案 1 :(得分:1)
<强> First U down load the classes from this link and this link help for u 强>
//Need to declare
**@property (nonatomic, retain)**
FbGraph *fbGraph;
UserProfile *profil_obj;
MBProgressHUD *HUD;
-(IBAction)FacebookButtonPressed
{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"]==NO)
{
}
else
{
HUD=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[HUD setLabelText:@"Loading Profile..."];
}
NSString *client_id = @"********";
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
[fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:)
andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins,user_birthday"];
}
<强> STEP_2 强>
-(void)post_wall{
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/feed" withGetVars:nil];
SBJSON *parser = [[SBJSON alloc] init];
NSMutableDictionary *parsed_json = [parser objectWithString:fb_graph_response.htmlResponse error:nil];
NSString *get_string = [NSString stringWithFormat:@"%@/picture", [parsed_json objectForKey:@"id"]];
FbGraphResponse *fb_graph_response1 = [fbGraph doGraphGet:get_string withGetVars:nil];
NSLog(@"getMeimagePressed: %@", fb_graph_response1.imageResponse);
[parsed_json setObject:fb_graph_response1.imageResponse forKey:@"profileImage"];
if([parsed_json count]>0)
{
profil_obj.userprofile_dic=parsed_json;
if([[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"]==NO)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else {
[parser release];
UIAlertView *ErrorAlrt = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Please check your internet connection and try again.." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[ErrorAlrt show];
[ErrorAlrt release];
}
}
<强> STEP-3 强>
- (void)fbGraphCallback:(id)sender {
if ( (fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0) ) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"FailMessage"
message:@"You are Not Login to Facebook."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[cookies deleteCookie:cookie];
}
} else {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"]==NO)
{
profil_obj=[[UserProfile alloc] initWithNibName:@"UserProfile" bundle:nil];
[self performSelector:@selector(post_wall)];
[HUD hide:YES];
[self presentModalViewController:profil_obj animated:YES];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"SucessMessage"
message:@"You are Sucessfully Login to Facebook."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
}
else
{
profil_obj=[[UserProfile alloc] initWithNibName:@"UserProfile" bundle:nil];
[self performSelector:@selector(post_wall)];
[self presentModalViewController:profil_obj animated:YES];
[HUD hide:YES];
}
}
}