Get Email and Name Facebook Sdk V4.4.0 Swift

Get email and name Facebook SDK v4.4.0 Swift

I've used fields in android, so I figured to try it in iOS as well, and it works.

let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
if(error == nil) {
print("result \(result)")
} else {
print("error \(error)")
}
}
)

result will print:

result {
email = "email@example.com";
id = 123456789;
name = "Your Name";
}

Found that these fields are equal to the User endpoint, see this link where you can see all the fields that you can get.

Update for Swift 4 and above

let r = FBSDKGraphRequest(graphPath: "me",
parameters: ["fields": "email,name"],
tokenString: FBSDKAccessToken.current()?.tokenString,
version: nil,
httpMethod: "GET")

r?.start(completionHandler: { test, result, error in
if error == nil {
print(result)
}
})

Update for Swift 5 with FBSDKLoginKit 6.5.0

guard let accessToken = FBSDKLoginKit.AccessToken.current else { return }
let graphRequest = FBSDKLoginKit.GraphRequest(graphPath: "me",
parameters: ["fields": "email, name"],
tokenString: accessToken.tokenString,
version: nil,
httpMethod: .get)
graphRequest.start { (connection, result, error) -> Void in
if error == nil {
print("result \(result)")
}
else {
print("error \(error)")
}
}

[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);
}];

How to get user email from facebook sdk 4 whith swift

I think you would need to add the parameters.

Try changing it to:

let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])

Swift facebook login can't get email?

post logIn(permission ..) API use GraphRequestConnection to get the user info.

    func getFbUserProfileInfo() {
let connection = GraphRequestConnection()
connection.add(GraphRequest(graphPath: "/me", parameters: ["fields" : "id,first_name,last_name,email,name"], tokenString: AccessToken.current?.tokenString, version: Settings.defaultGraphAPIVersion, httpMethod: .get)) { (connection, values, error) in
if let res = values {
if let response = res as? [String: Any] {
let username = response["name"]
let email = response["email"]
}
}
}
connection.start()
}

How to get user name and email id for Facebook SDK?

Use the Following Code

 FBSession *session = [[FBSession alloc] initWithPermissions:@[@"basic_info", @"email"]];
[FBSession setActiveSession:session];

[session openWithBehavior:FBSessionLoginBehaviorForcingWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
NSLog(@"accesstoken %@",[NSString stringWithFormat:@"%@",session.accessTokenData]);
NSLog(@"user id %@",user.id);
NSLog(@"Email %@",[user objectForKey:@"email"]);
NSLog(@"User Name %@",user.username);
}
}];
}
}];

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 username from Facebook SDK 4.0 in ios

You can´t get the username anymore:

/me/username is no longer available.

Source: https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api

If you want to detect returning users, use the (App Scoped) ID instead.

Retrieve email FacebookSDK and Swift

Put the fields you want back from the Graph request in your parameters

FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,email,name,picture.width(480).height(480)"]).startWithCompletionHandler({

Because the Graph API may sometimes only return a minimum amount of information unless otherwise requested.

How to print the public details like first name, last name or email in Facebook SDK 4.18.0 in swift3?

You should use AnyObject and not Any. More on it can be found here:

if let dic = result as? [String: AnyObject] { 
if let strFirstName = dic["first_name"] as? String, let strLastName = dic["last_name"] as? String
{
print("\(strFirstName) \(strLastName)")
}
}


Related Topics



Leave a reply



Submit