Reactnative-iOS回调Javascript的方法
Reactnative可以调用原生模块,原生模块也可以给JavaScript发送事件通知.最好的方法是继承RCTEventEmitter.自定义继承自PushEventEmitter的子类RCTEventEmitter.
#import#import #import @interfacePushEventEmitter:RCTEventEmitter -(void)addEventReminderReceived:(NSNotification*)notification; @end
实现supportedEvents方法
#import"PushEventEmitter.h"
@implementationPushEventEmitter
+(id)allocWithZone:(NSZone*)zone{
staticPushEventEmitter*sharedInstance=nil;
staticdispatch_once_tonceToken;
dispatch_once(&onceToken,^{
sharedInstance=[superallocWithZone:zone];
});
returnsharedInstance;
}
RCT_EXPORT_MODULE();
-(NSArray*)supportedEvents
{
return@[@"EventReminder"];
}
-(void)addEventReminderReceived:(NSNotification*)notification{
[selfsendEventWithName:@"EventReminder"body:@{@"name":@"FlyElephant"}];
}
@end
Reactnative设置:
import{
NativeModules,
NativeEventEmitter,
}from'react-native';
constPushEventEmitter=NativeModules.PushEventEmitter;
constemitterManager=newNativeEventEmitter(PushEventEmitter);
订阅通知和移除通知:
componentDidMount(){
subscription=emitterManager.addListener(
'EventReminder',
(reminder)=>console.log('JavaScript接收到通知:'+reminder.name)
);
}
componentWillUnmount(){
subscription.remove();//移除通知
}
调用测试:
PushEventEmitter*eventEmitter=[PushEventEmitterallocWithZone:nil];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。