在 iOS facebooks SDK 中,我想获取 facebook 用户 ID、朋友姓名和图像 URL(用户个人资料图片)。
问题:
我使用了 FBFriendPickerViewController,它显示了朋友的名字,但是有没有办法可以获取图像 URL 和 facebook id。
FBFriendPickerViewController 也是实际列出朋友列表的正确方法。或者我应该使用不同的 API。
在 iOS facebooks SDK 中,我想获取 facebook 用户 ID、朋友姓名和图像 URL(用户个人资料图片)。
问题:
我使用了 FBFriendPickerViewController,它显示了朋友的名字,但是有没有办法可以获取图像 URL 和 facebook id。
FBFriendPickerViewController 也是实际列出朋友列表的正确方法。或者我应该使用不同的 API。
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]);
}
}];
//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"];
}
第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];
}
}
第 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];
}
}
}