From 81198583f12813c32d0fb4a67d334139fcc4ae6d Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Fri, 23 Jan 2015 18:30:12 +0800 Subject: [PATCH] Fixed sets the page's base URL for WebView doesn't work. --- .../cocos2dx/lib/Cocos2dxWebViewHelper.java | 21 ++-------- cocos/ui/UIWebView.h | 2 +- cocos/ui/UIWebViewImpl-android.cpp | 41 +++++++++++++++++-- cocos/ui/UIWebViewImpl-ios.mm | 28 ++++++++++++- .../UIWebViewTest/UIWebViewTest.cpp | 4 +- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java index 2091ec74e7..5160f680db 100755 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxWebViewHelper.java @@ -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 callable = new Callable() { @Override @@ -230,7 +220,6 @@ public class Cocos2dxWebViewHelper { } } - @SuppressWarnings("unused") public static boolean canGoForward(final int index) { Callable callable = new Callable() { @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 diff --git a/cocos/ui/UIWebView.h b/cocos/ui/UIWebView.h index 3874cb4b02..bea6dfd29d 100644 --- a/cocos/ui/UIWebView.h +++ b/cocos/ui/UIWebView.h @@ -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. diff --git a/cocos/ui/UIWebViewImpl-android.cpp b/cocos/ui/UIWebViewImpl-android.cpp index 813633d3db..ae553ad456 100644 --- a/cocos/ui/UIWebViewImpl-android.cpp +++ b/cocos/ui/UIWebViewImpl-android.cpp @@ -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); diff --git a/cocos/ui/UIWebViewImpl-ios.mm b/cocos/ui/UIWebViewImpl-ios.mm index 0c07462eae..f1dc8d993a 100644 --- a/cocos/ui/UIWebViewImpl-ios.mm +++ b/cocos/ui/UIWebViewImpl-ios.mm @@ -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 shouldStartLoading; @property (nonatomic) std::function 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 { diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp index 284dbd15e6..e8d3e834a1 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIWebViewTest/UIWebViewTest.cpp @@ -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("Hello World","text/html"); + _webView->loadHTMLString("Hello World ","Images/"); }); this->addChild(loadHTMLBtn);