12 December 2012
Experience building games for iOS with Flash Builder or Flash Professional and Adobe AIR will help you make the most of this article.
Additional required other products
Intermediate
With the GoViral native extension for Android from Milkman Games, you can rapidly integrate social networking support for Facebook, Twitter, and email into your mobile AIR application using ActionScript 3.
The GoViral extension for Android is a completely native Java solution that enables you to:
GoViral is also available for iOS. For iOS-specific set up instructions, see Using the GoViral social networking extension for iOS.
Before you can use the Facebook features of the extension, you'll need to create the app on the Facebook Developer site and register for a Facebook App ID.



A Facebook App ID and App Secret have now been created for your application. Take note of the App ID; you'll need it later to configure your application with Adobe AIR.


The GoViral Social Media native extension requires the AIR 3.5 SDK or a later version. You can download the latest AIR SDK from here. If you haven't already installed the AIR 3.5 SDK for your Flash Professional CS6 or Flash Builder IDE, follow the instructions below.
The most time consuming part is done. Now you’re ready to write some code!
The next step is to add the GoViralAPI.swc library (or com.milkmangames.nativeextensions.GoViral.ane, for Flash Builder 4.6 and later) to your project. These files can be found in the extension folder of the GoViral extension package.
In Flash Builder 4.6:
In FlashDevelop:
You can start using the GoViral extension with a few simple calls. See example/GoViralExample.as for a full example that shows how to interface with Facebook, Twitter, and email.
Follow these steps to get started:
import com.milkmangames.nativeextensions.*;
import com.milkmangames.nativeextensions.events.*;
GoViral.create(). Make sure the current platform is supported (PCs and Android devices are not) by checking the GoViral.isSupported() method:if(GoViral.isSupported())
{
GoViral.create();
}
else {
trace(“GoViral only works on iOS!”);
return;
}
The GoViral extension offers complete support for Facebook mobile APIs, including single sign-on/authentication, wall posts, friend invitations, and the entire Facebook graph API.
// replace 000000 with your Facebook app id!
GoViral.goViral.initFacebook(“000000”,””);
// Listen for events.
GoViral.goViral.addEventListener(GVFacebookEvent.FB_DIALOG_CANCELED, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_DIALOG_FAILED, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_DIALOG_FINISHED, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_LOGGED_IN, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_LOGGED_OUT, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_LOGIN_CANCELED, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_LOGIN_FAILED, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_REQUEST_FAILED, onFacebookEvent);
GoViral.goViral.addEventListener(GVFacebookEvent.FB_REQUEST_RESPONSE, onFacebookEvent);
GoViral.goViral.authenticateWithFacebook().You can use the GoViral.goViral.isFacebookAuthenticated() method to determine if the user is logged in already:// If the user is not already logged in...
if(!GoViral.goViral.isFacebookAuthenticated())
{
// Show a connect with Facebook prompt.
// This method takes a comma separated list of facebook READ
// permissions as a first parameter, and a comma-separated list of
// Facebook WRITE permissions as a second parameter.
// You can refer to the facebook documentation at
// http://developers.facebook.com/docs/authentication/permissions/
// to determine which permissions your app requires.
GoViral.goViral.authenticateWithFacebook("user_likes,user_photos”,” publish_actions");
}
The user will be presented with the Facebook Connect prompt, and asked to grant your app permission to use Facebook on their behalf. Android will task switch to the Facebook app to authenticate the user and then return to your app on completion.
If the device does not support multitasking, an authentication dialog box will be presented inside your own app.
When the login succeeds, fails, or is cancelled by the user, a GVFacebookEvent.FB_LOGGED_IN, GVFacebookEvent.FB_LOGIN_FAILED, GVFacebookEvent.FB_LOGIN_CANCELED event will be dispatched respectively.
To post an update to the user's Facebook wall, use showFacebookFeedDialog(). The following example will display a Facebook dialog box prompting the user to make a wall post with a title, caption, message, description, link, and image URL:
// show a dialog to post the user's wall
GoViral.goViral.showFacebookFeedDialog(
"Posting from AIR",
"This is a caption",
"This is a message!",
"This is a description",
"http://www.milkmangames.com",
"http://www.milkmangames.com/blog/wp-content/uploads/2012/01/v202.jpg"
);
When the dialog box is dismissed, one of the following events will be dispatched:
GVFacebookEvent.FB_DIALOG_FINISHED if the dialog box finished normallyGVFacebookEvent.FB_DIALOG_CANCELED if the dialog box was canceledGVFacebookEvent.FB_DIALOG_FAILED if the dialog box failedYour app can call the showFacebookRequestDialog() method of the GoViral API to prompt the user to invite their friends to your app. The following example displays the invite friends dialog box, with a message and a title. If a friend accepts the request on the website, they'll be directed to Google Play to get your app. If they accept it on their mobile device, the app will be loaded if it is installed, or they'll be sent to Google Play to get it.
// show a dialog so the user can invite friends to app.
GoViral.goViral.showFacebookRequestDialog("Try out this app!","My App Title");
When the dialog box is dismissed, one of the following events will be dispatched:
GVFacebookEvent.FB_DIALOG_FINISHED if the dialog box finished normallyGVFacebookEvent.FB_DIALOG_CANCELED if the dialog box was canceledGVFacebookEvent.FB_DIALOG_FAILED if the dialog box failedYou can retrieve the user's profile with the requestMyFacebookProfile() method. This will cause a GVFacebookEvent.FB_REQUEST_RESPONSE event to be dispatched. The friends property of this event is a Vector.<GVFacebookFriend>, which will contain the user's profile information.
The following code shows how to request the profile and handle the response.
Refer to example/GoViralExample.as for a complete sample application that handles all Facebook events.
// listen for a response
GoViral.goViral.addEventListener(GVFacebookEvent.FB_REQUEST_RESPONSE, onFacebookResponse);
// request the user's profile.
GoViral.goViral.requestFacebookFriends();
function onFacebookResponse(e:GVFacebookEvent):void
{
// the graphPath property is 'me' for a profile request.
if(e.graphPath==”me”)
{
var myProfile:GVFacebookFriend=e.friends[0];
trace(“my name is:”+myProfile.name);
trace(“my gender is:”+myProfile.gender);
trace(“My bio is:”+myProfile.bio);
}
}
You can retrieve the user's list of friends with the requestFacebookFriends() method. This will cause a GVFacebookEvent.FB_REQUEST_RESPONSE event to be dispatched. The friends property of this event is a Vector.<GVFacebookFriend>, which will contain the user's friends' information.
The following code shows how to request the user's friends and loop through the list of friends received in the response.
// listen for a response
GoViral.goViral.addEventListener(GVFacebookEvent.FB_REQUEST_RESPONSE,onFacebookResponse);
// request the user's friends list.
GoViral.goViral.requestFacebookFriends();
function onFacebookResponse(e:GVFacebookEvent):void
{
// the graphPath property is 'me/friends' for a friends request.
if(e.graphPath==”me/friends”)
{
for each(var friend:GVFacebookFriend in e.friends)
{
trace(“i have a friend named:”+friend.name);
}
}
}
You can post a BitmapData image to the user's Facebook photos with the facebookPostPhoto() method. Note that this requires you to have initialized Facebook with the "publish_stream,user_photos" permissions in your call to authenticateWithFacebook(). Refer to this document for more information on permissions.
// posts a bitmapData called 'myBitmapData' to Facebook
GoViral.goViral.facebookPostPhoto("posted from android sdk",myBitmapData);
When the request is finished, a GVFacebookEvent.FB_REQUEST_RESPONSE will be dispatched with its graphPath set to me/photos .
The GoViral extension provides access to the entirety of the Facebook Graph API via facebookGraphRequest() . You can use any method Facebook supports, including scores, achievements, videos, check-ins, events, groups, and more directly from ActionScript 3.
For more information on accessing the Graph API via the extension, see the ActionScript documentation for facebookGraphRequest(). The example application class also demonstrates this feature in the postGraphFacebook() function.
For more information on the Graph API, refer to this page.
The GoViral extension will use the device’s currently installed Twitter client to send messages via Twitter. Some Twitter Android apps do not accept messages that have both custom status messages and images; in these cases, the image will be attached but a default status message may be displayed for the user to edit.
Note: Due to the limitations of interprocess communication on Android, the native Twitter app on the Android device may dispatch a DIALOG_FINISHED event, even if the dialog box was canceled by the user.
To post a status update to Twitter just call showTweetSheet() with the message you'd like to post:
// show a twitter status post dialog with bitmapData image
GoViral.goViral.showTweetSheetWithImage("This is a native twitter post!",myBitmapData);
Upon completion, a GVTwitterEvent.TW_DIALOG_FINISHED, GVTwitterEvent.TW_DIALOG_CANCELED, or GVTwitterEvent.TW_DIALOG_FAILED event will be dispatched.
You may post a BitmapData image to Twitter using the showTweetSheetWithImage() method.
// show a twitter status post dialog with bitmapData image
GoViral.goViral.showTweetSheetWithImage("This is a native twitter post!",myBitmapData);
Upon completion, a GVTwitterEvent.TW_DIALOG_FINISHED , GVTwitterEvent.TW_DIALOG_CANCELED , or GVTwitterEvent.TW_DIALOG_FAILED event will be dispatched.
Email is a great way to invite your users to share information from your app with their friends. The GoViral native extension supports plain-text and HTML email messages, as well as image attachments, which you may pass to the API as BitmapData objects.
The showEmailComposer() method will display the native iOS Email Composer window to the user, prepopulated with the information you specify. The method takes a subject, a comma-separated list of recipients, and an email body as its parameters. The last parameter is a Boolean value specifying the format. Set it to false if your email body is formatted as plain text; set it as true if your email body is formatted as HTML.
// show an email to who@where.com and john@doe.com, with subject 'this is a subject!', and a
// plain text body of 'This is the body of the message.'
GoViral.goViral.showEmailComposer(
"This is a subject!",
"who@where.com,john@doe.com",
"This is the body of the message.",
false
);
When the email composition window is dismissed, one of the following events will be dispatched:
GVMailEvent.MAIL_SAVED if the user saved but did not send the messageGVMailEvent.MAIL_SENT if the user sent the messageGVMailEvent.MAIL_CANCELED if the user canceled the operationGVMailEvent.MAIL_FAILED if an error occurred sendingNote: Due to the limitations of interprocess communication on Android, the native email app on the Android device may dispatch a MAIL_SENT event, even if the dialog box was canceled by the user.
The showEmailComposerWithBitmap() method will display the native Android Email Composer window to the user, prepopulated with the information you specify. It uses the same parameters as showEmailComposer() , with one exception—it takes an additional parameter that you can use to specify a BitmapData image to attach to the email message.
// show an email to who@where.com and john@doe.com, with subject 'this is a subject!', and a
// plain text body of 'This has a pic attached.' Attaches a bitmapData image called
// myBitmapData.
GoViral.goViral.showEmailComposer(
"This is a subject!",
"who@where.com,john@doe.com",
"This has a pic attached.",
false,
myBitmapData
);
As with showEmailComposer() as described above, when the email composition window is dismissed an event will be dispatched indicating the status of the operation.
The Android operating system supports Intents, a powerful feature that enables an application to post a message and have another application handle it on the user’s behalf. For instance, when sharing a message as an Intent, the user may choose to post it via SMS, Bluetooth, Instagram, or another app currently installed on their device.
The shareGenericImage() method will display a list of supported applications to the user, from which they may select one to send the message. The method takes a subject and a string message as parameters. The last parameter is a Boolean value specifying the format. Set it to false if your message is formatted as plain text; set it as true if your message is formatted as HTML.
The method isGenericShareAvailable() is checked first, to ensure that the current operating system supports Intents.
If(GoViral.goViral.isGenericShareAvailable())
{
// send an intent message ‘hello world!', and a
// plain text body of 'This is the body of the message.'
GoViral.goViral.shareGenericMessage(
"hello world!",
"This is the body of the message.",
false
);
}
When the Intent is finished, the GVShareEvent.GENERIC_MESSAGE_SHARED event will be dispatched.
The shareGenericMessageWithImage() method shares an Intent with an image. It uses the same parameters as shareGenericMessage(), with one exception—it takes an additional parameter that you can use to specify a BitmapData image to attach to the Intent.
If(GoViral.goViral.isGenericShareAvailable())
{
// send an intent message ‘hello world!', and a
// plain text body of 'This is the body of the message.',
// and an image myBitmapData
GoViral.goViral.shareGenericMessage(
"hello world!",
"This is the body of the message.",
false,
myBitmapData
);
}
In your application descriptor file, you need to specify the version of the AIR SDK you are using (3.5 or later) as well as a link to the extension. For a working example, see example/app.xml.
<application xmlns="http://ns.adobe.com/air/application/3.5">
<extensions>
<extensionID>com.milkmangames.extensions.GoViral</extensionID>
</extensions>
<android>
<manifestAdditions><![CDATA[
<manifest android:installLocation="auto">
<uses-sdk android:targetSdkVersion="12"/>
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application>
<activity android:name="com.milkmangames.extensions.android.MsgWrapActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.milkmangames.extensions.android.FacebookWrapActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
</application>
</manifest>
]]></manifestAdditions>
</android>
Flash Builder 4.6 has full support for native extensions.
FlashDevelop does not have complete native extension integration, but you can still use it to build the SWF file for your app.
cd c:\dev\myappc:\dev\air_sdk_31\bin\adt -package -target ipa-test-interpreter -storetype pkcs12 -keystore
YOURKEYSTOREFILE.P12 -storepass YOURPASSWORD -provisioning-profile
YOUR_MOBILEPROVISION_FILE.mobileprovision myapp.ipa myapp.xml myapp.swf -extdir.
Take note of the dot after -extdir. It is not a typo; it tells the adt packager to look for the extension in the current directory.
Unlike on iOS, Facebook uses the Android Key Hash when authenticating your application on Android. Your Android Key Hash is a special key string that is unique to the keystore (.p12 file) you use to sign your application.
Follow these steps to get your Android Key Hash:
Here are a few important points to keep in mind:
If Android is not returning to your app after launching the Facebook single sign-on page:
Now that you have social networking up and running in your app, you may want to explore the other native extension tutorials:
For additional extensions, see More Native extensions from Milkman Games.

This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.