从Facebook获取用户名和个人资料图片iOS 7
我已经阅读了很多关于从Facebook获取信息的教程,但目前为止我失败了。我只想从Facebook获取用户名和个人资料照片。从Facebook获取用户名和个人资料图片iOS 7
- (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上的教程。
谢谢您的关注。
这是我发现获取用户个人资料图片的最简单方法。
[[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]];
}
}];
可以使用的其他查询参数是:
- 类型:小,正常的,大的,正方形
- 宽度:<值>
-
高度: <价值>
- 使用两个宽度和高度得到裁剪,纵横填图像
你也可以获取用户名和图片如下:
[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。
谢谢!比提出2个请求更简单的方法 –
实际使用“http://graph.facebook.com/ /图片?类型=小”来抓取用户,甚至他们的朋友的个人资料图片是缓慢的。
更好更快的方式将FBProfilePictureView对象添加到您的视图中,并在其profileID属性中分配用户的Facebook ID。
例如: FBProfilePictureView * friendsPic;
friendsPic.profileID = @“1379925668972042”;
进行以下图表要求:
/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"
}
}
}
附: /me?fields=picture.type(large)
不适合我。
这是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)
}
}
})
}
UPDATE回答问题:
要下载公开个人资料图片,请从字典中获取Facebook ID:
let facebookID:NSString = dict["id"] as AnyObject? as NSString
然后调用使用Facebook ID来绘制的轮廓图像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)")
}
})
个人资料图片怎么样? –
其实我找到了一个更好的方法,你可以用键“** fields **”和值“** id,email,first_name,...,picture.type(large)** “ - >然后通过参数(而不是零) - >然后你会得到图片信息以及其他一切。 –
@IslamQ。你在哪里找到这些信息?我想知道我还能在“字段”中添加什么内容。 – Damasio
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];
}
}];
}
它适用于新的Facebook SDK v2.3 –
谢谢,它适用于最新的facebook SDK。 –
谢谢,下面的链接到Facebook文档: https://developers.facebook.com/docs/ios/graph#fetching –
我需要这一代码之前做一些事情?因为当我点击按钮时,它不会去if语句。 –
那么,SDK教程(https://developers.facebook.com/docs/ios/ios-sdk-tutorial/)中介绍了一些初始化。我假设你已经遵循了这些步骤。 – Guilherme
我想我做到了:)。我从Facebook获得了一个appID,并且我在.plist文件中写了这些。这些足够了,还是需要在提取信息之前先登录? –