Skip to content

Repository files navigation

HypFacebook

A Facebook native extension for NME

This NME native extension allows you to integrate Facebook into your NME application.

It integrates the Facebook iOS SDK 3.5.1 and the Facebook Android SDK 3.0.1

These are under the Apache License, Version 2.0

Installation

See Android setup on the wiki.

Be careful when testing your app. For some user having the Facebook app installed prevents them to test their app with sandbox mode on.

There is an include.nmml file and ndll are compiled for:

  • ios armv6
  • ios armv7
  • ios simulator
  • android armv6

iOS

For iOS you need to install the FacebookSDK to be able to link the FacebookSDK.framework in XCode.

Take a look at the facebook docs for more info, in particular the " Configure a new Xcode Project" section.

Add a FacebookDisplayName key in your app-Info.plist with your app name:

 <key>FacebookDisplayName</key>
<string>_________</string>

Add a CFBundleURLTypes key in your app-Info.plist with "fb":

 <key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb_______________</string>
</array>
</dict>
</array>

Also mind to add the FacebookSDK.framework and the bundles, to the framework folder in XCode before building. Check that you choose "Create groups for any added folders" and deselect 'Copy items into destination group's folder (if needed)'.

If you target ios < 6 (iOS > 5.0 is supported), toggle this framework to optional:

  • Security
  • Social
  • Accounts
  • AdSupport

Check in the Build Settings -> Other Linker Flags that you have

-fobjc-arc
-ObjC

If there is more than one "ios linker-flags" in the nmml files, only the last one works

Android

Add the LoginActivity to your AndroidManifest.xml

<activityandroid:name="com.facebook.LoginActivity"android:theme="@android:style/Theme.Translucent.NoTitleBar"android:label="::APP_TITLE::" />

Copy the MainActivity.java to the java src folder with your package name.

Example with the template tag in the nmml file:

<templatepath="../HypFacebook/templates/android/MainActivity.java"rename="src/my/package/name/MainActivity.java"/>

Recompiling

For recompiling the native extensions just use the sh files contained in the project folder

Usage

importnme.display.Sprite;
importfr.hyperfiction.HypFacebook;
classMainextendsSprite {
varfb:HypFacebook;
publicfunctionnew () {
super ();
connectToFacebook( );
}
functionconnectToFacebook( ) :Void {
fb=newHypFacebook( "your_app_id" );
varsession_is_valid=fb.connect( false ); // false to disallow login UIif( session_is_valid ) {
_doFacebookStuff( );
} else {
fb.addEventListener( HypFacebookEvent.OPENED, _onFbOpened );
fb.connect( true ); // true to allow login UI
}
}
function_onFbOpened( _ ) {
fb.removeEventListener( HypFacebookEvent.OPENED, _onFbOpened );
_doFacebookStuff( );
}
function_doFacebookStuff( ) {
fb.addEventListener( HypFacebookRequestEvent.GRAPH_REQUEST_RESULTS, _onGraphResults );
fb.call( GRAPH_REQUEST("/me") );
}
function_onGraphResults( event:HypFacebookRequestEvent ) :Void {
trace( 'sResult:'+event.sResult );
}
}

The allowUI parameter of the connect function allow to present the login page if there is no cached/active token. You should call connect( false ) first to check if there is an active token. If not, then present a login button to the user that call connect( true ).

Once HypFacebook is opened, you pass a FacebookRequest enum value to the call function:

enumFacebookRequest {
DIALOG ( sAction:String, ?params:Hash<String> );
FEED_DIALOG ( params:Hash<String> );
REQUEST_DIALOG ( params:Hash<String> );
GRAPH_REQUEST ( sGraphPath:String, ?params:Hash<String>, ?sMethod:HTTP_METHOD );
}

And listen to the result event:

classHypFacebookEventextendsEvent{
publicvarsFacebook_token:String;
publicvarsError:String;
publicstaticinlinevarOPENED:String='OPENED';
publicstaticinlinevarCLOSED_LOGIN_FAILED:String='CLOSED_LOGIN_FAILED';
publicstaticinlinevarOPENED_TOKEN_UPDATED:String='OPENED_TOKEN_UPDATED';
}
classHypFacebookDialogEventextendsHypFacebookEvent{
publicstaticinlinevarDIALOG_CANCELED="DIALOG_CANCELED";
publicstaticinlinevarDIALOG_ERROR="DIALOG_ERROR";
publicstaticinlinevarDIALOG_SENT="DIALOG_SENT";
publicvarsPostID:String;
}

When you make a graph request, you get the raw String result from Facebook in a HypFacebookRequestEvent:

classHypFacebookRequestEventextendsHypFacebookEvent{
publicstaticinlinevarGRAPH_REQUEST_ERROR="GRAPH_REQUEST_ERROR";
publicstaticinlinevarGRAPH_REQUEST_RESULTS="GRAPH_REQUEST_RESULTS";
publicvarsResult:String;
publicvarsGraphPath:String;
}

Quick reference

classTestFb {
// Open a session with additional READ permissionsfunctionopenWithRead( ) :Void {
fb.connectForRead( true, ["user_about_me"] );
}
// USE ONLY IF YOU KNOW WHAT YOU ARE DOING: see [this](http://stackoverflow.com/questions/15840893/facebook-android-sdk-session-openforpublish-not-creating-a-new-session)// Open a session with additional PUBLISH permissionsfunctionopenWithPublish( ) :Void {
fb.connectForPublish( true, ["publish_actions"] );
}
// Present a request dialogfunctionrequestDialog( ) :Void {
varh=newHash<String>( );
h.set( "message" , "Test Request");
h.set( "to" , "<fb user id>");
fb.call( REQUEST_DIALOG( h ) );
}
// Present a feed dialogfunctionfeedDialog( ) :Void {
varh=newHash<String>( );
h.set("name","Facebook extension for NME");
h.set("caption","Build great social apps and get more installs with Haxe/NME.");
h.set("description","The Facebook extension for NME makes it easier and faster to develop Facebook integrated apps build with Haxe");
h.set("link","http://www.nme.io");
h.set("picture","https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
fb.call( FEED_DIALOG( h ) );
}
// Make a request to the Facebook Graph APIfunctiongraphApi( ) :Void {
varh=newHash<String>( );
h.set( "score", "42" );
fb.call( GRAPH_REQUEST("/<fb user id>/scores", h ,POST) );
}
// Request additional read permissionsfunctionrequestReadPermissions( ) :Void {
fb.requestNew_read_permissions( ["user_about_me"] );
}
// Request additional publish permissionsfunctionrequestPublishPermissions( ) :Void {
fb.requestNew_publish_permissions( ["publish_actions"] );
}
// Close the Facebook sessionfunctioncloseSession( ) :Void {
fb.logout( );
}
}

You can also present any facebook dialog with:

classTestFb {
functionanyDialog( ) :Void {
fb.call( DIALOG( "<dialog name>", <hashofparameters> ) );
}
}

Made at Hyperfiction

Developed by :

License

This work is under BSD simplified License.

About

HypFacebook : Facebook native extension for Haxe NME

Resources

Stars

45 stars

Watchers

14 watching

Forks

Releases

Packages

Contributors

Languages