Passing parameter between cocos and native app

Hi,
I am using cocos creator and would need to pass values to/from the native app.

Basically, I have app link information that the native iOS/Android gets and would need to pass it to the Cocos JS app to redirect to the correct page in my app.

Anyone has tried this?
Need help to make this happen

Thanks

You need define a global function on JS first, then you do native call like this.

Java:
Cocos2dxJavascriptJavaBridge.evalString(" … call global function code ")

C++:
se::ScriptEngine::getInstance()->evalString(" … call global function code ")

1 Like

Hi, thanks for this. Will try on android.
Do you have the objective C sample for it? Thanks.

Hi Huanxinyin,
I am not so familiar with this. I guess I need to import the global function in Java right? how do I do that? thanks.

It took me some time to find out… BUT finally got it.

Here is a sample code that works:

in Xcode, add a JS file in the “Resources” folder. (Initially I thought it should be on Cocos)
e.g. I have is:
GlobalFunction.js

var NativeBridge = NativeBridge || {};
NativeBridge.storeSubscriptionId = function(id) {
   cc.log("CHANGE ID:  " + id);
 };

then on my AppDelegate.cpp, I added below to trigger it:

jsb_run_script("GlobalFunctions.js");
se::ScriptEngine::getInstance()->evalString("NativeBridge.storeRedirectToUser('cholo')");

Hope it helps someone…

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.