Nested syntax to use on Facebook Graph API request - ios

I want to receive a larger profile picture than the standard 50px one that returns.
I can receive email etc no problem, but how do I pass fields to indicate that I want a larger profile picture. I am clear that you can pass nested parameters to picture to return a larger one, but I cannot find information on how to write this when dealing with the Facebook iOS SDK.
extension LoginManagerLoginResult {
func graphData() {
let params = ["fields": "first_name, last_name, email, picture{height: 1000}"]
let graphRequest = GraphRequest(
graphPath: "me",
parameters: params,
tokenString: self.token!.tokenString,
version: nil,
httpMethod: .get
)
graphRequest.start { graph, any, error in
debugPrint(any)
debugPrint(error)
debugPrint(graph)
}
}
}

I had the syntax wrong. To get pictures off different sizes you can pass fields like below:
let params = ["fields": "first_name, last_name, email, picture.width(1000)"]
You can also pass:
picture.type(large)
or:
picture.height(300)
Or other height values as required.

Related

Post on Facebook/ twitter in background in Swift

Can anyone recommend me any swift library to post on facebook and twiiter.
I am trying this for now
if FBSDKAccessToken.currentAccessToken().hasGranted("publish_actions")
{
print("publish actions already granted.")
}
else
{
FBSDKGraphRequest.init(graphPath: "me/feed", parameters: ["message" : "hello world"], HTTPMethod: "POST").startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error == nil))
{
print("Post id: \(result.valueForKey("id") as! String)")
}
})
}
There is a situation that when user creates an event then my app will automatically post/tweet on its wall about the event he just created.
I am fimiliar about swifter and Facebook SDK but i am not sure if it will help me post in background
How luschn said, you can't post automatically, but you can ask user if he want to post event.
For facebook post just use Facebook SDK(https://developers.facebook.com/docs/ios), you have there instruction for installation. You can use "Posting Data" from https://developers.facebook.com/docs/ios/graph, but you need first to check if user give you right to post it.
For Twitter post, apple give you a library(Social.framework) can help you for post on Twitter. You can learn how use it from http://code.tutsplus.com/tutorials/social-framework-fundamentals--mobile-14333. Also you can use this framework for facebook post.
guys i know i asked a silly question , but after upgrading my project to swift 2.0 and fb sdk 4.6 i did the posting
var params: NSDictionary = NSDictionary()
let userInfo: AnyObject = LocalStore.userDetails()!
let name = userInfo["name"] as? String
params = ["message": msgString]
let request: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/me/feed", parameters: params as [NSObject: AnyObject], HTTPMethod: "POST")
request.startWithCompletionHandler({ (connection, result, error) -> Void in })
This way you can post any message in background... but do check for fb login and ask for publishPermission(loginWithPublishPermission).

iOS Facebook Graph Request multiple picture sizes

I'm using Facebook's graph API and am trying to get two different picture sizes of a user's current profile picture. One picture I would like to be size 250x250, and the other picture I would like be size 1080x1080. Here is my current code:
let params = ["fields": "first_name, last_name, email, picture.width(1080).height(1080)"]
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
graphRequest.startWithCompletionHandler { (connection, result, error) in
if error != nil {
print(error)
}
}
This returns the URL to a picture of size 1080x1080 and works. Now if I change the params to add a request for a picture of size 250x250:
let params = ["fields": "first_name, last_name, email, picture.width(250).height(250), picture.width(1080).height(1080)"]
I get this error basically saying I can't use the field picture more than once:
Syntax error "Field picture specified more than once. This is only possible before version 2.1" at character 94: first_name, last_name, email, picture.width(250).height(250), picture.width(1080).height(1080)
Does this mean the only way to accomplish what I want is to make a batch request? And if that's the case, is there a function that gets called once both batch requests have completed?
There's another (better) way of doing this with just a single, non-batch request by using field aliasing, which I came across recently when I had the same issue.
In the OP's case the fields parameter would be set to:
first_name, last_name, email, picture.width(250).height(250).as(picture_small), picture.width(1080).height(1080).as(picture_large)
The response will then include two fields named picture_small and picture_large containing the respective picture data. E.g:
{
"first_name": "Mark",
"last_name": "Zuckerberg",
"picture_small": {
"data": {
"height": 320,
"is_silhouette": false,
"url": "https://scontent.xx.fbcdn.net/hprofile-xtl1/v/t1.0-1/p320x320/12208495_10102454385528521_4749095086285673716_n.jpg?oh=e14ffdec03ceb6f30da80d1c4c5c4c02&oe=57254837",
"width": 320
}
},
"picture_large": {
"data": {
"height": 547,
"is_silhouette": false,
"url": "https://scontent.xx.fbcdn.net/hprofile-xtl1/v/t1.0-1/12208495_10102454385528521_4749095086285673716_n.jpg?oh=b008ae35daeea9b0d9babbee9d701a34&oe=572336EC",
"width": 547
}
},
"id": "4"
}
This worked for me to get the large size images. Check the following query.
NSString * graphPath = [NSString stringWithFormat:#"%#?fields=photos.fields(id,link,source)", albumID];
You have two solutions:
the boring one is to make two requests. while the better solution is to make a batch request:
This is the cURL batch request to my profile, and it works:
curl -XPOST "https://graph.facebook.com/v2.4" -d "access_token=put_your_access_token_here" -i -d 'batch=[{"method" : "GET", "relative_url":"me?fields=first_name,last_name,picture.width(250).height(250)"},{"method" : "GET", "relative_url" : "me?fields=first_name,last_name,picture.width(1080).height(1080)"}]' -i
You just need to put your access token.

Fetch all fitness data at once

From the official Facebook docs you can get simple statement that you can fetch 3 types of fitness activities:
fitness.bikes
fitness.walks
fitness.runs
That's great but they all share the same fields and there's no possibility to distiguish which one was fetched besides the graphPath.
I need all of this data and ideally I want to fetch it all at once but if I create a method like this:
let walkRequest = FBSDKGraphRequest(graphPath: "me/fitness.walks", parameters: nil)
let runRequest = FBSDKGraphRequest(graphPath: "me/fitness.runs", parameters: nil)
let bikeRequest = FBSDKGraphRequest(graphPath: "me/fitness.bikes", parameters: nil)
var connection = FBSDKGraphRequestConnection()
connection.addRequest(walkRequest) { (connection, result, error) -> Void
//
}
connection.addRequest(runRequest) { (connection, result, error) -> Void
//
}
connection.addRequest(bikeRequest) { (connection, result, error) -> Void
//
}
connection.start()
I get three different callbacs, returning same data and somehow I have to merge this into one structure so I can send it to the backend.
I'd like to get a Dictionary with structure similiar to this:
[
"walks": [ JSON ],
"runs": [JSON],
"bikes": [ JSON ]
]
Is it possible to create a batched request like this? Or do I have to do it manually with 3 different FBSDKGraphRequests and just wait for them to finish?
You should be able to do this via the so-called field expansion:
GET /me?fields=fitness.bikes,fitness.walks,fitness.runs
As I unfortunately have no such data in my account, I can't test the result, but the query runs smoothly.

Facebook Login doesn't fetching all User's profile information in swift

In my app I implemented FacebookLogin accessing permissions:
let facebookReadPermissions = ["public_profile", "email", "user_friends"]
Once all permissions granted, I am calling graph API for fetching user's profile info using:
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
It is returning just two values: { id = 86160953055XXXX; name = "Devendranath Reddy"; }
I want Address, email, phone, first and last names also.
What is wrong here.
I even enabled for live access in developer.apple.com -> MyApp -> Settings
Please help me out.
Thanks in advance.
try this out, you need to set the parameters value:
so for Swift it would be this:
parameters:#{#"fields": #"id, name"}
In swift it would be something similar
something like this:
let params = ["fields": "email, friends"]
so:
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
Add whatever fields you want to add, like "first_name" "last_name" etc, etc.
let params = ["fields": "email, first_name, gender, last_name, location"]
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: params)
graphRequest.startWithCompletionHandler(
{
(connection, result, error) -> Void in
if ((error) != nil)
{
// Failure code goes here.
}
else
{
// Success code goes here
println("User Name is: \(userName)")
println("User Email is: \(userMail)")
println("Gender is: \(gender)")
}
})
This request won't grab all the information until your app is reviewed by Facebook's app review. It will only give you access to the public profile info, regardless of what permissions the user accepts.

Trouble retrieving list of facebook friends from json data in swift

I am using FBSDK in my Swift iOS application and currently I am trying to retrieve the list of friends who also use my app. I have two-three people who should show up but whenever I search the graphrequest result for friends it returns nil..
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result:AnyObject!, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
//get Facebook ID
let faceBookID: NSString = result.valueForKey("id") as NSString
//get username
let userName : NSString = result.valueForKey("name") as NSString
//get facebook friends who use app
let friendlist: AnyObject = result.valueForKey("friends") as AnyObject
}
I have the "email", "user_friends", and "public_profile" permissions which should be enough to retrieve this information. Any Ideas on what I am doing wrong?? this is doing my head in as my friend doing an android version has successfully got his working..
My problem here was that I never Actually requested for the list of friends in my facebook graph request...
after replacing FBSDKGraphRequest(graphPath: "me", parameters: nil)
with FBSDKGraphRequest(graphPath: "me?fields=id,name,friends", parameters: nil)
I was able to retrieve the list of friends because the response actually contained information for this. A very trivial mistake on my part.
In v2.0 of the Graph API, you must request the user_friends permission
from each user. user_friends is no longer included by default in every
login. Each user must grant the user_friends permission in order to
appear in the response to /me/friends.
see here : https://developers.facebook.com/bugs/1502515636638396/

Resources