C++11 无法从cocos2d-x(v3.10)中的Webview获取回调
我正在学习并尝试在cocos2d-x中开发应用程序。 如何从WebView is实现回调? 我一直在使用cocos2d-xv3.10 test_index.htmlC++11 无法从cocos2d-x(v3.10)中的Webview获取回调,c++11,webview,cocos2d-iphone,cocos2d-x,C++11,Webview,Cocos2d Iphone,Cocos2d X,我正在学习并尝试在cocos2d-x中开发应用程序。 如何从WebView is实现回调? 我一直在使用cocos2d-xv3.10 test_index.html <head> <meta charset="utf-8"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="format-detection" content="telephon
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<meta name="robots" content="none" />
<meta name="viewport" content="width=device-width,height=device-height">
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="js/sp.js"></script>
<ul class="slideshow">
<li>
<a href="#" onclick='sp.call("test1")'>
<img src="img/test1.jpg" width="100%"/>
</li>
</ul>
<style>
.bx-default-pager {
display: none;
}
.bx-viewport {
border: none !important;
}
</style>
</body>
test.cpp
bool TestScene::init()
{
auto webView = cocos2d::experimental::ui::WebView::create();
webView->loadURL("http://test/test_index.html");
webview->setONJSCallback(CC_CALLBACK_1(TestScene::callbackFromJS,this));
webView->setAnchorPoint(Point(0,0));
webView->setPosition(Point(0,150));
webView->setContentSize(Size(WINSIZE.width,WINSIZE.height-150));
this->addChild(webView,1);
}
void TestScene::callbackFromJS(cocos2d::experimental::ui::WebView* webview, std::string* url)
{
log("call this method");
}
错误使用“setOnJSCallback” 那么您只能访问“https”而不能访问“http”?这是因为新的IOS 9限制。为了能够访问http,您需要将以下内容添加到plist中
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connection -->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
JSCallback的工作原理如下
每次加载url时,它都会检查是否包含为webview定义的JavascriptInterfaceScheme名称(如果有)。如果它在那里,它将不会加载该url,而是调用JSCallBack函数。如果没有,它通常会加载url。在@Plusmiles情况下,您没有设置任何JavascriptInterfaceScheme名称
下面是为我工作的代码
cocos2d::experimental::ui::WebView * Practice::_webView;
_webView = experimental::ui::WebView::create();
_webView->setContentSize(Size(1200, 1200));
_webView->setPosition(Vec2(900,768));
_webView->setJavascriptInterfaceScheme("cocos2dx");
_webView->loadURL("https://example.com");
_webView->setOnJSCallback(&Practice::callbackFromJS);
this->addChild(_webView, 1);
void Practice::callbackFromJS(cocos2d::experimental::ui::WebView* webview, const std::string &answer) {
std::string response = answer;
}
要从html文件调用,需要设置
window.location.href = "cocos2dx:" + text;
其中cocos2dx是webview中设置的JavascriptInterfaceScheme名称。每当它在url中找到这个名称时,它将调用函数,webview对象作为第一个参数,url作为第二个参数。因此,如果希望加载url而不是调用JSCallback函数,则需要在url中避免此webview
我希望这个答案会有所帮助。您的项目设置允许从非https站点进行任意加载吗?不,我的项目也可以访问https。谢谢,但是已经允许“NSAllows Arbital loads”了。可以加载网站。但我无法通过按Web按钮获得回拨。
window.location.href = "cocos2dx:" + text;