Cocos2d-x3.3 使用WebView以及遇到的问题
首先需要添加webview的lua绑定1、 添加Webview的ini文件。 在 2、运行 5、WebView的常用API。 void loadURL(const std::string & url)//载入一个URL
bool canGoBack() //返回是否有一个历史项
//载入给定的HTML的字符串,baseURL为空字符串
void loadHTMLString(const std::string & string,const std::string & baseURL )
goForward
goBack
void reload() //重新载入当前的URL,所以loadURL后用
void setScalesPageToFit(const bool scalesPageToFit)//自动缩放以适应屏幕
create --创建一个webview
6、Lua代码 local webview = cc.WebView:create()
webview:addTo(self.webViewLayer)
webview:setVisible(true)
webview:setScalesPageToFit(true)
webview:loadHTMLString("html的内容","")
webview:setContentSize(size.width,size.height) -- 必要
遇到的坑1、WebView的背景是白色的期望变成透明。 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;
}
- (void)setupWebView {
if (!self.uiWebView) {
self.uiWebView = [[[UIWebView alloc] init] autorelease];
self.uiWebView.delegate = self;
// 2016/10/15 add
**[self.uiWebView setOpaque:NO];
[self.uiWebView setBackgroundColor:[UIColor clearColor]];
}**
if (!self.uiWebView.superview) {
auto view = cocos2d::Director::getInstance()->getOpenGLView();
auto eaglview = (CCEAGLView *) view->getEAGLView();
[eaglview addSubview:self.uiWebView];
}
}
- (void)setScalesPageToFit:(const bool)scalesPageToFit {
if (!self.uiWebView) {[self setupWebView];}//add
self.uiWebView.scalesPageToFit = scalesPageToFit;
}
- (void)loadHTMLString:(const std::string &)string baseURL:(const std::string &)baseURL {
if (!self.uiWebView) {[self setupWebView];} //for fixing no content show
//[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())]];
}
UIWebViewImpl-android.cpp 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;
}
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,"loadHTMLString","(ILjava/lang/String;Ljava/lang/String;)V")) {
jstring jString = t.env->NewStringUTF(string.c_str());
//change
//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,jBaseURL);
t.env->DeleteLocalRef(jString);
t.env->DeleteLocalRef(jBaseURL);
t.env->DeleteLocalRef(t.classID);
}
}
Cocos2dxWebViewHelper.java @SuppressWarnings("unused")
public static void loadHTMLString(final int index,final String htmlString,final String baseURL) {
cocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
if (webView != null) {
webView.loadDataWithBaseURL(baseURL,htmlString,null,null);
}
}
});
}
Cocos2dxWebView.java (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |