29 May 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 extension for iOS 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 iOS is a completely native iOS solution that enables you to:
This section covers setting up a Facebook mobile app in the Developer section of the Facebook website.
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 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 Professional:
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.ios.*;
import com.milkmangames.nativeextensions.ios.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 permissions as a 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_stream");
}
The user will be presented with the Facebook Connect prompt, and asked to grant your app permission to use Facebook on their behalf. If the official Facebook app is installed, and the device supports multitasking, iOS will task switch to the Facebook app to authenticate the user and then return to your app on completion.
If the official Facebook app is not installed, and the device supports multitasking, iOS will task switch to Safari and present an authentication dialog box there; on completion, your app will be returned to the foreground.
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 the App Store 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 the App Store 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 ios 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 exposes iOS 5's built-in Twitter functionality to ActionScript 3. Because the AIR 3.1 and AIR 3.2 SDKs do not yet support iOS 5 on Windows machines, in order to access native Twitter functions the final IPA must be compiled on a Mac with the latest version of Xcode installed.
If you are not able to compile on a Mac, the GoViral extension also supports an alternative Twitter implementation that can be compiled from Windows using Twitter's basic mobile WebViews instead. For details on compiling an IPA with AIR and the iOS 5 SDK, refer to Building your app with full Twitter support (Mac required).
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. This method shows a native iOS Twitter post dialog box and requires the iOS 5 SDK to work; if you do not compile your IPA with the iOS SDK on a Mac, or the user's device does not have iOS 5 (or later) installed, then a web-based status dialog box will be shown instead.
// 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 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 sendingThe showEmailComposerWithBitmap() method will display the native iOS 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 .
In your application descriptor file, you need to specify the version of the AIR SDK you are using (3.1 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.1">
<extensions>
<extensionID>com.milkmangames.extensions.GoViral</extensionID>
</extensions>
<id> property exactly matches the App ID you created in iTunes Connect.<iphone> section to the descriptor file, and replace the 000000 in fb000000 with your Facebook App ID.<iPhone>
<InfoAdditions>
<![CDATA[
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb000000</string>
</array>
</dict>
</array>
]]>
</InfoAdditions>
</iPhone>
When building with the extension, you'll need to specify the directory containing the com.milkmangames.nativeextensions.GoViral.ane file. Because the AIR SDK for Windows does not include support for iOS 5, you'll need a Mac with Xcode and the iOS 5 SDK installed to build an IPA file with full native Twitter support.
If you're building on a Mac, use the .ane file from the /extension/maconly folder in the extension package. If you're building on Windows, or don't need native Twitter UI support, see the next section, Building your app with basic Twitter support (or on Windows).
Flash Builder 4.6 has full support for native extensions. Follow these steps:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/./Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/.As noted above, you'll need to specify the directory containing the com.milkmangames.nativeextensions.GoViral.ane file when building with the extension. If you're building on Windows, or don't need native Twitter UI support, you can use the .ane file in the /extension root directory.
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.
su mes the directory is c:\dev\myapp.cd c:\dev\myappUse the AIR SDK to compile the final IPA for your dev ice. The foll owing command demonstrates how this is done for the sample paths described above. Replace these as necessary before running the command.c:\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.
Now that you have the GoViral extension for iOS up and running, you may want to explore the ActionScript 3 documentation or check out the other tools from available from Milkman Games.

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