mirror of https://github.com/axmolengine/axmol.git
Fixed sets the page's base URL for WebView doesn't work.
This commit is contained in:
parent
0c8d10b8dd
commit
81198583f1
|
@ -54,7 +54,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
onJsCallback(index, message);
|
onJsCallback(index, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static int createWebView() {
|
public static int createWebView() {
|
||||||
final int index = viewTag;
|
final int index = viewTag;
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
|
@ -72,7 +71,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
return viewTag++;
|
return viewTag++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void removeWebView(final int index) {
|
public static void removeWebView(final int index) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -86,7 +84,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void setVisible(final int index, final boolean visible) {
|
public static void setVisible(final int index, final boolean visible) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -99,7 +96,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) {
|
public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -112,7 +108,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void setJavascriptInterfaceScheme(final int index, final String scheme) {
|
public static void setJavascriptInterfaceScheme(final int index, final String scheme) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -125,33 +120,30 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void loadData(final int index, final String data, final String mimeType, final String encoding, final String baseURL) {
|
public static void loadData(final int index, final String data, final String mimeType, final String encoding, final String baseURL) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Cocos2dxWebView webView = webViews.get(index);
|
Cocos2dxWebView webView = webViews.get(index);
|
||||||
if (webView != null) {
|
if (webView != null) {
|
||||||
webView.loadDataWithBaseURL(baseURL, data, mimeType, encoding, null);
|
webView.loadDataWithBaseURL(baseURL, data, mimeType, encoding, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
public static void loadHTMLString(final int index, final String data, final String baseUrl) {
|
||||||
public static void loadHTMLString(final int index, final String htmlString, final String mimeType, final String encoding) {
|
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Cocos2dxWebView webView = webViews.get(index);
|
Cocos2dxWebView webView = webViews.get(index);
|
||||||
if (webView != null) {
|
if (webView != null) {
|
||||||
webView.loadData(htmlString, mimeType, encoding);
|
webView.loadDataWithBaseURL(baseUrl, data, null, null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void loadUrl(final int index, final String url) {
|
public static void loadUrl(final int index, final String url) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -164,7 +156,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void loadFile(final int index, final String filePath) {
|
public static void loadFile(final int index, final String filePath) {
|
||||||
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
||||||
CocosPlayClient.updateAssets(filePath);
|
CocosPlayClient.updateAssets(filePath);
|
||||||
|
@ -212,7 +203,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
return task.get();
|
return task.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static boolean canGoBack(final int index) {
|
public static boolean canGoBack(final int index) {
|
||||||
Callable<Boolean> callable = new Callable<Boolean>() {
|
Callable<Boolean> callable = new Callable<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -230,7 +220,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static boolean canGoForward(final int index) {
|
public static boolean canGoForward(final int index) {
|
||||||
Callable<Boolean> callable = new Callable<Boolean>() {
|
Callable<Boolean> callable = new Callable<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -248,7 +237,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void goBack(final int index) {
|
public static void goBack(final int index) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -261,7 +249,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void goForward(final int index) {
|
public static void goForward(final int index) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -274,7 +261,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void evaluateJS(final int index, final String js) {
|
public static void evaluateJS(final int index, final String js) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -287,7 +273,6 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void setScalesPageToFit(final int index, final boolean scalesPageToFit) {
|
public static void setScalesPageToFit(final int index, final boolean scalesPageToFit) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -72,7 +72,7 @@ public:
|
||||||
* @param string The content for the main page.
|
* @param string The content for the main page.
|
||||||
* @param baseURL The base URL for the content.
|
* @param baseURL The base URL for the content.
|
||||||
*/
|
*/
|
||||||
void loadHTMLString(const std::string &string, const std::string &baseURL);
|
void loadHTMLString(const std::string &string, const std::string &baseURL = "");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the given URL.
|
* Loads the given URL.
|
||||||
|
|
|
@ -41,6 +41,39 @@
|
||||||
|
|
||||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__)
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__)
|
||||||
|
|
||||||
|
static const std::string s_defaultBaseUrl = "file:///android_asset/";
|
||||||
|
static const std::string s_sdRootBaseUrl = "file://";
|
||||||
|
|
||||||
|
static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||||
|
{
|
||||||
|
std::string fixedBaseUrl;
|
||||||
|
if (baseUrl.empty())
|
||||||
|
{
|
||||||
|
fixedBaseUrl = s_defaultBaseUrl;
|
||||||
|
}
|
||||||
|
else if (baseUrl.find(s_sdRootBaseUrl) != std::string::npos)
|
||||||
|
{
|
||||||
|
fixedBaseUrl = baseUrl;
|
||||||
|
}
|
||||||
|
else if (baseUrl.c_str()[0] != '/') {
|
||||||
|
if(baseUrl.find("assets/") == 0) {
|
||||||
|
fixedBaseUrl = s_defaultBaseUrl + baseUrl.c_str()[7];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fixedBaseUrl = s_defaultBaseUrl + baseUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fixedBaseUrl = s_sdRootBaseUrl + baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fixedBaseUrl.c_str()[fixedBaseUrl.length() - 1] != '/') {
|
||||||
|
fixedBaseUrl += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixedBaseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
/*
|
/*
|
||||||
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
|
* Class: org_cocos2dx_lib_Cocos2dxWebViewHelper
|
||||||
|
@ -144,7 +177,7 @@ void loadDataJNI(const int index, const std::string &data, const std::string &MI
|
||||||
jstring jData = t.env->NewStringUTF(data.c_str());
|
jstring jData = t.env->NewStringUTF(data.c_str());
|
||||||
jstring jMIMEType = t.env->NewStringUTF(MIMEType.c_str());
|
jstring jMIMEType = t.env->NewStringUTF(MIMEType.c_str());
|
||||||
jstring jEncoding = t.env->NewStringUTF(encoding.c_str());
|
jstring jEncoding = t.env->NewStringUTF(encoding.c_str());
|
||||||
jstring jBaseURL = t.env->NewStringUTF(baseURL.c_str());
|
jstring jBaseURL = t.env->NewStringUTF(getFixedBaseUrl(baseURL).c_str());
|
||||||
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jData, jMIMEType, jEncoding, jBaseURL);
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jData, jMIMEType, jEncoding, jBaseURL);
|
||||||
|
|
||||||
t.env->DeleteLocalRef(jData);
|
t.env->DeleteLocalRef(jData);
|
||||||
|
@ -158,10 +191,10 @@ void loadDataJNI(const int index, const std::string &data, const std::string &MI
|
||||||
void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) {
|
void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) {
|
||||||
// LOGD("error: %s,%d",__func__,__LINE__);
|
// LOGD("error: %s,%d",__func__,__LINE__);
|
||||||
cocos2d::JniMethodInfo t;
|
cocos2d::JniMethodInfo t;
|
||||||
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) {
|
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;)V")) {
|
||||||
jstring jString = t.env->NewStringUTF(string.c_str());
|
jstring jString = t.env->NewStringUTF(string.c_str());
|
||||||
jstring jBaseURL = t.env->NewStringUTF(baseURL.c_str());
|
jstring jBaseURL = t.env->NewStringUTF(getFixedBaseUrl(baseURL).c_str());
|
||||||
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL,nullptr);
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL);
|
||||||
|
|
||||||
t.env->DeleteLocalRef(jString);
|
t.env->DeleteLocalRef(jString);
|
||||||
t.env->DeleteLocalRef(jBaseURL);
|
t.env->DeleteLocalRef(jBaseURL);
|
||||||
|
|
|
@ -34,6 +34,30 @@
|
||||||
#include "platform/CCFileUtils.h"
|
#include "platform/CCFileUtils.h"
|
||||||
#include "ui/UIWebView.h"
|
#include "ui/UIWebView.h"
|
||||||
|
|
||||||
|
static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||||
|
{
|
||||||
|
std::string fixedBaseUrl;
|
||||||
|
if (baseUrl.empty() || baseUrl.c_str()[0] != '/') {
|
||||||
|
fixedBaseUrl = [[[NSBundle mainBundle] resourcePath] UTF8String];
|
||||||
|
fixedBaseUrl += "/";
|
||||||
|
fixedBaseUrl += baseUrl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fixedBaseUrl = baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t pos = 0;
|
||||||
|
while ((pos = fixedBaseUrl.find(" ")) != std::string::npos) {
|
||||||
|
fixedBaseUrl.replace(pos, 1, "%20");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fixedBaseUrl.c_str()[fixedBaseUrl.length() - 1] != '/') {
|
||||||
|
fixedBaseUrl += "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixedBaseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
@interface UIWebViewWrapper : NSObject
|
@interface UIWebViewWrapper : NSObject
|
||||||
@property (nonatomic) std::function<bool(std::string url)> shouldStartLoading;
|
@property (nonatomic) std::function<bool(std::string url)> shouldStartLoading;
|
||||||
@property (nonatomic) std::function<void(std::string url)> didFinishLoading;
|
@property (nonatomic) std::function<void(std::string url)> didFinishLoading;
|
||||||
|
@ -136,11 +160,11 @@
|
||||||
[self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()]
|
[self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()]
|
||||||
MIMEType:@(MIMEType.c_str())
|
MIMEType:@(MIMEType.c_str())
|
||||||
textEncodingName:@(encodingName.c_str())
|
textEncodingName:@(encodingName.c_str())
|
||||||
baseURL:[NSURL URLWithString:@(baseURL.c_str())]];
|
baseURL:[NSURL URLWithString:@(getFixedBaseUrl(baseURL).c_str())]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL {
|
- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL {
|
||||||
[self.uiWebView loadHTMLString:@(string.c_str()) baseURL:[NSURL URLWithString:@(baseURL.c_str())]];
|
[self.uiWebView loadHTMLString:@(string.c_str()) baseURL:[NSURL URLWithString:@(getFixedBaseUrl(baseURL).c_str())]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)loadUrl:(const std::string &)urlString {
|
- (void)loadUrl:(const std::string &)urlString {
|
||||||
|
|
|
@ -124,11 +124,11 @@ bool WebViewTest::init()
|
||||||
|
|
||||||
Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png",
|
Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png",
|
||||||
"cocosui/animationbuttonpressed.png");
|
"cocosui/animationbuttonpressed.png");
|
||||||
loadHTMLBtn->setTitleText("Load HTML");
|
loadHTMLBtn->setTitleText("Load Data");
|
||||||
loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
|
loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
|
||||||
loadHTMLBtn->getContentSize().width/2 + 10,0 ));
|
loadHTMLBtn->getContentSize().width/2 + 10,0 ));
|
||||||
loadHTMLBtn->addClickEventListener([=](Ref*){
|
loadHTMLBtn->addClickEventListener([=](Ref*){
|
||||||
_webView->loadHTMLString("<body style=\"font-size:50px;\">Hello World</body>","text/html");
|
_webView->loadHTMLString("<body style=\"font-size:50px;\">Hello World <img src=\"Icon.png\"/> </body>","Images/");
|
||||||
});
|
});
|
||||||
this->addChild(loadHTMLBtn);
|
this->addChild(loadHTMLBtn);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue