How to post to the Facebook wall in an iPhone app with phonegap, FaceBook Connect and ChildBrother plugin

Posting to the user’s wall is a very cool and efficient marketing strategy. The user can post on his wall his progress with a message promoting the app directly from an iPhone app.

It’s very fast and easy to add in phonegap, but the first time can be time consuming. After reading this post on the phonegap mailing
list and reading this post, I was a bit confused. With the following method, you don’t need to setup a server on Google App Engine, it’s very straight forward :

I will explain the process step by step, and at the end of this article, a demo iOS project is available for download.

Sorry for the bad formatting, unfortunately, I wrote this article in google document, and the resulting html code was horrible, I will never do that again.

Create your App in FaceBook

You need to register your app in facebook
:


http://developers.facebook.com/setup/

(Keep the ID of the app, it will be usefull for
later

Install ChildBrowser plugin:

This plugin can open an external URL directly
in the app.

  1. Download the childbrowser plugin from

    http://github.com/purplecabbage/PhoneGap-Plugins
  2. Put the file ChildBrowser.js and FBConnect.js
    inside the www folder
  3. drag and drop the files under
    ChildBrowser/iPhone/ to the /plugins folder on your xcode
    project;
  4. On XCode, right click on the plugin folder,
    and choose add existing files -> add all the ChildBrowser
    files :

Add JS files

In the FBConnect.js file, there is no function
to post to the user wall. So you need to add the following
functions :

FBConnect.prototype.postFBWall = function(message, urlPost, urlPicture, callBack)

{

console.log(‘inside postFBWall ‘+message + ‘ urlPost=’+urlPost + ‘ urlPicture=’+urlPicture);

var url = ‘https://graph.facebook.com/me/feed?access_token=’ + this.accessToken+’&message=’+message;

if (urlPost) {

url += ‘&link=’+encodeURIComponent(urlPost);

}

if (urlPicture) {

url += ‘&picture=’+encodeURIComponent(urlPicture);

}

var req = this.postFBGraph(url);

req.onload = callBack;

}

FBConnect.prototype.postFBGraph = function(url)

{

console.log(‘inside postFBGraph url=’+url);

var req = new XMLHttpRequest();

req.open(“POST”, url, true);

/*req.onreadystatechange = function() {//Call a function when the state

if(req.readyState == 4 && req.status == 200) {

alert(req.responseText);

}

};*/

req.send(null);

return req;

}

You also need to customize the connect function to ask the authorization to post to the user’s wall :

FBConnect.prototype.connect =
function(client_id,redirect_uri,display)

{

this.client_id = client_id;

this.redirect_uri = redirect_uri;


var authorize_url  = “https://graph.facebook.com/oauth/authorize?”;

authorize_url += “client_id=” +
client_id;

authorize_url += “&redirect_uri=” +
redirect_uri;

authorize_url += “&display=”+ ( display ?
display : “touch” );

authorize_url += “&type=user_agent”;

//if you want to post message on the wall : publish_stream, offline_access,

authorize_url +=”&scope=publish_stream”;


window.plugins.childBrowser.showWebPage(authorize_url);

var self = this;

window.plugins.childBrowser.onLocationChange =
function(loc){self.onLocationChange(loc);};

}

You need to add the ChildBrowser.js and the FBConnect.js in the www folder.

Then, you need to call include them in you index.html :

<!doctype html>

<html>

<head>

<meta charset=”UTF-8″ />

<title>Test</title>


<script type=”text/javascript”
src=”lib/json2.js” charset=”utf-8″></script>

<script type=”text/javascript”
src=”phonegap.js” charset=”utf-8″></script>

<script type=”text/javascript”
src=”ChildBrowser.js” charset=”utf-8″></script>

<script type=”text/javascript”
src=”FBConnect.js” charset=”utf-8″></script>

Connect to Facebook within the app

in your html, you can add a button that will share your message on Facebook:

<div class=”button facebook”>Share on Facebook</div>

And in your javascript file, you can register this button :

$(‘.facebook’).click(function() {

shareOnFaceBook(“This message will be posted on the user wall”, “http://www.mydomain.com/myImage.png”, “http://www.mydomain.com/”)

});

And you also need to customize and add this
function in your javascript:

/**********************

FaceBook Share

**********************/

// this function share the article on facebook

// text : the text to share

// image : the image to display near the link

// url : the url of the article

function shareOnFaceBook(text, image, url) {

var client_id = “XXXXXXXXXXXXXX”;//TODO customize your FaceBook AppID : http://developers.facebook.com/setup/

var redir_url = “http://www.facebook.com/connect/login_success.html”;

navigator.notification.activityStart();//Phonegap function to show a waiting message

if (typeof fbPlugin === ‘undefined’) {

console.log(‘install FBConnect’);

fbPlugin = FBConnect.install();

}

fbPlugin.connect(client_id, redir_url, “touch”);

fbPlugin.onConnect = function() {

console.log(‘onFBConnected id=’ + window.plugins.fbConnect.accessToken);

window.plugins.fbConnect.postFBWall(text, url, image, function() {

console.log(‘inside callback after postFBWall’);

alert(‘Successfully shared on Facebok (check your status)’);

navigator.notification.activityStop();//Phonegap function to hide a waiting message

});

};

}

Sorry again for the aspect of this post, next time, i promise, I will code myself the html code (never trust google doc for HTML).

And that’s it. It should be working. If you want a working example, I have build a quick and dirty iOS project in
a zip file with a sample phonegap project with this shareOnFacebook feature.

Download the sample FaceBook iOS XCode Project


How to create a Screencast video for a mobile (iPhone or Android) application with your Mac

I have created my first screencast for MosaLingua (only in french for now), and I want to share my experience here because I spent a lot of time searching disparate information, and with several trials and errors.

With this article, it will be very straightforward, and you will quickly create a video to promote your application

Application video recording

To have a perfect looking screencast, I followed the advices in this article : Not Your average iphone screencast.

So I used the application Simfinger to replace the mouse cursor by the circle, and to install the fake application in the iPhone simulator.

If you don’t want to spend time checking the sources on github and compiling the application, you can download simfinger ready to use here.

I did not use the fake iPhone body image of Simfinger, because I wanted to integrate the video on different mobile platform, and on my website.

To record the video of the application in action, I used the excellent Snapz Pro X to record the iPhone screen with  :

http://www.ambrosiasw.com/utilities/snapzprox/

Regarding the settings of Snapz Pro X, make sure you choose million of colour, and the max quality settings or the result will be ugly.

Video editing with iMovie

So you want to add a speech, transitions and maybe some marketing slides.

iMovie is really easy to use and included in Mac OS. It provides all feature needed to create a good screencast :

iMovie Quicktime export for iPhone:

When the video is finished, we need to export it in the right format, to be able to play it on the iPhone.

According to apple, the following compression standards are supported:

  • H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps. Note that B frames are not supported in the Baseline profile.
  • MPEG-4 Part 2 video (Simple Profile)
  • AAC-LC audio, up to 48 kHz

Movie files with the extensions .mov, .mp4, .m4v, and .3gp are supported.

See Creating Video for Safari on iPhone and Creating Content for Safari on iPhone.

In iMovie, export your movies using QuickTime Pro 7.2, as described in “Encoding Video for Wi-Fi, 3G, and EDGE,” :

Choose Share -> Export with Quicktime and change the settings :

After a lot of trials and errors, I can say that the optimal settings, in term of quality and size (6Mo for 2.5min) are :

Sorry for the french screenshot, but I don’t know how to change the language settings in the Apple applications.

Export your application screencast on Youtube

So you don’t want to waste your bandwith, and Youtube is perfect to host your video.

High quality export for youtube :

If you have a demo video of your iPhone app in 320×480 (portrait) and whish to upload it to YouTube, you should upscale it to HD. Open the video in QuickTime 7 Pro and export it as MP4 with Video Format H.264, 2000 kBit/sec, 1280×720 HD and check “Preserve aspect ratio using Letterbox”.

(from http://forums.macrumors.com/showthread.php?t=798599)

Add the video into an iPhone Web App :

To include your video in an iPhone web application, or a native application with UIWebView (with phonegap for example), you can use the html5 video tag :

<video src=”media/ScreenCast.mp4″ height=”100″ width=”100″>

Or you can also include the embed code presented by youtube. It works only on a real iPhone/iPod Touch and not in the simulator.

Add the video in your website

As you can see in the most of iPhone app websites (see for example 40 Awesome iPhone Application Websites), your site should include in the frontpage your screencast.

For better result, you need to use the video hosted in your website (control your bandwidth…), because the youtube video cannot be included in the iPhone body image.

The next article talk about the different way to include your video into your web site (hosting your own video, or use youtube), with code sample. Stay tuned.