Passing parameters for posting an item to Facebook - ios

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"])
}
})
}
}

Related

Cannot fetch Facebook uploaded images

func fetchListOfUserPhotos() {
// let graphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "picture"])
graphRequest.start(completionHandler: { [self] (_, result, error) in
if error != nil {
print(error as Any)
} else {
print(result)
}
})
}
getting empty data.. please help me to resolve this issue.

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

Swift can't get Facebook username

I use the lastest Facebook SDK in swift. My problem is when I try to pass my Facebook name into a variable. There is my code :
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
// This variable is declared under my class declaration
self.userFacebook = result.valueForKey("name") as! String
println(self.userFacebook) //it's works, my Facebook name appear
}
})
}
But, when I try to use my variable "userFacebook" outsite of this function (returnUserData), the result is "nil".
I don't understand why my variable is set only in the function and not in my class.
Thanks
You pass parameter request what data is get for example get name.
request parameter like this
["fields": "id, name, first_name, last_name, picture.type(large), email"]
Try this code :
#IBAction func btnFBLoginPressed(sender: AnyObject) {
var fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager .logInWithReadPermissions(["email"], handler: { (result, error) -> Void in
if (error == nil){
var fbloginresult : FBSDKLoginManagerLoginResult = result
if(fbloginresult.grantedPermissions.contains("email"))
{
self.getFBUserData()
fbLoginManager.logOut()
}
}
})
}
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result)
println(result["name"])
}
})
}
}

Facebook iOS SDK & Swift - How do I create dependent batch requests?

I'm trying to figure out how to compose FB Graph API requests (FB SDK 4.0 and Swift) when the second (child) request is dependent on the first (parent) request. Specifically I would like to get a user's albums and each album's cover photo.
me/albums?fields=name,cover_photo <-- Get user albums request
/888474748 <-- Get cover photo request
The documentation is very vague regarding this and API docs for FBSDKGraphRequestConnection mention that method addRequest:completionHandler:batchParameters: can accept parameters such as "name" and "depends_on". This appears to be the method I'm looking for but I can find an example of its use in Obj-C or Swift.
Should it look something like this? Thanks!
let albumRequest = FBSDKGraphRequest(graphPath: "me/albums?fields=name,cover_photo", parameters: nil)
let albumCoverRequest = FBSDKGraphRequest(graphPath: "cover_photo_id", parameters: nil) //what should this look like? jsonpath?
let graphConnection = FBSDKGraphRequestConnection()
graphConnection.addRequest(albumRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
if(error != nil){
println(error)
}else{
}
},batchParameters: ["name" : "albums"])
graphConnection.addRequest(albumRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
if(error != nil){
println(error)
}else{
}
},batchParameters: ["depends_on" : "albums"]) //should this be some jsonpath expression?
Got it figured out.
let albumRequest = FBSDKGraphRequest(graphPath: "me/albums?fields=name,cover_photo", parameters: nil)
let albumCoverRequest = FBSDKGraphRequest(graphPath: "?ids={result=albums:$.data.*.cover_photo}", parameters: nil) // use jsonpath syntax to "inject" parent results into "child" request
let graphConnection = FBSDKGraphRequestConnection()
graphConnection.addRequest(albumRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
if(error != nil){
println(error)
}else{
println(result)
}
},batchParameters: ["name" : "albums"]) //Set "parent" batch alias
graphConnection.addRequest(albumCoverRequest, completionHandler: { (connection:FBSDKGraphRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
if(error != nil){
println(error)
}else{
println(result)
}
},batchParameters: ["depends_on" : "albums"]) //depend on parent batch alias
graphConnection.start()

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