WVStandardEventCenter.postNotificationToNative
H5 向 Native 发送事件,具体的事件名称可以是与 Native 业务方商议一致的任意名称。
iOS 事件监听用法
使用标准的消息中心监听事件:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myEventListener) name:eventName object:nil]
- (void)myEventListener🙁NSNotification *)notification {
// 事件参数可以从 notification.userInfo 中获取。
}
Android 事件监听用法
@import android.taobao.windvane.service;
@import android.taobao.windvane.standardmodal
// 首先,需要自己实现监听用的 Listener。
public class JsEventListener implements WVEventListener {
@Override
public WVEventResult onEvent(int id, WVEventContext ctx, Object... obj) {
// 所有事件均派发到这里,WVEventId.H5TONATIVE_EVENT 表示 H5 发送过来的事件。
if (id == WVEventId.H5TONATIVE_EVENT) {
if (obj[0] instanceof String) {
String params = (String)obj[0];
// 这里的 params 是包含 event 和 param 的 JSON String,需要自行反序列化。
}
}
return null;
}
}
// 然后,注册监听器。
WVEventService.getInstance().addEventListener(new JsEventListener());
输入参数
- [
string
] event – 要发送的事件名称。 - [
object
] param – [可选]要发送的事件参数。
回调参数
无回调参数,如果成功蜂鸣,则进入 success
回调,否则进入 failure
回调。
var params = {
event: 'eventName',
param: {
// 事件要传递的数据。
}
};
window.WindVane.call('WVStandardEventCenter','postNotificationToNative', params, function(e) {
alert('success');
}, function(e) {
alert('failure: ' + JSON.stringify(e));
});
原创文章,作者:网友投稿,如若转载,请注明出处:https://www.cloudads.cn/archives/33676.html