Reactnative-iOS回调Javascript的方法
Reactnative可以调用原生模块,原生模块也可以给JavaScript发送事件通知.最好的方法是继承RCTEventEmitter.自定义继承自PushEventEmitter的子类RCTEventEmitter.
创新互联是专业的孙吴网站建设公司,孙吴接单;提供成都网站建设、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行孙吴网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
#import#import #import @interface PushEventEmitter : RCTEventEmitter - (void)addEventReminderReceived:(NSNotification *)notification; @end
实现supportedEvents方法
#import "PushEventEmitter.h" @implementation PushEventEmitter + (id)allocWithZone:(NSZone *)zone { static PushEventEmitter *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [super allocWithZone:zone]; }); return sharedInstance; } RCT_EXPORT_MODULE(); - (NSArray*)supportedEvents { return @[@"EventReminder"]; } - (void)addEventReminderReceived:(NSNotification *)notification { [self sendEventWithName:@"EventReminder" body:@{@"name": @"FlyElephant"}]; } @end
React native 设置:
import { NativeModules, NativeEventEmitter, } from 'react-native'; const PushEventEmitter = NativeModules.PushEventEmitter; const emitterManager = new NativeEventEmitter(PushEventEmitter);
订阅通知和移除通知:
componentDidMount() { subscription = emitterManager.addListener( 'EventReminder', (reminder) => console.log('JavaScript接收到通知:'+reminder.name) ); } componentWillUnmount(){ subscription.remove();// 移除通知 }
调用测试:
PushEventEmitter *eventEmitter = [PushEventEmitter allocWithZone:nil];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
网页标题:Reactnative-iOS回调Javascript的方法
新闻来源:http://myzitong.com/article/ggjgdi.html