Fixed sets the page's base URL for WebView doesn't work.

This commit is contained in:
Wenhai Lin 2015-01-23 18:30:12 +08:00
parent 0c8d10b8dd
commit 81198583f1
5 changed files with 69 additions and 27 deletions

View File

@ -54,7 +54,6 @@ public class Cocos2dxWebViewHelper {
onJsCallback(index, message);
}
@SuppressWarnings("unused")
public static int createWebView() {
final int index = viewTag;
sCocos2dxActivity.runOnUiThread(new Runnable() {
@ -72,7 +71,6 @@ public class Cocos2dxWebViewHelper {
return viewTag++;
}
@SuppressWarnings("unused")
public static void removeWebView(final int index) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
@ -86,7 +84,6 @@ public class Cocos2dxWebViewHelper {
});
}
@SuppressWarnings("unused")
public static void setVisible(final int index, final boolean visible) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@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) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
@ -112,7 +108,6 @@ public class Cocos2dxWebViewHelper {
});
}
@SuppressWarnings("unused")
public static void setJavascriptInterfaceScheme(final int index, final String scheme) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@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) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
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 htmlString, final String mimeType, final String encoding) {
public static void loadHTMLString(final int index, final String data, final String baseUrl) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
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) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
@ -164,7 +156,6 @@ public class Cocos2dxWebViewHelper {
});
}
@SuppressWarnings("unused")
public static void loadFile(final int index, final String filePath) {
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
CocosPlayClient.updateAssets(filePath);
@ -212,7 +203,6 @@ public class Cocos2dxWebViewHelper {
return task.get();
}
@SuppressWarnings("unused")
public static boolean canGoBack(final int index) {
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
@ -230,7 +220,6 @@ public class Cocos2dxWebViewHelper {
}
}
@SuppressWarnings("unused")
public static boolean canGoForward(final int index) {
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
@ -248,7 +237,6 @@ public class Cocos2dxWebViewHelper {
}
}
@SuppressWarnings("unused")
public static void goBack(final int index) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
@ -261,7 +249,6 @@ public class Cocos2dxWebViewHelper {
});
}
@SuppressWarnings("unused")
public static void goForward(final int index) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
@ -274,7 +261,6 @@ public class Cocos2dxWebViewHelper {
});
}
@SuppressWarnings("unused")
public static void evaluateJS(final int index, final String js) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
@ -287,7 +273,6 @@ public class Cocos2dxWebViewHelper {
});
}
@SuppressWarnings("unused")
public static void setScalesPageToFit(final int index, final boolean scalesPageToFit) {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override

View File

@ -72,7 +72,7 @@ public:
* @param string The content for the main page.
* @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.

View File

@ -41,6 +41,39 @@
#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" {
/*
* 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 jMIMEType = t.env->NewStringUTF(MIMEType.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->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) {
// LOGD("error: %s,%d",__func__,__LINE__);
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 jBaseURL = t.env->NewStringUTF(baseURL.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL,nullptr);
jstring jBaseURL = t.env->NewStringUTF(getFixedBaseUrl(baseURL).c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL);
t.env->DeleteLocalRef(jString);
t.env->DeleteLocalRef(jBaseURL);

View File

@ -34,6 +34,30 @@
#include "platform/CCFileUtils.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
@property (nonatomic) std::function<bool(std::string url)> shouldStartLoading;
@property (nonatomic) std::function<void(std::string url)> didFinishLoading;
@ -136,11 +160,11 @@
[self.uiWebView loadData:[NSData dataWithBytes:data.c_str() length:data.length()]
MIMEType:@(MIMEType.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 {
[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 {

View File

@ -124,11 +124,11 @@ bool WebViewTest::init()
Button *loadHTMLBtn = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
loadHTMLBtn->setTitleText("Load HTML");
loadHTMLBtn->setTitleText("Load Data");
loadHTMLBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
loadHTMLBtn->getContentSize().width/2 + 10,0 ));
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);