Facebook Scores API only shows me - swift

I have two users logged in with publish_actions set to friends but on both users /appID/scores only returns one user's scores. Both are posting scores successfully.
Here is the posting code:
FBSDKGraphRequest(
graphPath: "/me/scores",
parameters: ["score" : finalScore], HTTPMethod: "POST").startWithCompletionHandler { (connection, result, error) in
if error != nil {
print(error)
}
NSNotificationCenter.defaultCenter().postNotificationName(SPostedScoreNotification, object: nil)
complete()
}
And here is the retrieving code:
dispatch_async(dispatch_get_main_queue(), {
FBSDKGraphRequest(graphPath: "/" + appID + "/scores", parameters: ["fields" : "user,score"]).startWithCompletionHandler { (connection, result, error) in
var fbScores = [FBScore]()
guard
let validResult = result,
let data = validResult["data"] as? [[String : AnyObject]]
else {
complete(fbScores)
return
}
for scoreData in data {
if let fbScore = FBScore(attributes: scoreData) {
if NSCalendar.currentCalendar().isDateInToday(fbScore.date) {
fbScores.append(fbScore)
}
}
}
complete(fbScores)
}
})

Figured it out.
You must request public_profile and user_friends first and then publish_actions. I was only requesting publish_actions.
publish_actions must not be set to only_me.

Related

Requesting Facebook friends count in Swift 3

I'm trying to get the user Facebook friends count in Swift 3, but I found some problems:
First of all, I created this code based on Facebook Documention:
func getNumberOfFriends() {
let parameters = ["fields": "summary"]
FBSDKGraphRequest(graphPath: "me/friends", parameters: parameters, httpMethod: "GET").start { (connection, result, error) in
let userInformations = User.instance
if let error = error {
print("erro: \(error.localizedDescription)")
return
}
if let result = result as? [String: AnyObject] {
if let summary = result["summary"] as? NSDictionary {
if let total_count = summary["total_count"] as? Int {
userInformations.numberOfFriends = total_count
self.getFriendsCountDelegate?.didGetFriendsCount(error:false)
}
}
}
}
}
The problem is that I'm receiving an empty data response. I think the problem could be a strange thing I found in my Login: I'm requesting these permissions:
var loginButton = FBSDKLoginButton()
self.loginButton.readPermissions = ["public_profile", "user_friends", "email", "user_posts"]
self.loginButton.publishPermissions = ["publish_actions"]
but when I check those permissions, I found that I don't have it! I only have "public_profile" and "publish_actions".
When I check my FacebookApp Page, I see that it seems ok:
All permissions are ok on my Facebook App Page
Does somebody could help me? I don't know what else should I do!
Thank you

Get facebook user groups names

I'm looking for a way to get the names (names only) for all the groups a user is a member of. I'm not looking for publishing permissions or anything tricky. All I found online is how to get the groups a user is an admin of.
Edit
After searching some more, this is what i've got:
func returnUserData() {
let userRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
userRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil) {
// Process error
print("Error: \(error.debugDescription)")
} else {
guard let data = result as? [String:Any] else {
return
}
guard let userID = data["id"] else {
return
}
let groupsRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "\(userID)/groups", parameters: ["fields":"name"])
groupsRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil) {
// Process error
print("Error: \(error.debugDescription)")
} else {
print("\(result.debugDescription)")
}
})
}
})
}
Thing is that it doesn't work. The error is not nil but the result is 0 which is not true.
Please help :)
Thanks

Retrieve Facebook friends using the app - Swift 3

I am trying to retrieve a list of the user's Facebook friends that are using the app. To my knowledge, this permission is granted automatically but please do correct me if I'm wrong.
Here is the code I am using to retrieve the list. At the moment it just skips the code in the completion handler without leaving any messages in the console.
let params = ["fields": "id, first_name, last_name, name"]
FBSDKGraphRequest(graphPath: "/me/friends", parameters: params).start { (connection, result, error) in
if error != nil {
print("ERROR: \(error.debugDescription)")
}
else {
if let friendArray : NSArray = (result as! NSDictionary).value(forKey: "data") as! NSArray
{
for friend in friendArray {
print("Name \((friend as AnyObject).value(forKey: "name"))")
}
}
}
}
Unfortunately I can't seem to find any up to date documentation on this topic.

Can not fetch email from Facebook in Swift

EDIT: The problem with this question is a bit silly. As in the answer the problem is that I have not given the parameters which I want.
I can not get through with it anymore :) What can be the problem?
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let userName : NSString = result.valueForKey("name") as! NSString
print("User Name is: \(userName)")
let userEmail : NSString = result.valueForKey("email") as! NSString
print("User Email is: \(userEmail)")
}
})
}
I call the function like that:
func loginButton(loginButton: FBSDKLoginButton!,
didCompleteWithResult result: FBSDKLoginManagerLoginResult!,
error: NSError!) {
print("User Logged In")
if ((error) != nil)
{
// Process error
} else if result.isCancelled {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if result.grantedPermissions.contains("email")
{
// Do work
returnUserData()
}
}
}
I get the error:
My problem is about the fatal error. However, I am kindly waiting for the comments on 2nd and 3rd lines of the output. Because, I get this output every time but the app runs correctly.
You must specify the parameters for the request.
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "first_name, last_name, email"])

Get Facebook user details with swift and parse

i need to get the Facebook user information details when i login a new user through parse. at present i am logging in the new user below. can't seem to get the user details though. I've seen some code written on objective - c. most of the functions don't work anymore
The Facebook iOS sdks i am running is v4.3.0.
#IBAction func facebookButton(sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
println("User signed up and logged in through Facebook!")
} else {
println("User logged in through Facebook!")
}
} else {
println("Uh oh. The user cancelled the Facebook login.")
}
}
}
To get the user details you have to send a FBSDKGraphRequest after the login request.
This can be done inside the if let user = user {...} block.
// Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath:"me", parameters:nil)
// Send request to Facebook
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil {
// Some error checking here
}
else if let userData = result as? [String:AnyObject] {
// Access user data
let username = userData["name"] as? String
// ....
}
}
For the new Facebook API and version of Swift 3.1 you can do something like this:
if((FBSDKAccessToken.current()) != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name,first_name,last_name,email"]).start(completionHandler: {(connection, result, error) -> Void in
if(error != nil) {
print("Some error occurred.");
} else {
print(result!)
}
})
}