1. 修改“cocos2dx\platform\win32”下的CCApplication.h和CCApplication.cpp,添加
// CCApplication.h
void openURL(const char* pszUrl);
// CCApplication.cpp
void CCApplication::openURL(const char* pszUrl)
{
ShellExecuteA(NULL, "open", pszUrl, NULL, NULL, SW_SHOWNORMAL);
}
2. 修改“cocos2dx\platform\Android”下的CCApplication.h和CCApplication.cpp,添加
//CCApplication.h
void openURL(const char* pszUrl);
//CCApplication.cpp
void CCApplication::openURL(const char* pszUrl)
{
JniMethodInfo minfo;
if(JniHelper::getStaticMethodInfo(minfo,
"org/cocos2dx/application/ApplicationDemo",
"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);
}
}
3.修改“cocos2dx\platform\android\Java\src\org\cocos2dx\lib”下的Cocos2dxActivity.java,添加如下语句:
private static Activity me = null;
protected void onCreate(final Bundle savedInstanceState) {
...
me = this;
...
}
public static void openURL(String url)
{
try {
Uri uri = Uri.parse(url);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
sContext.startActivity(it);
}
catch(Exception e) {
e.printStackTrace();
}
}
4.修改“\cocos2dx\platform\iOS\”下的CCApplication.h和CCApplication.mm,添加如下语句:
// CCApplication.h
void openURL(const char* pszUrl);
// CCApplication.mm
void CCApplication::openURL(const char* pszUrl)
{
NSString *str = [NSString stringWithCString:(pszUrl) encoding:NSUTF8StringEncoding]; //char* 转NSString*
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //网址包含中文字符,加上这句话
NSURL *url = [NSURL URLWithString:str]; //NSString转NSURL
[[UIApplication sharedApplication] openURL:url];
}
5.要在lua层使用,在tools/tolua++路径下
//CCApplication.pkg
void openURL(const char* pszUrl);
//LuaCocos2d.cpp
local function openUrlWithDefaultBrowser( addr )
if EDFLAGIOS or EDFLAGANDROID then
if CCApplication.openURL then
CCApplication:sharedApplication():openURL("http://www.baidu")
end
end
end
ed.openUrlWithDefaultBrowser = openUrlWithDefaultBrowser
1. 修改“cocos2dx\platform\win32”下的CCApplication.h和CCApplication.cpp,添加
// CCApplication.h
void openURL(const char* pszUrl);
// CCApplication.cpp
void CCApplication::openURL(const char* pszUrl)
{
ShellExecuteA(NULL, "open", pszUrl, NULL, NULL, SW_SHOWNORMAL);
}
2. 修改“cocos2dx\platform\Android”下的CCApplication.h和CCApplication.cpp,添加
//CCApplication.h
void openURL(const char* pszUrl);
//CCApplication.cpp
void CCApplication::openURL(const char* pszUrl)
{
JniMethodInfo minfo;
if(JniHelper::getStaticMethodInfo(minfo,
"org/cocos2dx/application/ApplicationDemo",
"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);
}
}
3.修改“cocos2dx\platform\android\Java\src\org\cocos2dx\lib”下的Cocos2dxActivity.java,添加如下语句:
private static Activity me = null;
protected void onCreate(final Bundle savedInstanceState) {
...
me = this;
...
}
public static void openURL(String url)
{
try {
Uri uri = Uri.parse(url);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
sContext.startActivity(it);
}
catch(Exception e) {
e.printStackTrace();
}
}
4.修改“\cocos2dx\platform\iOS\”下的CCApplication.h和CCApplication.mm,添加如下语句:
// CCApplication.h
void openURL(const char* pszUrl);
// CCApplication.mm
void CCApplication::openURL(const char* pszUrl)
{
NSString *str = [NSString stringWithCString:(pszUrl) encoding:NSUTF8StringEncoding]; //char* 转NSString*
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //网址包含中文字符,加上这句话
NSURL *url = [NSURL URLWithString:str]; //NSString转NSURL
[[UIApplication sharedApplication] openURL:url];
}
5.要在lua层使用,在tools/tolua++路径下
//CCApplication.pkg
void openURL(const char* pszUrl);
//LuaCocos2d.cpp
local function openUrlWithDefaultBrowser( addr )
if EDFLAGIOS or EDFLAGANDROID then
if CCApplication.openURL then
CCApplication:sharedApplication():openURL("http://www.baidu")
end
end
end
ed.openUrlWithDefaultBrowser = openUrlWithDefaultBrowser