How to share dynamic high scores on Facebook using Graph API?

Hey guys, I’m trying to post dynamic high scores on Facebook using the Graph API, but I’m running into some issues. Here’s what I’ve got so far:

let attachment = [
    "name": "High Score",
    "caption": "Check out my game performance!",
    "description": String(format: "%d days", timePlayed),
    "link": "https://mygame.com"
]

let jsonData = try? JSONSerialization.data(withJSONObject: attachment)
let attachmentString = String(data: jsonData!, encoding: .utf8)

let params = [
    "api_key": "myAppId",
    "user_message_prompt": "Share your score",
    "attachment": attachmentString
]

The problem is, when I try to include the dynamic timePlayed value, it doesn’t show up in the Facebook post. Any ideas on how to fix this? Also, how can I properly format the date difference for JSON parsing? Thanks in advance!

I’ve found that using the Facebook SDK, specifically the Graph API, can be tricky for dynamic content. Instead of trying to embed the timePlayed directly in the attachment, consider creating a custom Open Graph object for your game scores. This allows for more flexibility and better representation of dynamic data.

Define a custom object type in your Facebook App settings, then use the API to create an instance of this object with your dynamic data. You can then share this object, which will render correctly on Facebook.

Remember to handle error cases and respect user privacy settings. Also, ensure you’re complying with Facebook’s platform policies regarding game data sharing. This approach has worked well for me in similar situations.

I’ve run into similar challenges before and discovered that switching to the Facebook SDK really streamlines the process. I made sure that my project was fully set up with the Facebook SDK and integrated FBSDKShareKit. After creating a FBSDKShareLinkContent object and assigning the necessary properties, I handled the sharing process using FBSDKShareDialog. I also reformatted the dynamic time played value into a more user-friendly string, such as ‘2 days, 5 hours’, rather than just a numeric day count. It’s important to account for cases where the Facebook app might not be installed, using a fallback like web sharing. I hope this provides a useful perspective.

hey runningriver, i ran into similar probs. try using FBSDKShareLinkContent instead of raw API calls. it’s easier to handle dynamic data. smthing like:

let content = FBSDKShareLinkContent()
content.contentTitle = “High Score”
content.contentDescription = “(timePlayed) days played”
content.contentURL = URL(string: “https://mygame.com”)

hope this helps!