从Facebook iOS 7获取用户名和个人资料照片
我已经阅读了很多关于从Facebook获取信息的教程,但到目前为止我失败了。 我只想从Facebook获取用户名和个人资料照片。
- (IBAction)login:(id)sender { [FBSession openActiveSessionWithReadPermissions:@[@"email",@"user_location",@"user_birthday",@"user_hometown"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { switch (state) { case FBSessionStateOpen: [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (error) { NSLog(@"error:%@",error); } else { // retrive user's details at here as shown below NSLog(@"FB user first name:%@",user.first_name); NSLog(@"FB user last name:%@",user.last_name); NSLog(@"FB user birthday:%@",user.birthday); NSLog(@"FB user location:%@",user.location); NSLog(@"FB user username:%@",user.username); NSLog(@"FB user gender:%@",[user objectForKey:@"gender"]); NSLog(@"email id:%@",[user objectForKey:@"email"]); NSLog(@"location:%@", [NSString stringWithFormat:@"Location: %@\n\n", user.location[@"name"]]); } }]; break; case FBSessionStateClosed: case FBSessionStateClosedLoginFailed: [FBSession.activeSession closeAndClearTokenInformation]; break; default: break; } } ]; }
我用这个代码获取信息,但是我不能得到任何信息。 你能帮我吗? 或者你更喜欢教程来阅读它? 我已经阅读了developer.facebook.com上的教程。
感谢您的关注。
这是我find用户的个人资料图片最简单的方法。
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) { if (error) { // Handle error } else { NSString *userName = [FBuser name]; NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [FBuser objectID]]; } }];
其他可以使用的查询参数是:
- types :小,正常,大,方
- 宽度 :<值>
- 高度 :<值>
- 使用宽度和高度来获得裁剪的方面填充图像
if ([FBSDKAccessToken currentAccessToken]) { [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields" : @"id,name,picture.width(100).height(100)"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { NSString *nameOfLoginUser = [result valueForKey:@"name"]; NSString *imageStringOfLoginUser = [[[result valueForKey:@"picture"] valueForKey:@"data"] valueForKey:@"url"]; NSURL *url = [[NSURL alloc] initWithURL: imageStringOfLoginUser]; [self.imageView setImageWithURL:url placeholderImage: nil]; } }]; }
进行以下图表请求:
/me?fields=name,picture.width(720).height(720){url}
而且你会得到非常大的和酷的个人资料图片:
{ "id": "459237440909381", "name": "Victor Mishin", "picture": { "data": { "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/t31.0-1/c628.148.1164.1164/s720x720/882111_142093815957080_669659725_o.jpg" } } }
PS /me?fields=picture.type(large)
不适合我。
你也可以得到如下的用户名和图片:
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info"] allowLoginUI:YES completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) { if(!error && state == FBSessionStateOpen) { { [FBRequestConnection startWithGraphPath:@"me" parameters:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"id,name,first_name,last_name,username,email,picture",@"fields",nil] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSDictionary *userData = (NSDictionary *)result; NSLog(@"%@",[userData description]); }]; } } }]; Output: picture = { data = { "is_silhouette" = 0; url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc1/t5.0-1/xxxxxxxxx.jpg"; }; }; username = xxxxxxxxx;
您可以将参数保留为图片和用户名,并根据您的要求排除其他参数。 HTH。
这是Facebook SDK 4和Swift的代码:
if FBSDKAccessToken.currentAccessToken() != nil { FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler({ (connection, result, error) -> Void in println("This logged in user: \(result)") if error == nil{ if let dict = result as? Dictionary<String, AnyObject>{ println("This is dictionary of user infor getting from facebook:") println(dict) } } }) }
更新回答问题:
要下载公共个人资料图片,您需要从字典中获取Facebook ID:
let facebookID:NSString = dict["id"] as AnyObject? as NSString
然后使用facebook ID调用请求来为configuration文件图像绘制API:
let pictureURL = "https://graph.facebook.com/\(fbUserId)/picture?type=large&return_ssl_resources=1"
示例代码:
let pictureURL = "https://graph.facebook.com/\(fbUserId)/picture?type=large&return_ssl_resources=1" // var URLRequest = NSURL(string: pictureURL) var URLRequestNeeded = NSURLRequest(URL: URLRequest!) println(pictureURL) NSURLConnection.sendAsynchronousRequest(URLRequestNeeded, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!, error: NSError!) -> Void in if error == nil { //data is the data of profile image you need. Just create UIImage from it } else { println("Error: \(error)") } })
实际上使用“ http://graph.facebook.com/ / picture?type = small”来获取用户的个人资料图片,甚至他们的朋友都很慢。
将FBProfilePictureView对象添加到视图和其profileID属性中的更好更快的方法是分配用户的Facebook标识。
例如:FBProfilePictureView * friendsPic;
friendsPic.profileID = @“1379925668972042”;
看看这个lib: https : //github.com/jonasman/JNSocialDownload
你甚至可以得到推特
- Facebook SDKlogin从不在iOS 9上回拨我的应用程序
- 想要自定义标题/图像/描述在Facebook的共享链接从一个Flash应用程序
- 自定义Facebookloginbutton – Android
- jQuery的拖放 – 逆向工程Facebook的uiTokenizer关于“Arts&Interests”
- Facebook:获取用户是pipe理员的页面列表
- Facebook Graph API中的Feed,post和状态有什么区别?
- Facebook的API – 如何获取用户的地址,电话号码?
- Open Graph命名空间声明:带有XMLNS或头部前缀的HTML?
- 我如何删除我开发的Facebook应用程序?