Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cocos2dx/platform/android/CCApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ void CCApplication::setAnimationInterval(double interval)
}
}

void CCApplication::openURL(const char* pszUrl)
{

JniMethodInfo minfo;

if(JniHelper::getStaticMethodInfo(minfo,
"org/cocos2dx/lib/Cocos2dxActivity",
"openURL",
"(Ljava/lang/String;)V"))
{
jstring StringArg1 = minfo.env->NewStringUTF(pszUrl);
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, StringArg1);
minfo.env->DeleteLocalRef(StringArg1);
minfo.env->DeleteLocalRef(minfo.classID);
}
}

//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions cocos2dx/platform/android/CCApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class CC_DLL CCApplication : public CCApplicationProtocol
*/
int run();

/**
@brief Open URL with device's default internal browser.
*/
void openURL(const char* pszUrl);

/**
@brief Get current application instance.
@return Current application instance pointer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ public void init() {
// Set framelayout as the content view
setContentView(framelayout);
}


public static void openURL(String url) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably here you should add Intent.FLAG_ACTIVITY_NEW_TASK. Because without this one sometimes it crashes on my Nexus4

my code looks like:

// Opens URL as the new activity
public static void openURL(String url) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setData(Uri.parse(url));
    sContext.startActivity(i);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also exceptions for invalud parsing uri should be handled ?

me.startActivity(i);
}

public Cocos2dxGLSurfaceView onCreateView() {
return new Cocos2dxGLSurfaceView(this);
}
Expand Down
7 changes: 6 additions & 1 deletion cocos2dx/platform/ios/CCApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class CC_DLL CCApplication : public CCApplicationProtocol
*/
int run();

/**
/**
@brief Open URL with device's default internal browser.
*/
void openURL(const char* pszUrl);

/**
@brief Get the current application instance.
@return Current application instance pointer.
@js getInstance
Expand Down
7 changes: 7 additions & 0 deletions cocos2dx/platform/ios/CCApplication.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ of this software and associated documentation files (the "Software"), to deal
return 0;
}

void CCApplication::openURL(const char* pszUrl)
{
NSString *msg = [NSString stringWithCString:pszUrl encoding:NSASCIIStringEncodeing];
NSURL *nsUrl = [NSURL URLWithString:msg];
[[UIApplication sharedApplication] openURL:nsUrl];
}

void CCApplication::setAnimationInterval(double interval)
{
[[CCDirectorCaller sharedDirectorCaller] setAnimationInterval: interval ];
Expand Down
22 changes: 22 additions & 0 deletions scripting/javascript/bindings/cocos2d_specifics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,28 @@ JSBool js_cocos2dx_setBlendFunc(JSContext *cx, uint32_t argc, jsval *vp)
return JS_FALSE;
}

JSBool js_cocos2dx_CCApplication_openURL(JSContext *cx, uint32_t argc, jsval *vp)
{
jsval *argv = JS_ARGV(cx, vp);
JSBool ok = JS_TRUE;
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::CCApplication* cobj = (cocos2d::CCApplication *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, JS_FALSE, "Invalid Native Object");

if (argc == 1) {
std::string arg0;
ok &= jsval_to_std_string(cx, argv[0], &arg0);
JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments");
cobj->openURL(arg0.c_str());
}
else {
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
}

return JS_FALSE;
}

JSBool js_cocos2dx_CCSprite_setBlendFunc(JSContext *cx, uint32_t argc, jsval *vp)
{
return js_cocos2dx_setBlendFunc<CCSprite>(cx, argc, vp);
Expand Down