无法从cocos2d-x(v3.10)中的Webview获取回调

时间:2016-05-18 09:10:22

标签: c++11 webview cocos2d-iphone cocos2d-x

我正在学习并尝试在cocos2d-x中开发应用程序。 如何从WebView实现回调? 我一直在使用cocos2d-x v3.10。

test_index.html

<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>

sp.js

(function(){
    var sp = {};
    var isAndroid = navigator.userAgent.match(/Android/);
    var isIOs = navigator.userAgent.match(/iPhone/) || navigator.userAgent.match(/iPod/) || navigator.userAgent.match(/iPad/);
    sp.call = function(text){
        if(isAndroid){
            window.Cocos2dx.call(text);
        } else if (isIOs) {
            window.location.href = "cocos2dx:" + text;
        } else {
            window.location.href = text;
        }
    };
    window.sp = sp;
})();

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");
}

错误使用&#34; setOnJSCallback&#34;?

2 个答案:

答案 0 :(得分:1)

JSCallback的工作原理如下。

每次加载url时都会检查它是否包含JavascriptInterfaceScheme名称(如果已为webview定义)。如果它在那里它不会加载该url而是调用JSCallBack函数。如果不是,它通常会加载网址。在@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。

我希望这个答案会有所帮助。

答案 1 :(得分:0)

所以你只能访问&#34; https&#34;但不是&#34; http&#34;?这是因为新的IOS 9限制。您需要将以下内容添加到plist中才能访问http

class First extends React.Component{
  sendValue(){
    browserHistory.push('/second/' + this.refs.textField.value);
  }

  render(){
    return (
      <div>
        <input type='text' ref='textField' />
        <button onClick={() => {this.sendValue()}}>send</button>
      </div>)
  }
}

class Second extends React.Component { 
  render(){
    return <div>second component: {this.props.params.test}</div>
  }
}

class Main extends React.Component{
  render(){
    return (
     <Router history={browserHistory}>
          <Route path='/' component={First}></Route>  
          <Route path='/second/:test' component={Second}></Route>  
    </Router>)
   }
}