This may be obvious for some, but it took me some digging and I figured I would share what I learned in a nice, easy to digest format.
Facebook documentation recommends the share dialog over the feed dialog for mobile developers. It’s supposed to be the easiest way to share stuff from your app. It doesn’t seem to be documented, but Corona supports the share dialog.
Feed allows you to pass in a name, caption, description, and a link to customize your dialog. Share on the other hand, only takes an href parameter – this is the url for your page. When you activate the share dialog, Facebook scrapes your page looking for specific meta tags to fill in the content.
Normally these meta tags are static, but if you want to take your sharing to the next level, you can dynamically generate them using a bit of server side code.
Note: The following assumes you have facebook configured with your app and that you have access to a server running php.
First, let’s look at the Lua code for Corona:
facebook.showDialog( "share", { href = "http://www.yoursite.com/share.php?score="..tostring(levelScore).."&level="..tostring(levelId) })
Notice that we are encoding a score and level Id into the url. Pretty simple so far!
Next, let’s look at the contents of share.php:
\<?php $score = $\_GET['score']; //read the score from the url $level = $\_GET['level']; //read the level from the url ?\> \<!DOCTYPE html\> \<head\> \<!-- This url should be the same as the href you passed in to showDialog --\> \<meta property="og:url" content="\<?= "http://". $\_SERVER['HTTP\_HOST'] . $\_SERVER['REQUEST\_URI']; ?\>" /\> \<!-- Here I customized the title, but you can customize any property you want --\> \<meta property="og:title" content="I scored \<?= $score ?\> points on Level \<?= $level ?\>!"/\> \<meta property="og:description" content="This is literally the best game on Earth! Download it now!" /\> \<meta property="og:image" content="http://yoursite.com/appimage.png" /\> \<!-- Manually redirect to the page you want the user to land on. This is optional --\> \<meta http-equiv="refresh" content="0;url=http://yoursite.com"\> \<script type="text/javascript"\> window.location.href = "http://yoursite.com" \</script\> \</head\>
And that’s all it takes! Facebook has a nifty tool to verify that your meta tags are being read properly. You can check it out here. Simply enter your href with some sample values to see the results.
Example:
http://www.yoursite.com/share.php?score=390&level=50
You may be wondering why you should go through all the trouble to use Share instead of Feed. The main advantage is that you can update your meta tag content on your server, and have it propagate to all versions of your app immediately instead of building and pushing out an update just to change one or two dialog properties.
Hopefully this helps a few of you!