How to open URL?

HI all!
I want to add to my game button which opens any URL (for example my facebook profile) in system browser.
Is that possible? Please don’t tell me no :wink: I wish I could do that.
Greetings.

Anyone?

I want to add in my free app image which will open AppStore page with my payed app. How to add opening external url functionality to my app?
Any suggestion will be appreciated!

bind to your own native function.

Tobias S wrote:

HI all!
I want to add to my game button which opens any URL (for example my facebook profile) in system browser.
Is that possible? Please don’t tell me no :wink: I wish I could do that.
Greetings.

I wrote this function recently.

First, you have to create a h file to declare which you want to call Android and IOS:

//
//  WebOpNative.h
//  HelloJavascript
//
//  Created by Simon on 13/9/9.
//
//

#ifndef __HelloJavascript__WebOpNative__
#define __HelloJavascript__WebOpNative__

#include 
#include "cocos2d.h"
#include "cocos-ext.h"
#include 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include 
#include "platform/android/jni/JniHelper.h"
#include 
#endif

using namespace cocos2d;

class WebOpNative {

public:
    static void openLinkWithWebView(const char* url);//usage for Android
    static void openLinkWithWebView_oc(const char* url);//usage for IOS
};

#endif /* defined(__HelloJavascript__WebOpNative__) */

implements openLinkWithWebView(const char* url) as below:

//
//  WebOpNative.cpp
//  HelloJavascript
//
//  Created by Simon on 13/9/9.
//
//

#include "WebOpNative.h"

void WebOpNative::openLinkWithWebView(const char *url)
{
    CCLog("into openLink");

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    CCLog("Involking openFansPage JNI...");
    JniMethodInfo t;
    //去JAVA層找出static函式openPage
    if (JniHelper::getStaticMethodInfo(t, "com/hardworking/challengejs/WebOpAndroid"
                                       ,"openPage"
                                       ,"(Ljava/lang/String;)V"))
    {
        jstring str = t.env->NewStringUTF(url);
        t.env->CallStaticVoidMethod(t.classID,t.methodID,str);
    }
#endif
}

The code above means you will call JNI from C*+ to Java.
So, you need to write some code in Java to corresponding caller above.
Android side:
<pre>
public class WebOpAndroid {
/***
** 開啟網頁
* `param url 請傳入欲開啟的url
*/
public static void openPage(String url){
Log.i(“tag”, "android side get url: "+url);
android.net.Uri uri = android.net.Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
MyNativeActivity.cnx.startActivity(intent);
}
}

Remember to write native class name you will “make” in Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2djs_shared

LOCAL_MODULE_FILENAME := libcocos2djs

LOCAL_SRC_FILES := hellojavascript/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../../Utility/WebManager/WebOpNative.cpp \
                   ../../libs/JSBinding/BindingCode/WebOperator.cpp \
                   ../../libs/JSBinding/BindingCode/autogencustombindings.cpp


LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static
LOCAL_WHOLE_STATIC_LIBRARIES += scriptingcore-spidermonkey
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static

LOCAL_EXPORT_CFLAGS := -DCOCOS2D_DEBUG=2 -DCOCOS2D_JAVASCRIPT

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,CocosDenshion/android)
$(call import-module,external/chipmunk)
$(call import-module,scripting/javascript/spidermonkey-android)
$(call import-module,scripting/javascript/bindings)
$(call import-module,cocos2dx/platform/android)

For open the page you want,
have to declare user-permission in AndroidManifest.xml(Prefer to attachment prefer3.png)

Implements openLinkWithWebView_oc function (IOS side) as below.
IOS side:

//  本頁使用了ARC機制
//
//  FileManager.m
//  Challenge_IOS
//
//  Created by Simon on 13/7/17.
//
//
#import "../../../Utility/WebManager/WebOpNative.h"

