how to get user data from Facebook SDK on iOS
Your GraphRequest was incorrect. If you want to take user data, graphPathe should be "me" and you should request paramter in order to get relationship state and other information. And also you need to request public profile in LogInWithReadPermissons, So In read permisson :-
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
fbLoginManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self) { (result, error) -> Void in
if error != nil {
print(error.localizedDescription)
self.dismissViewControllerAnimated(true, completion: nil)
} else if result.isCancelled {
print("Cancelled")
self.dismissViewControllerAnimated(true, completion: nil)
} else {
}
}
And When retrieving information :-
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, relationship_status"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
let fbDetails = result as! NSDictionary
print(fbDetails)
}
})
By the way if you want marital status you should use graphPath as "me" not "/{user-id}/family". You can use that for get The person's family relationships.
iOS facebookSDK get user full details
As per the new Facebook SDK, you must have to pass the parameters with the FBSDKGraphRequest
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
}
})
}
Documentations Link : https://developers.facebook.com/docs/facebook-login/permissions/v2.4
User object reference : https://developers.facebook.com/docs/graph-api/reference/user
With public profile you can get gender :
public_profile (Default)
Provides access to a subset of items that are part of a person's public profile. A person's public profile refers to the following properties on the user object by default:
id
name
first_name
last_name
age_range
link
gender
locale
timezone
updated_time
verified
How to get user info from Facebook SDK in iOS?
After login authentication, you can get details from active FBSession like below
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:@"email"];
NSString *imageUrl = [[NSString alloc] initWithFormat: @"http://graph.facebook.com/%@/picture?type=large", facebookId];
}
}];
}
Update: Instead of id
use objectID
property, after release of version v3.14.1(May 12, 2014), the id
property has been deprecated
NSString *facebookId = user.objectID;
Get Facebook user details with swift and parse
To get the user details you have to send a FBSDKGraphRequest after the login request.
This can be done inside the if let user = user {...}
block.
// Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath:"me", parameters:nil)
// Send request to Facebook
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil {
// Some error checking here
}
else if let userData = result as? [String:AnyObject] {
// Access user data
let username = userData["name"] as? String
// ....
}
}
Getting user's Personal info from Facebook in iOS
To get user Email ID you must ask permission for email while logging.
FBSDKLoginButton *loginView = [[FBSDKLoginButton alloc] init];
loginView.readPermissions = @[@"email"];
loginView.frame = CGRectMake(100, 150, 100, 40);
[self.view addSubview:loginView];
You can get user email Id in New SDK using GraphPath.
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@ and Email : %@", result,result[@"email"]);
}
}];
}
result would get you all the user Details and result[@"email"] would get you the email for logged in user.
To get Profile picture you can use
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];
or u can also use FBSDKProfilePictureView to get profile Picture by passing user profile Id:
FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
[profilePictureview setProfileID:result[@"id"]];
[self.view addSubview:profilePictureview];
Refer to :https://developers.facebook.com/docs/facebook-login/ios/v2.3#profile_picture_view
or u can also get both by passing as parameters
[[[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]);
}
}];
[Facebook-iOS-SDK 4.0]How to get user email address from FBSDKProfile
To fetch email you need to utilize the graph API, specifically providing the parameters field populated with the fields you want. Take a look at Facebook's Graph API explore tool, which can help to figure out the queries. https://developers.facebook.com/tools/explorer
The code that worked for me to fetch email is the following, which assumes you are already logged in:
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:@"id,name,email" forKey:@"fields"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result, NSError *error) {
aHandler(result, error);
}];
Related Topics
Finding It Difficult to Pass Data to Separate Viewcontroller
How to Center a Label Horizontally for All iOS Devices in Swift
Compiler Error When Assigning the Delegate for a Protocol in Swift iOS
Convenience Initialization of Uinavigationcontroller Subclass Makes Subclass Constant Init Twice
Objective-C Call Swift Function
Converting an Existing Project into Customizable Framework
Set the Center of a Uibutton Programmatically - Swift
When Should I Use Anyobject Insted of Uibutton in Swift
Xcode 6 Swift Wkwebview Keyboard Settings
Buttonwithtype' Is Unavailable: Use Object Construction 'Uibutton(Type:)
Different Cell in Tableview Swift 3
Validate Jwt Token with Rs256 or Rs512 with Swift iOS
Core Data Transient Values with Swift
Get Current Song Playing in Spotify on Iphone
How to Convert This Opengl Pointer Math to Swift