how we can open a link click on a button in cocos2d-x javascirt.
To open a link, you need to call native methods on each platform.
For Android ,it’s very easy. You can use jsb.reflection to call java static method.
modify AppActivity:
public class AppActivity extends Cocos2dxActivity {
private static AppActivity app = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = this;
}
public static void openUrl(final String url)
{
app.runOnUiThread(new Runnable(){
@Override
public void run() {
Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
getContext().startActivity(it);
}});
}
}
and call openUrl by:
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "openUrl", "(Ljava/lang/String;)V", "http://www.cocos2d-x.org");
ReferenceError: jsb is not defined
If you use 3.0beta , it’s cc.reflection. And it was renamed to jsb.reflection in 3.0rc0.
3.0rc0 changelog
I am using 2.2.5, will it work ?
No, I’m sorry it won’t.
Thank you for your help! I’m porting some apps from 2.x to 3.x. In 2.x there was a cc.Application.openURL I think. Was it removed on 3.x? Can I file a bug report to get it back somewhere?
On Android we can use reflection and get the openURL back. Any solution for iOS?
Thanks a lot!
You can also use reflection for IOS such as :
jsb.reflection.callStaticMethod("NativeOcClass","openWebView:","http://www.google.com");
At your OC class you can write a static method at your oc class
+(void)openWebView:(NSString *)url{
// open a webview
}
You can find a example at test-cases ReflectionTest.js at branch master on github.
Native oc example is at this path
Excelent! I guess it’s not included on v3.0rc0 though, I have to get the latest source from github master. Thanks a lot!
The Obj-c reflection will be included in RC2
jsb.reflection how to call js from objective-c? thank you
How can I call openURL from JS for Windows Phone?