@tianfangye
2015-08-04T11:04:05.000000Z
字数 7964
阅读 2592
NativeAPI 用于 JavaScript 与 Native Code 双向通信。
sendToNative 方法,JavaScript 通过调用此方法向 Native 发送消息。sendToJavaScript 方法,Native 通过调用此方法此方法向 JavaScript 发送消息。params 为 0、1、123 之类数字时,类型全部用 String(即使用 "0"、"1"、"123" 这样的字符串)。
if ( /hbgj/i.test(appName) ) {window.location.href = "openetjs://start?type=nativeapi";} else if ( /gtgj/i.test(appName) ) {window.location.href = "gtgj://start?type=nativeapi";}
method
confirmparams
title String 标题信息message String 对话框消息yes_btn_text String 确定按钮⽂文字no_btn_text String 取消按钮⽂文字result
YES Number 常量 1NO Number 常量 0CLOSE Number 常量 2value Number 0/1/2JS -> Native: {method: "confirm", params: { title: "test", message: "hello world", yes_btn_text: "是的", no_btn_text: "不是" }, id: 1}
Native -> JS: {result {value: 0, YES:1, NO: 0, CLOSE: 2}, id: 1}
// invoke 方法是对 sendToNative 的封装NativeAPI.invoke("confirm", {title: "提示",message: "message",yes_btn_text: "~确定~",no_btn_text: "~取消~"},function(err, data) {if (err) {return handleError(err);}switch (data.value) {case data.YES:echo("你点了确定按钮");break;case data.NO:echo("你点了取消按钮");break;case data.CLOSE:echo("你使用其他方式关闭了弹窗");break;default:echo("未知动作,返回code是[" + data.value + "]");}});
method
alertparams
title String 标题信息message String 对话框消息btn_text String 确定按钮⽂文字result
YES Number 常量 1CLOSE Number 常量 0value Number 1 或者 0JS -> Native: {method: "alert", params: { title: 123, message: "msg", btn_text: "是的" }, id: 1}
Native -> JS: {result {value: 0, YES:0, CLOSE: 1}, id: 1}
NativeAPI.invoke("alert", {title: "这是标题",message: "这是消息",btn_text: "确定按钮"},function(err, data) {if (err) {return handleError(err);}switch (data.value) {case data.YES:echo("你点了确定按钮");break;case data.CLOSE:echo("你使用其他方式关闭了弹窗");break;default:echo("未知动作,返回code是[" + data.value + "]");}});
method
closemethod
closeAllmethod
createWebViewparams
url String url有可能是相对的,如果是相对路径的话就根据当前 webView 的 url 进⾏计算controls Array controls是一个数组,元素中 type: title 表示要设置的是标题,text: "Native Page 2" 为具体文本。
NativeAPI.invoke("createWebView", {url: window.location.origin + "xxx",controls: [{type: "title",text: "Native Page 2"}]});
method
backJS -> Native: {method: "back", params:null}
method
webViewCallbackparams
url String url有可能是相对的,如果是相对路径的话就根据当前webView的url进⾏计算JS -> Native: {method: "webViewCallback", params: {url: "/index.html" }}
method
loginparams
result
* SUCC 常量,表示登录流程完成
* FAIL 常量,表示登录流程中断
* CANCEL 常量,表示用户在登录过程中作了取消操作
* value 它的值应该是 SUCC/FAIL/CANCEL 其中之一,用来判断成功/失败等状态。
method
getUserInfoparams
appName String "gtgj"代表高铁管家,"hbgj"代表航班管家。result
phone String ⽤户电话uid String userid String ⽤户IDauthcode String hbuserid String 过渡期参数,下一版本统一用 userid。hbauthcode String 过渡期参数,下一版本统一用 authcode。error
code 描述:获取当前的客户端信息
method
getDeviceInforesult
imei String 移动设备国际识别码p String 用于识别 APP 的自定义的标识uuid String 该字段包含⽤户的登录信息,后端 API 可以通过将此字段兑换成 sessionId 之类的数据用于校验⽤户登录状态。channel String 例如:appstorename String 例如:gtgjversion String APP 版本method
makePhoneCallparams
number String 电话号码result
value Numbermethod
updateTitleparams
text String 标题栏⽂文字method
storageparams
action String 操作名称 [get|set|remove]key Stringvalue Stringresult
value Stringmethod
updateHeaderRightBtnparams
type String 指定按钮类型,例如:"phone", "order", "share"等。action String 控制按钮显⽰与隐藏,当 action == "show" 的时候展⽰,当 action == "hide" 的时候隐藏。icon String 按钮图片,图片的base64编码,如果没有找到icon则显⽰text字段,text字段默认为空。text String 按钮文字,没有 icon 字段时使用。data Object btn上附属的数据,在⽤户点击后传给⻚⾯内的js回调。JS -> Native: {method: "clearAppCache", params: null }
method
isSupportedparams
method String 需要查询的方法result
value Boolean true 支持,false 不支持。由于历史原因,isSupported 方法目前返回value:"1/0"。method
selectContactparams
selectedContacts String phone以逗号分隔。表示已经选择的联系人,用于显示联系人列表时自动选中。maxNum String 表示选择联系人的数量上限,默认为1(即单选),0 则表示不设上限。result
name String 联系人的名字phone Array 电话号码数组contacts Array 以数组形式返回联系人信息。原本的 name 和 phone 字段,则为数组中第一个联系人的信息(即兼容单选)。method
setOrientationparams
orientation String 默认值为 portrait(强制竖屏),其他值包括 auto(跟随手机旋转),landscape(强制横屏)。method
setGestureBackparams
preventDefault Boolean false 表⽰执⾏默认⾏为(手势返回有效),true 表⽰阻⽌止默认⾏为(手势返回无效)。method
sendSMSparams
phone Stringmessage Stringresult
value Boolean true 成功,false 不成功。method
getCurrentPositionresult
latitude The latitude as a decimal numberlongitude The longitude as a decimal numberaccuracy The accuracy of positionheading The heading as degrees clockwise from Northspeed The speed in meters per secondtimestamp The date/time of the responsemethod
scanBarcoderesult
value String 扫描得到的字符串method
sharePageparams
title String 分享块标题desc String 分享块描述link String 分享块链接imgUrl String 分享块图片type String 分享类型;支持 weixin weibo pengyouquan sms email;如果是 all 表示想要分享到所有,此参数为空或不传此参数时,效果应等同于 all。result
value Boolean true 成功,false 不成功。method
trackEventparams
event String 记录事件名attrs String 统计字符串(JSON String)method
startPayparams
quitpaymsg String 退出时候的提示title String 支付标题price String 商品价格orderid String 订单号productdesc String 商品描述url String 显示订单基本信息的Wap页面subdesc String 商品详情描述canceltype String 取消等待支付结果的跳转类型:0代表跳转机票订单,1代表跳转酒店订单,2代表跳转其他订单,-1代表不做跳转(默认为-1)result
SUCC 常量,表示支付成功FAIL 常量,表示支付失败CANCEL 常量,表示用户取消了支付(高铁管家3.3开始支持)PENDING 常量,表示目前为支付待确认状态,由于第三方支付结果可能需要等待较长的时间,未知状态时可以返回 PENDINGvalue SUCC/FAIL/CANCEL/PENDING
NativeAPI.invoke("startPay", {quitpaymsg: "您尚未完成支付,如现在退出,可稍后进入“个人中心->其他订单”完成支付。确认退出吗?, ",title: "商城订单",price: "145",orderid: "150513188189022",productdesc: "接机订单支付",subdesc: "专车接送",url: "http://jp.rsscc.com/Fmall/rest/client/v4/pay.htm?orderid=150513188189022",canceltype: -1}, function(err, data) {switch (data.value) {case data.SUCC:alert("支付成功, 跳转详情");break;case data.FAIL:alert("支付失败");break;case data.CANCEL:alert("您取消了支付");break;case data.PENDING:alert("目前为支付待确认状态");break;default:alert("支付异常");}});
method
loadingparams
show String 1 显示,0 隐藏text String 显示的文字result
SUCC 常量,正常显示FAIL 常量,显示失败CANCEL 常量,点了返回键value SUCC/FAIL/CANCELmethod
resumeparams
prevUrl String 当前 webview 为基准,前一个 webview 的 URL(没有就为空)。nextUrl String 当前 webview 为基准,后一个 webview 的 URL。Native -> JS: {method: "resume", params: {prevUrl: "a.html", nextUrl: "c.html"}}
method
backresult
preventDefault Boolean false 表⽰执⾏默认⾏为(在 Android 上表⽰⽴刻关闭当前界⾯),true 表⽰阻⽌止默认⾏为。流程说明:点返回按钮——N 调用 J 的 back——J 返回给 N {preventDefault: true/false}——N 根据 preventDefault 值决定是否执行返回操作。
Native -> JS: {method: "back", params:null, id: 1}
JS -> Native: {result: {preventDefault: false}, id: 1}
params
Native -> JS: {method: "headerRightBtnClick", params: null}
Native -> JS: {method: "headerRightBtnClick", params: { "foo": "bar"}}
// 先调用 updateHeaderRightBtn 设置右边按钮的文字或图片,再调用 headerRightBtnClick 设置点击了右侧按钮后做什么。rightButtonText: function() {var self = this;NativeAPI.invoke("updateHeaderRightBtn", {action: "show",text: "分享",data: {list: [1, 2, 3]}}, function(err, data) {if (err) {return handleError(err);}echo(JSON.stringify(data));});NativeAPI.registerHandler("headerRightBtnClick", function(data) {echo(JSON.stringify(data));self.share();});}