improve iOS WebView

This commit is contained in:
andyque 2014-09-15 18:02:11 +08:00
parent 126ec18a89
commit 5f95ad221e
5 changed files with 140 additions and 53 deletions

View File

@ -35,7 +35,11 @@ namespace experimental{
namespace ui{
WebView::WebView()
: _impl(new WebViewImpl(this))
: _impl(new WebViewImpl(this)),
_onJSCallback(nullptr),
_onShouldStartLoading(nullptr),
_onDidFinishLoading(nullptr),
_onDidFailLoading(nullptr)
{
}
@ -74,9 +78,9 @@ namespace experimental{
_impl->loadHTMLString(string, baseURL);
}
void WebView::loadUrl(const std::string &url)
void WebView::loadURL(const std::string &url)
{
_impl->loadUrl(url);
_impl->loadURL(url);
}
void WebView::loadFile(const std::string &fileName)
@ -135,6 +139,65 @@ namespace experimental{
Node::setVisible(visible);
_impl->setVisible(visible);
}
cocos2d::ui::Widget* WebView::createCloneInstance()
{
return WebView::create();
}
void WebView::copySpecialProperties(Widget* model)
{
WebView* webView = dynamic_cast<WebView*>(model);
if (webView)
{
this->_impl = webView->_impl;
this->_onShouldStartLoading = webView->_onShouldStartLoading;
this->_onDidFinishLoading = webView->_onDidFinishLoading;
this->_onDidFailLoading = webView->_onDidFailLoading;
this->_onJSCallback = webView->_onJSCallback;
}
}
void WebView::setOnDidFailLoading(const ccWebViewCallbak &callback)
{
_onDidFailLoading = callback;
}
void WebView::setOnDidFinishLoading(const ccWebViewCallbak &callback)
{
_onDidFinishLoading = callback;
}
void WebView::setOnShouldStartLoading(const std::function<bool(WebView *sender, std::string url)> &callback)
{
_onShouldStartLoading = callback;
}
void WebView::setOnJSCallback(const ccWebViewCallbak &callback)
{
_onJSCallback = callback;
}
std::function<bool(WebView *sender, std::string url)> WebView::getOnShouldStartLoading()const
{
return _onShouldStartLoading;
}
WebView::ccWebViewCallbak WebView::getOnDidFailLoading()const
{
return _onDidFailLoading;
}
WebView::ccWebViewCallbak WebView::getOnDidFinishLoading()const
{
return _onDidFinishLoading;
}
WebView::ccWebViewCallbak WebView::getOnJSCallback()const
{
return _onJSCallback;
}
} // namespace ui
} // namespace experimental
} //namespace cocos2d

View File

@ -44,46 +44,12 @@ public:
*/
static WebView *create();
/**
* Default constructor
*/
WebView();
/**
* Default destructor
*/
virtual ~WebView();
/**
* Call before a web view begins loading.
* @param sender The web view that is about to load new content.
* @param url content URL.
* @return YES if the web view should begin loading content; otherwise, NO .
*/
std::function<bool(WebView *sender, std::string url)> shouldStartLoading;
/**
* Call after a web view finishes loading.
* @param sender The web view that has finished loading.
* @param url content URL.
*/
std::function<void(WebView *sender, std::string url)> didFinishLoading;
/**
* Call if a web view failed to load content.
* @param sender The web view that has failed loading.
* @param url content URL.
*/
std::function<void(WebView *sender, std::string url)> didFailLoading;
/**
* Set javascript interface scheme.
* @see #onJsCallback
*/
void setJavascriptInterfaceScheme(const std::string &scheme);
/**
* This callback called when load URL that start with javascript interface scheme.
*/
std::function<void(WebView *sender, std::string message)> onJsCallback;
/**
* Sets the main page contents, MIME type, content encoding, and base URL.
@ -109,7 +75,7 @@ public:
* Loads the given URL.
* @param url content URL
*/
void loadUrl(const std::string &url);
void loadURL(const std::string &url);
/**
* Loads the given fileName.
@ -162,9 +128,67 @@ public:
virtual void draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) override;
virtual void setVisible(bool visible) override;
typedef std::function<void(WebView *sender, std::string url)> ccWebViewCallbak;
/**
* Call before a web view begins loading.
* @param sender The web view that is about to load new content.
* @param url content URL.
* @return YES if the web view should begin loading content; otherwise, NO .
*/
void setOnShouldStartLoading(const std::function<bool(WebView *sender, std::string url)>& callback);
/**
* Call after a web view finishes loading.
* @param sender The web view that has finished loading.
* @param url content URL.
*/
void setOnDidFinishLoading(const ccWebViewCallbak& callback);
/**
* Call if a web view failed to load content.
* @param sender The web view that has failed loading.
* @param url content URL.
*/
void setOnDidFailLoading(const ccWebViewCallbak& callback);
/**
* This callback called when load URL that start with javascript interface scheme.
*/
void setOnJSCallback(const ccWebViewCallbak& callback);
std::function<bool(WebView *sender, std::string url)> getOnShouldStartLoading()const;
ccWebViewCallbak getOnDidFinishLoading()const;
ccWebViewCallbak getOnDidFailLoading()const;
ccWebViewCallbak getOnJSCallback()const;
protected:
virtual cocos2d::ui::Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
std::function<bool(WebView *sender, std::string url)> _onShouldStartLoading;
ccWebViewCallbak _onDidFinishLoading;
ccWebViewCallbak _onDidFailLoading;
ccWebViewCallbak _onJSCallback;
/**
* Default constructor
*/
WebView();
/**
* Default destructor
*/
virtual ~WebView();
private:
WebViewImpl *_impl;
friend class WebViewImpl;
};
} // namespace ui

