Call an Android Java method from Cocos2d-x Lua to open a Facebook page in the app, with a web fallback.
A product requirement asked the game to open its Facebook page. Calling device.openURL() opened the browser rather than the Facebook application, so I used the Lua-to-Java bridge on Android.
Lua
1 2 3 4 5 6 7 8 9 10 11 12
functionAppInfo.openFacebook(name) if device.platform == "android"then local signature = "(Ljava/lang/String;)V" local arguments = {name} luaj.callStaticMethod( AppInfo.JAVA_CLASSNAME, "openFacebook", arguments, signature ) end end
The Java method checks whether the Facebook package is installed, attempts an app deep link, and falls back to the HTTPS page. Deep-link formats can change between Facebook app versions, so verify the current supported link format when maintaining this code.