码迷,mamicode.com
首页 > 移动开发 > 详细

App交互demo

时间:2015-11-03 19:16:13      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

Android

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>方法实现</title>
<meta name="author" content="m.gome.com.cn">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<script type="text/javascript" src="js/zepto.js"></script>
</head>
<body>
<h1>JS_Android交互测试</h1>
<script type="text/javascript">

function test(){
var _log = document.createElement(p);
document.body.appendChild(_log);
_log.innerHTML = JS handler [test] was called;
}

//打印错误日志
window.onerror = function(err) {
var errlog = document.createElement(p);
document.body.appendChild(errlog);
errlog.innerHTML = err;
}
Zepto(function(){
bridge.send(Enemy spotted);
})

window.onload = function(){
var uniqueId = 1;

//日志记录 
function log(message, data) { 
var log = document.getElementById(log),
el = document.createElement(div);
el.className = logLine;
el.innerHTML = uniqueId++ + .  + message + (data ? :  + JSON.stringify(data) : ‘‘);
if (log.children.length) {
log.insertBefore(el, log.children[0]);
} 
else {
log.appendChild(el);
} 
}

log(window onload Ready);

/* 功能1
* 页面加载完成后给Native传值
* Native随时给js传值
* 应用场景: H5页面传值给Native,动态生成顶栏标题。
* 测试用例: 页面加载完成后,js给Native传值,Native拿到后传值给js,页面打印日志
*/


/* 功能2
* Native调用js方法,执行完后回调
* 应用场景: Native返回按钮调用H5返回功能
* 测试用例: 项目js和wap公用js(不影响现有wap站js库,且调用灵活)
*/
// bridge.delayJsCallBack(0, ‘call back haha‘, function (msg) {
// bridge.alert(msg);
// });
//bridge.registerHandler(‘test‘, wapJs());
//test();

/* 功能3
* js调用Native方法,执行完后回调
* 应用场景: H5分享按钮调用Native分享功能
* 测试用例: alert
*/
var Btn = document.getElementById(btn);
Btn.ontouchstart = function(e){
e.preventDefault();
log("Calling Native handler testOC");
bridge.callHandler(testOC,{id: 123}, function(response) {
log(Got response from testOC callBack, response)
})
} 
}
</script>
<button id=‘btn‘>js call Native</button>
<div id=‘log‘></div>
</body>
</html>

IOS:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>方法实现</title>
<meta name="author" content="m.gome.com.cn">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.effect.js"></script>
</head>
<body>
<h1>JS_OC交互测试</h1>
<script type="text/javascript">
    
    window.onerror = function(err) {
        log(err);
    }

    //给WebViewJavascriptBridgeReady事件注册一个Listener  
    document.addEventListener(WebViewJavascriptBridgeReady, onBridgeReady, false)  
    //事件的响应处理  
    function onBridgeReady(event) {  
        var bridge = event.bridge,
            uniqueId = 1;
        
        //日志记录  
        function log(message, data) {  
            var log = document.getElementById(log),
                el = document.createElement(div);
            el.className = logLine;
            el.innerHTML = uniqueId++ + .  + message + (data ? :  + JSON.stringify(data) : ‘‘);
            if (log.children.length) {
                log.insertBefore(el, log.children[0]);
            }  
            else {
                log.appendChild(el);
            }  
        }
        
        log(WebView2Javascript Bridge Ready);
        
        /* 功能1
         * 页面加载完成后给Native传值
         * Native随时给js传值
         * 应用场景: H5页面传值给Native,动态生成顶栏标题。
         * 测试用例: 页面加载完成后,js给Native传值,Native拿到后传值给js,页面打印日志
        */
        bridge.send(Enemy spotted);
        //初始化操作,并定义默认的消息处理逻辑
        bridge.init(function(message) {
            log(Roger that, 3Q js!, message);
        })
        
        /* 功能2
         * Native调用js方法,执行完后回调
         * 应用场景: Native返回按钮调用H5返回功能
         * 测试用例: 项目js和wap公用js(不影响现有wap站js库,且调用灵活)
         */
        bridge.registerHandler(test, function(data, response) {
            log(JS handler [test] was called, data);
            response.respondWith({ Javascript:Roger! }); 
        })
        //bridge.registerHandler(‘test‘, wapJs());
        
        /* 功能3
         * js调用Native方法,执行完后回调
         * 应用场景: H5分享按钮调用Native分享功能
         * 测试用例: alert
         */
        var Btn = document.getElementById(btn);
        Btn.ontouchstart = function(e){
            e.preventDefault();
            log("Calling Native handler testOC");
            bridge.callHandler(testOC,{id: 123}, function(response) {
                log(Got response from testOC callBack, response)
            })
        }  
    }
</script>
    <button id=‘btn‘>js call Native</button>
    <div id=‘log‘></div>
</body>
</html>

 

App交互demo

标签:

原文地址:http://www.cnblogs.com/xuxu-dragon/p/4933809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!