static void static_openLinkWithWebView(const char* willopenurl)
{
    //Create a URL object.
    NSString *url = [NSString stringWithUTF8String:willopenurl];
    NSLog(`“will open url is, %@”,url);
 openURL:];
}
void WebOpNative::openLinkWithWebView\_oc
{
 static\_openLinkWithWebView;
}
\
Put above code into ios folder
Congratulations! you have finished the code which can open url using C*+ on Android and IOS if you don’t use Javascript(JSB).

If you are using Javascript to write cocos2dx game,
last thing you have to do is using JSB binding-generator to build a bridge to connect Javascript and C++.
You have to create one class that declare what function you will use in Jaavscript(like attachment prefer2.png).
then using binding-generator to bind them.(Maybe you can prefer my blog tutorial: http://lp43-cocos2dx.blogspot.tw/2013/09/cocos2dx-jsbjavascript-call-cbindings.html to learn how to generate JSB-Binding code).

The way above is my way to create custom function we need.
A bit hard work I know.

Good Luck!

Simon

Simon Pan wrote:

Tobias S wrote:

Good Luck!
>
Simon

you wrote everything very useful.
Thanks a lot

only one thing I am confused.

when useing jsb generator,

step 11 compile and run bindings-generator

execute

./test.sh

it reports

…. …. Processing section testandroid

Errors in parsing headers:

  1. <severity = Warning,
    location = <SourceLocation file None, line 0, column 0>,
    details = "argument unused during compilation: ’~~nostdinc++‘“>
  2. <severity = Fatal,
    location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
    details =”’string’ file not found">

* Found errors~~ can not continue
Fatal error in parsing headers

john w wrote:

Simon Pan wrote:
> Tobias S wrote:
>
> Good Luck!
>
> Simon
>
>
you wrote everything very useful.
Thanks a lot
>
only one thing I am confused.
>
when useing jsb generator,
http://lp43-cocos2dx.blogspot.tw/2013/09/cocos2dx-jsbjavascript-call-cbindings.html
>
step 11 compile and run bindings-generator
>
how to know which .h/.cpp file need to bind?
for example, if have created WebOpNative.h and WebOpNative.cpp
how to make the generator to know WebOpNative.h and WebOpNative.cpp are the files I hope to bind?

In tutorial,
you will found simple_test folder,
under this folder you’ll see simple_class.h and simple_class.cpp that is function you want to call from JS,
this concept like a interface, adapter or delegate,
call native function is the target,
so you have to write code here before compile and JSB binding
For example in your question,
I wrote code below:

//
//  WebOperator.cpp
//  Challenge_IOS
//
//  Created by Simon on 13/8/12.
//
//

#include "WebOperator.h"
#include "../../../../Utility/WebManager/WebOpNative.h"

WebOperator::WebOperator(){

}
WebOperator::~WebOperator(){

}
void WebOperator::openLink(const char * url)
{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    WebOpNative::openLinkWithWebView(url);
    CCLog("into openLink %s on Android", url);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    WebOpNative::openLinkWithWebView_oc(url);
    CCLog("into openLink %s on IOS", url);
#endif
}

use this interface to call native function: WebOpNative::openLinkWithWebView(url);
and then I use binding-generator to generate this JSB file(prefer4.png as attachment)

last thing is you have to modify your phyton code to compile JSB-Binding code as below:
in my case in test.sh

# options

usage(){
cat << EOF
usage: $0 [options]

Test the C++ <-> JS bindings generator

OPTIONS:
-h  this help

Dependencies :
PYTHON_BIN
CLANG_ROOT
NDK_ROOT

Define this to run from a different directory
CXX_GENERATOR_ROOT

EOF
}

while getopts "dvh" OPTION; do
case "$OPTION" in
d)
debug=1
;;
v)
verbose=1
;;
h)
usage
exit 0
;;
esac
done

# exit this script if any commmand fails
set -e

# find current dir
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# read user.cfg if it exists and is readable

_CFG_FILE=$(dirname "$0")"/user.cfg"
if [ -e "$_CFG_FILE" ]
then
    [ -r "$_CFG_FILE" ] || die "Fatal Error: $_CFG_FILE exists but is unreadable"
    . "$_CFG_FILE"
fi

# paths

if [ -z "${NDK_ROOT+aaa}" ]; then
# ... if NDK_ROOT is not set, use "$HOME/bin/android-ndk"
    NDK_ROOT="$HOME/bin/android-ndk"
fi

if [ -z "${CLANG_ROOT+aaa}" ]; then
# ... if CLANG_ROOT is not set, use "$HOME/bin/clang+llvm-3.3"
    CLANG_ROOT="$HOME/bin/clang+llvm-3.3"
fi

if [ -z "${PYTHON_BIN+aaa}" ]; then
# ... if PYTHON_BIN is not set, use "/usr/bin/python2.7"
    PYTHON_BIN="/usr/bin/python2.7"
fi

# paths with defaults hardcoded to relative paths

if [ -z "${CXX_GENERATOR_ROOT+aaa}" ]; then
    CXX_GENERATOR_ROOT="$DIR/.."
fi

echo "CLANG_ROOT: $CLANG_ROOT"
echo "NDK_ROOT: $NDK_ROOT"
echo "CXX_GENERATOR_ROOT: $CXX_GENERATOR_ROOT"
echo "PYTHON_BIN: $PYTHON_BIN"

# Generate bindings for simpletest using Android's system headers
echo "Generating bindings for simpletest with Android headers..."
set -x
LD_LIBRARY_PATH=${CLANG_ROOT}/lib $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py /Users/lp43/cocos2d-x3.0/projects/ChallengeJS/libs/JSBinding/test.ini -t spidermonkey -s testandroid -o ./BindingCode

last line means you have test.ini script that is use to compile JSB-Binding code.
./BindingCode means where is the path you want to put the compiled files.

in test.ini I modified as below:

[testandroid]
name = BindingCode
prefix = autogencustombindings
classes = WebOperator PictureOperator
extra_arguments = -D_SIZE_T_DEFINED_ -nostdinc -nostdinc++ -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(clangllvmdir)s/lib/clang/3.3/include -I%(projectpath)s/libs/JSBinding/WebManager -x c++ -std=c++11
headers = %(projectpath)s/libs/JSBinding/BindingCode/WebOperator.h %(projectpath)s/libs/JSBinding/BindingCode/PictureOperator.h
target_namespace = utility
remove_prefix =
skip = 
base_objects =
abstract_classes =
classes_have_type_info = no
rename =
rename_functions =
rename_classes =
# classes for which there will be no "parent" lookup
classes_have_no_parents =

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip =

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = yes

prefix = autogencustombindings 

that is tell generator you will auto generate a function that is autogencustombindings, you will use this in AppDelegate.hpp

 sc->addRegisterCallback(register_all_autogencustombindings);

classes = WebOperator  

that means you have what class in your interface,
is this case as above is WebOperator.

headers = %(projectpath)s/libs/JSBinding/BindingCode/WebOperator.h

means where is this interface’s header.
compliled code will need to use this header to know how to call the implements body(.cpp).

target_namespace = utility

means you will use this JSB-binding function in Javascript side like:

utility.WebOperator.openLink(url);

as you can see, I believe you have known how to let generator know which class is you want to bind.

I have already tell you all what I know I survey and studied.
the other script meaning or usage I can’t understand as well.

Simon Pan wrote:
>

Thanks again
in my case
It compiles and reports error

…. …. Processing section testandroid
>
>
Errors in parsing headers:

  1. <severity = Warning,
    location = <SourceLocation file None, line 0, column 0>,
    details = "argument unused during compilation: ’~~nostdinc++‘“>
  2. <severity = Fatal,
    location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
    details =”’string’ file not found">
    >
    Found errors~~ can not continue
    Fatal error in parsing headers

But it generates the jsb files

confuse why and if the jsb files are useful

I notice that cocos2dx supports pluginx
but pluginx does not support jsb now.
I wonder if I can do using the same way above to create jsb for pluginx

john w wrote:

Simon Pan wrote:
>
>
Thanks again
in my case
It compiles and reports error
>
> …. …. Processing section testandroid
>
>
> Errors in parsing headers:
> 1. <severity = Warning,
> location = <SourceLocation file None, line 0, column 0>,
> details = "argument unused during compilation: ’nostdinc++‘“>
> 2. <severity = Fatal,
> location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
> details =”’string’ file not found">
>
> Found errors
can not continue
> Fatal error in parsing headers
>
But it generates the jsb files
>
confuse why and if the jsb files are useful
>

>
>

I notice that cocos2dx supports pluginx
but pluginx does not support jsb now.
I wonder if I can do using the same way above to create jsb for pluginx

What’s error?

Simon Pan wrote:

john w wrote:
> Simon Pan wrote:
> >
>
> Thanks again
> in my case
> It compiles and reports error
>
> > …. …. Processing section testandroid
> >
> >
> > Errors in parsing headers:
> > 1. <severity = Warning,
> > location = <SourceLocation file None, line 0, column 0>,
> > details = “argument unused during compilation: ‘-nostdinc++’”>
> > 2. <severity = Fatal,
> > location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
> > details = “‘string’ file not found”>
> >
> > Found errors - can not continue
> > Fatal error in parsing headers
>
> But it generates the jsb files
>
> confuse why and if the jsb files are useful
>
>
>
>
> I notice that cocos2dx supports pluginx
> but pluginx does not support jsb now.
> I wonder if I can do using the same way above to create jsb for pluginx
>
What’s error?

#include

it said

location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,

> > details = “‘string’ file not found”

Thank you very much for Your tutorial Simon. I’ll try it in next week!

john w wrote:

Simon Pan wrote:
> john w wrote:
> > Simon Pan wrote:
> > >
> >
> > Thanks again
> > in my case
> > It compiles and reports error
> >
> > > …. …. Processing section testandroid
> > >
> > >
> > > Errors in parsing headers:
> > > 1. <severity = Warning,
> > > location = <SourceLocation file None, line 0, column 0>,
> > > details = “argument unused during compilation: ‘-nostdinc++’”>
> > > 2. <severity = Fatal,
> > > location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
> > > details = “‘string’ file not found”>
> > >
> > > Found errors - can not continue
> > > Fatal error in parsing headers
> >
> > But it generates the jsb files
> >
> > confuse why and if the jsb files are useful
> >
> >
> >
> >
> > I notice that cocos2dx supports pluginx
> > but pluginx does not support jsb now.
> > I wonder if I can do using the same way above to create jsb for pluginx
>
> What’s error?
>
#include
>
it said
>
location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
> > > details = “‘string’ file not found”

It seems your directory architecture’s problem I guess.
can’t find where is your cocos2d library.

Have you set entire environment path in your mac?

john w wrote:

Simon Pan wrote:
> john w wrote:
> > Simon Pan wrote:
> > >
> >
> > Thanks again
> > in my case
> > It compiles and reports error
> >
> > > …. …. Processing section testandroid
> > >
> > >
> > > Errors in parsing headers:
> > > 1. <severity = Warning,
> > > location = <SourceLocation file None, line 0, column 0>,
> > > details = “argument unused during compilation: ‘-nostdinc++’”>
> > > 2. <severity = Fatal,
> > > location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
> > > details = “‘string’ file not found”>
> > >
> > > Found errors - can not continue
> > > Fatal error in parsing headers
> >
> > But it generates the jsb files
> >
> > confuse why and if the jsb files are useful
> >
> >
> >
> >
> > I notice that cocos2dx supports pluginx
> > but pluginx does not support jsb now.
> > I wonder if I can do using the same way above to create jsb for pluginx
>
> What’s error?
>
#include
>
it said
>
location = <SourceLocation file ‘/Users/myname/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/simple_test/simple_class.h’, line 4, column 10>,
> > > details = “‘string’ file not found”

maybe this is answer for you:
[[http://cocos2d.cocoachina.com/bbs/forum.php?mod=viewthread&tid=10632]]

Here also have answer:
http://www.cocos2d-x.org/boards/20/topics/24515

Simon Pan wrote:

maybe this is answer for you:

It is ok now. :slight_smile: Thanks again for your patience and clear tutorial

before this I added jsb manually which also works!
but it needs more thing to do(needs to add/modify 4~6 files)

and you asked:

Can you give the .ini file sample which can include “cocos2d.h”,

James Chen has replied at

http://cocos2d-x.org/forums/20/topics/31124

add

cocos_headers = Is/cocos2dx/include -Is/cocos2dx/platformIs/cocos2dx/platform/android -I(cocosdir)s/cocos2dx ~~I%s/cocos2dx/kazmath/include
cocos_flags =~~DANDROID -DCOCOS2D_JAVASCRIPT

to .ini

john w wrote:

Simon Pan wrote:
>
> maybe this is answer for you:
>
>
It is ok now. :slight_smile: Thanks again for your patience and clear tutorial
>
before this I added jsb manually which also works!
but it needs more thing to do(needs to add/modify 4~6 files)
>
and you asked:
> Can you give the .ini file sample which can include “cocos2d.h”,
>
James Chen has replied at
>
http://cocos2d-x.org/forums/20/topics/31124
>
add
>
> cocos_headers = Is/cocos2dx/include -Is/cocos2dx/platformIs/cocos2dx/platform/android -I(cocosdir)s/cocos2dx ~~I%s/cocos2dx/kazmath/include
> cocos_flags =~~DANDROID -DCOCOS2D_JAVASCRIPT
>
to .ini

Yes,
you are correct.
I found this answer recently as well.
Now I have to do is study how to send parameter from javascript like ccNode to C++ with JSB-binding.

Simon Pan wrote:

> > cocos_headers = Is/cocos2dx/include -Is/cocos2dx/platformIs/cocos2dx/platform/android -I(cocosdir)s/cocos2dx ~~Is/cocos2dx/kazmath/include
> > cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT
>
> to .ini

Yes,
you are correct.
I found this answer recently as well.
Now I have to do is study how to send parameter from javascript like ccNode to C++ with JSB-binding.

unfortunetly. I can not make it work for the case that include cocos2d.h

I add the code below to test.ini

cocos_headers = -Is/cocos2dx/include~~Is/cocos2dx/platform -I(cocosdir)s/cocos2dx/platform/android ~~Is/cocos2dx -Is/cocos2dx/kazmath/include
cocos_flags =~~DANDROID -DCOCOS2D_JAVASCRIPT

but it reports

Errors in parsing headers:

  1. <severity = Warning,
    location = <SourceLocation file None, line 0, column 0>,
    details = “argument unused during compilation: ‘-nostdinc++’”>
  2. <severity = Fatal,
    location = <SourceLocation file ‘/Users/zhengwang/cd-standard/cocos2d-x-3.0alpha0-pre/tools/bindings-generator/test/socketio/socketio.h’, line 63, column 10>,
    details = “‘cocos2d.h’ file not found”>

Post my setting.

test.ini

[testandroid]
name = BindingCode
prefix = autogencustombindings
classes = WebOperator PictureOperator FileOperator Network

# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = utility

cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I../../cocos2dx/platform/ios -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/scripting/javascript/bindings -I%(cocosdir)s/extensions


cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT

clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
clang_flags = -nostdinc -x c++ -std=c++11

cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common

binding_headers = -I%(cocosdir)s/scripting/javascript/bindings -I%(cocosdir)s/scripting/javascript/spidermonkey-ios/include -I%(cocosdir)s/scripting/javascript/spidermonkey-ios/include/js

extra_arguments = -D_SIZE_T_DEFINED_ -nostdinc -nostdinc++ -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(cxxgenerator_headers)s %(cocos_headers)s %(cocos_flags)s %(clang_headers)s %(clang_flags)s -I%(projectpath)s/libs/JSBinding/WebManager %(binding_headers)s

headers = %(cocosdir)s/cocos2dx/include/cocos2d.h %(cocosdir)s/CocosDenshion/include/SimpleAudioEngine.h %(projectpath)s/libs/JSBinding/BindingCode/WebOperator.h %(projectpath)s/libs/JSBinding/BindingCode/PictureOperator.h %(projectpath)s/libs/JSBinding/BindingCode/FileOperator.h %(projectpath)s/libs/JSBinding/BindingCode/Network.h


remove_prefix =
skip = 
base_objects =
abstract_classes =
classes_have_type_info = no
rename =
rename_functions =
rename_classes =
# classes for which there will be no "parent" lookup
classes_have_no_parents =

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip =

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = yes

user.cfg

PYTHON_BIN=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

userconf.ini

[DEFAULT]
androidndkdir=/Users/lp43/Android/adt-bundle-mac/android-ndk-r8e
clangllvmdir=/Users/lp43/bin/clang+llvm-3.3
cxxgeneratordir=/Users/lp43/cocos2d-x3.0/tools/bindings-generator
projectpath=/Users/lp43/cocos2d-x3.0/projects/ChallengeJS
cocosdir=/Users/lp43/cocos2d-x3.0

test.sh

# options

usage(){
cat << EOF
usage: $0 [options]

Test the C++ <-> JS bindings generator

OPTIONS:
-h  this help

Dependencies :
PYTHON_BIN
CLANG_ROOT
NDK_ROOT

Define this to run from a different directory
CXX_GENERATOR_ROOT

EOF
}

while getopts "dvh" OPTION; do
case "$OPTION" in
d)
debug=1
;;
v)
verbose=1
;;
h)
usage
exit 0
;;
esac
done

# exit this script if any commmand fails
set -e

# find current dir
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# read user.cfg if it exists and is readable

_CFG_FILE=$(dirname "$0")"/user.cfg"
if [ -e "$_CFG_FILE" ]
then
    [ -r "$_CFG_FILE" ] || die "Fatal Error: $_CFG_FILE exists but is unreadable"
    . "$_CFG_FILE"
fi

# paths

if [ -z "${COCOS2DX_ROOT+aaa}" ]; then
COCOS2DX_ROOT="$DIR/../../../../"
fi

if [ -z "${NDK_ROOT+aaa}" ]; then
# ... if NDK_ROOT is not set, use "$HOME/bin/android-ndk"
    NDK_ROOT="$HOME/bin/android-ndk"
fi

if [ -z "${CLANG_ROOT+aaa}" ]; then
# ... if CLANG_ROOT is not set, use "$HOME/bin/clang+llvm-3.3"
    CLANG_ROOT="$HOME/bin/clang+llvm-3.3"
fi

if [ -z "${PYTHON_BIN+aaa}" ]; then
# ... if PYTHON_BIN is not set, use "/usr/bin/python2.7"
    PYTHON_BIN="/usr/bin/python2.7"
fi

# paths with defaults hardcoded to relative paths

if [ -z "${CXX_GENERATOR_ROOT+aaa}" ]; then
    CXX_GENERATOR_ROOT="$DIR/.."
fi

if [ -z "${TOJS_ROOT+aaa}" ]; then
TO_JS_ROOT="$COCOS2DX_ROOT/tools/tojs"
fi

echo "CLANG_ROOT: $CLANG_ROOT"
echo "NDK_ROOT: $NDK_ROOT"
echo "CXX_GENERATOR_ROOT: $CXX_GENERATOR_ROOT"
echo "PYTHON_BIN: $PYTHON_BIN"
echo "COCOS2DX_ROOT: $COCOS2DX_ROOT"
echo "TO_JS_ROOT: $TO_JS_ROOT"

# Generate bindings for cocos2dx

#echo "Generating bindings for cocos2dx..."
#set -x
#LD_LIBRARY_PATH=${CLANG_ROOT}/lib $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TO_JS_ROOT}/cocos2dx.ini -s cocos2d-x -t spidermonkey -o ${COCOS2DX_ROOT}/scripting/auto-generated/js-bindings  -n jsb_cocos2dx_auto

#echo "Generating bindings for cocos2dx_extension..."
#LD_LIBRARY_PATH=${CLANG_ROOT}/lib $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TO_JS_ROOT}/cocos2dx_extension.ini -s cocos2dx_extension -t spidermonkey -o ${COCOS2DX_ROOT}/scripting/auto-generated/js-bindings -n jsb_cocos2dx_extension_auto

# Generate bindings for simpletest using Android's system headers
echo "Generating bindings for bindingCode with Android headers..."
set -x
LD_LIBRARY_PATH=${CLANG_ROOT}/lib $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${COCOS2DX_ROOT}/projects/ChallengeJS/libs/JSBinding/test.ini -t spidermonkey -s testandroid -o ./BindingCode

my system environment setting: .bash_profile

export PATH=${PATH}:/Users/lp43/Android/adt-bundle-mac/sdk/platform-tools/
export PATH=${PATH}:/Users/lp43/Android/adt-bundle-mac/sdk/tools/
export PATH=${PATH}:/Users/lp43/cocos2d-x3.0/projects/ChallengeJS/proj.android/
export PATH=${PATH}:/Users/lp43/cocos2d-x3.0/projects/ChallengeJS/libs/JSBinding/
export PATH=${PATH}:/usr/local/mysql-5.6.11-osx10.7-x86_64/bin/
export PATH=${PATH}:/usr/local/apache-tomcat-7.0.40/bin/
export NDK_ROOT=/Users/lp43/Android/adt-bundle-mac/android-ndk-r8e/
export COCOS2DX_HOME=/Users/lp43/cocos2d-x3.0/
export TOMCAT_HOME=/usr/local/apache-tomcat-7.0.40/
export PATH=$PATH:/opt/local/bin
export MANPATH=$MANPATH:/opt/local/share/man
export INFOPATH=$INFOPATH:/opt/local/share/info
export CXX_GENERATOR_ROOT=/Users/lp43/cocos2d-x3.0/tools/bindings-generator
export BINDING=/Users/lp43/cocos2d-x3.0/projects/ChallengeJS/libs/JSBinding/
#
# Your previous /Users/lp43/.bash_profile file was backed up as /Users/lp43/.bash_profile.macports-saved_2013-09-07_at_17:51:53
##

# MacPorts Installer addition on 2013-09-07_at_17:51:53: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

Check what is different with you.

Simon Pan wrote:

Check what is different with you.

finally it works:) thanks again for your patience

one thing is that I noticed that keyword
[testandroid]
cocos_headers = ~~I%s/cocos2dx/platform/android
cocos_flags =~~DANDROID -DCOCOS2D_JAVASCRIPT

does it compile only for android or ios android both?

I don’t know,
but it works on ios as well.:wink:

I included a button to launch a URL in the native device browser in my JNI demo project: https://github.com/ElliotMebane/Cocos2d-x_CPP_to_Java_via_JNI_Samples

And, I also just found a SonarSystems video tutorial that implements openURL in both iOS and Android:

His approach is slightly different.