View File

@ -55,7 +55,7 @@ public:
void loadHTMLString(const std::string &string, const std::string &baseURL);
void loadUrl(const std::string &url);
void loadURL(const std::string &url);
void loadFile(const std::string &fileName);

View File

@ -231,24 +231,24 @@ WebViewImpl::WebViewImpl(WebView *webView)
[_uiWebViewWrapper retain];
_uiWebViewWrapper.shouldStartLoading = [this](std::string url) {
if (this->_webView->shouldStartLoading) {
return this->_webView->shouldStartLoading(this->_webView, url);
if (this->_webView->_onShouldStartLoading) {
return this->_webView->_onShouldStartLoading(this->_webView, url);
}
return true;
};
_uiWebViewWrapper.didFinishLoading = [this](std::string url) {
if (this->_webView->didFinishLoading) {
this->_webView->didFinishLoading(this->_webView, url);
if (this->_webView->_onDidFinishLoading) {
this->_webView->_onDidFinishLoading(this->_webView, url);
}
};
_uiWebViewWrapper.didFailLoading = [this](std::string url) {
if (this->_webView->didFailLoading) {
this->_webView->didFailLoading(this->_webView, url);
if (this->_webView->_onDidFailLoading) {
this->_webView->_onDidFailLoading(this->_webView, url);
}
};
_uiWebViewWrapper.onJsCallback = [this](std::string url) {
if (this->_webView->onJsCallback) {
this->_webView->onJsCallback(this->_webView, url);
if (this->_webView->_onJSCallback) {
this->_webView->_onJSCallback(this->_webView, url);
}
};
}
@ -275,7 +275,7 @@ void WebViewImpl::loadHTMLString(const std::string &string, const std::string &b
[_uiWebViewWrapper loadHTMLString:string baseURL:baseURL];
}
void WebViewImpl::loadUrl(const std::string &url) {
void WebViewImpl::loadURL(const std::string &url) {
[_uiWebViewWrapper loadUrl:url];
}

View File

@ -43,12 +43,12 @@ bool WebViewTest::init()
_webView = cocos2d::experimental::ui::WebView::create();
_webView->setPosition(winSize/2);
_webView->setContentSize(winSize * 0.5);
_webView->loadUrl("http://www.google.com");
_webView->loadURL("http://www.google.com");
_webView->setScalesPageToFit(true);
_webView->shouldStartLoading = CC_CALLBACK_2(WebViewTest::onWebViewShouldStartLoading, this);
_webView->didFinishLoading = CC_CALLBACK_2(WebViewTest::onWebViewDidFinishLoading, this);
_webView->didFailLoading = CC_CALLBACK_2(WebViewTest::onWebViewDidFailLoading, this);
_webView->setOnShouldStartLoading(CC_CALLBACK_2(WebViewTest::onWebViewShouldStartLoading, this));
_webView->setOnDidFinishLoading(CC_CALLBACK_2(WebViewTest::onWebViewDidFinishLoading, this));
_webView->setOnDidFailLoading(CC_CALLBACK_2(WebViewTest::onWebViewDidFailLoading, this));
this->addChild(_webView);
@ -71,7 +71,7 @@ bool WebViewTest::init()
resetBtn->setPosition(Vec2(winSize/2) + Vec2(50, _webView->getContentSize().height/2 +
resetBtn->getContentSize().height/2 + 10));
resetBtn->addClickEventListener([=](Ref*){
_webView->loadUrl(std::string("http://") + urlTextField->getString());
_webView->loadURL(std::string("http://") + urlTextField->getString());
});
this->addChild(resetBtn);