iOS facebook graph api comments - ios

Is there way to fetch facebook comments with graph api without logging. When user is logged in comments can be fetch without problem, first I fetch user comment box object from web and after that make second request with id of comment box. If user is logged out in first request completition handler returns error The operation couldn’t be completed. (com.facebook.sdk.core error 8.)
let request = FBSDKGraphRequest(graphPath: "http://testapi.club/test.php", parameters: nil, HTTPMethod: "GET")
request.startWithCompletionHandler { (connection, result, error) in
if error == nil {
print(result)
let og_object = (result as! Dictionary<String, AnyObject>)["og_object"]
let objecId = (og_object as! Dictionary<String, AnyObject>)["id"]
let requestForComments = FBSDKGraphRequest(graphPath: "/\(objecId!)/comments", parameters: ["summary": "true","filter": "toplevel", "order":"chronological"], HTTPMethod: "GET")
requestForComments.startWithCompletionHandler({ (connection, result, error) in
if error != nil {
print(error.description)
}else {
print(result)
}
})
}else{
print(error.localizedDescription)
}
}

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"].

Passing parameters for posting an item to Facebook

I have a code for post to Facebook but in parameters I have to add my own but its not working for me.
I have these 3 things to post my Facebook i.e. title, description, date time.
let notSureItem = NotSureItem()
notSureItem.title = self.textField.text!
notSureItem.textDescription = self.textAreaDescription.text!
notSureItem.dateTime = self.dateTime
I have this code for posting to Facebook.
if (FBSDKAccessToken.currentAccessToken() != nil){
if FBSDKAccessToken.currentAccessToken().hasGranted("publish_actions") {
FBSDKGraphRequest(graphPath: "me/feed", parameters: ["message": "hello"], HTTPMethod: "POST").startWithCompletionHandler({
(connection, result, error) -> Void in
if !(error != nil) {
NSLog("Post id:%#", result["id"])
}
})
}
}
In parameters I had passed a message which is successfully post on Facebook but if I had to pass parameters which I had stated above how to do that.
If anyone can help thanks a lot.
This solved my problem. Simply passed as string and its work.
if (FBSDKAccessToken.currentAccessToken() != nil){
if FBSDKAccessToken.currentAccessToken().hasGranted("publish_actions") {
FBSDKGraphRequest(graphPath: "me/feed", parameters: ["message": "\(self.textField.text!)\n \(self.textAreaDescription.text!)\n \(self.dateTime)"], HTTPMethod: "POST").startWithCompletionHandler({
(connection, result, error) -> Void in
if !(error != nil) {
NSLog("Post id:%#", result["id"])
}
})
}
}

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