Get location / country from Facebook - Swift 2 - ios

I'm having problems trying to get the user's country.
It was easy to get email, name, birthday, gender, hometown, etc... but when I try to get the user location I get nothing.
The code I'm using is:
Permisions:
loginManager.logInWithReadPermissions(["public_profile", "user_hometown", "user_location"], fromViewController: self)
The request:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"location"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if error == nil{
print(result)
let location : NSDictionary! = result.valueForKey("location") as! NSDictionary
print(location)
}else{
print(error)
}
})
The result of this request is: {id = 1083969718300950;}
I read about FBSDKGraphRequest with parameters: nil, but it doesn't work anymore.
Someone knows how to get the country nowdays?

Related

How to get user's list of Events they are attending from FBSDK (Swift 4, iOS)

I keep getting back an empty array from the FBSDK even though my user account in Facebook is marked as attending 4 upcoming events.
Here is my code
func loadEvents() {
let parameters = ["fields": "id, name, place"]
FBSDKGraphRequest(graphPath: "me/events", parameters: parameters).start { (connection, result, error) -> Void in
if ((error) != nil) {
print("Error")
print("Error getting events \(String(describing: error))");
}
print(result)
if let result = result as? [String: Any] {
if let data = result["data"] as? NSArray {
//print(data)
}
}
}
And the result im getting in data is the following:
["data": <__NSArray0 0x60c00000b410>(
) ]
Any help appreciated. Is this a permissions issue? or am i passing in the wrong parameters object?
OK it was a permissions issue. I needed to add the user_events permission when login.
loginButton.readPermissions = ["public_profile", "user_events"]

Facebook Page Image returning null in FBSDKGraphRequest

I am attempting to retrieve the image from a facebook page. I have retrieved the page ID, and now I need to use this ID to get the image from the page. I am using the following code:
let pictureRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: picPath, parameters: ["fields" : "data"], HTTPMethod: "GET")
pictureRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
if (error != nil) {
print("picResult: \(result)")
} else {
print(error!)
}
})
I am not getting an error, but my result is null. The picpath looks like this:
/110475388978628/picture
I copied the code directly from here but it isn't working. Any Idea what I'm doing wrong? I have the access token because I am able to get the page ID through the graph request
If you want to get the picture in the same request as the rest of the users information you can do it all in one graph request.
let request = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email, picture.type(large)"])
request.startWithCompletionHandler({ (connection, result, error) in
let info = result as! NSDictionary
if let imageURL = info.valueForKey("picture")?.valueForKey("data")?.valueForKey("url") as? String {
//Download image from imageURL
}
})
there is no parameter called "fields". replace ["fields" : "data"] with nil
let pictureRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: picPath, parameters: nil, HTTPMethod: "GET")
pictureRequest.startWithCompletionHandler({ (connection: FBSDKGraphRequestConnection!, result, error) -> Void in
if (error != nil) {
print("picResult: \(result)")
} else {
print(error!)
}
})
You can use the Graph API explorer tool to help construct the graph request first: https://developers.facebook.com/tools/explorer
In this case, you'll see that you want the graph path to be just the object id (i.e., your picPath should be just 110475388978628 and your parameters should be [ "fields" : "picture"].
Then you'll want to parse the "url" out of the result["picture"]["data"]["url"].

Why am I getting this error when accessing Facebook information of user?

Why is it giving me the error 2015-09-14 20:56:31.773 Musec[7207:3288153] FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter I am trying to access user information like friends, email, etc. How do I fix the error? Below is the login plus the permissions to get access to user info.
#IBAction func loginWithFB(sender: AnyObject) {
var permissions = [ "public_profile", "email", "user_friends"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: { (user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
println("User signed up and logged in through Facebook!")
self.performSegueWithIdentifier("success", sender: self)
//let params = ["fields": "name, email, friends"]
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "name, email, friends"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
//get username
let userName : NSString = result.valueForKey("name") as! NSString
println(userName)
//get Facebook ID
let email: NSString = result.valueForKey("email") as! NSString
println(email)
//get facebook friends who use app
let friendlist: NSString = result.valueForKey("friends") as! NSString
println(friendlist)
}
})
} else {
println("User logged in through Facebook!")
self.performSegueWithIdentifier("success", sender: self)
}
} else {
println("Uh oh. The user cancelled the Facebook login.")
}
})
}
I don't think that FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter is actually an error. It is just a Log. If you will run the code and set a break point you will find that, error is actually nil and you will be able to successfully compile the code and see the else portion of the code getting executed.
Now, you are trying to access the name of Facebook user but not passing name in the fields. That is why you will get name as null. Pass name as list of parameters.
let params = ["fields": "name, email, friends"] <----- This line
Or here:
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "name, email, friends"])
Now, you will be able to fetch name, email and friends of the Facebook user.

Get data on Facebook user - Swift

I'm trying to get data on a facebook user by using his ID.
I have an array of facebook user ids called IDs, and I set the graphPath to: "/IDs[0]", when I run the code I get an error.
cam you tell me whats wrong with my code?
var pathIs: String = "/"
pathIs += IDs[0]
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: pathIs, parameters: ["fields": "id, name, birthday, picture.type(small)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")
}
})
Thanks!

iOS Facebook SDK 4.0.1 getting user's email

I'm trying to get the user's email address with the followings:
fbLoginManager.logInWithReadPermissions(["email", "public_profile"], handler: { (loginResult, error) -> Void in
if error != nil {
//handle error
} else if loginResult.isCancelled {
//handle cancellation
} else {
//Logged in
self.loggedinUser.fbToken = FBSDKAccessToken.currentAccessToken().tokenString
var graphReq = FBSDKGraphRequest(graphPath: "me", parameters: nil).startWithCompletionHandler { (connection, result, error) -> Void in
if let user = result as? NSDictionary {
var email = result.objectForKey("email") as? String
var name = result.objectForKey("name") as? String
self.loggedinUser.email = email ?? nil
self.loggedinUser.fullName = name ?? nil
}
}
}
})
But in the loginResult object the user's email is not sent. Any idea how to solve this?
thanks
You must provide in the parameters what you want to get:
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
For example to get more than one field:
parameters: ["fields": "id, name, email, picture.type(large)"]

Resources