Reactnative-iOS回调Javascript的方法
更新时间:2018年09月03日 10:07:31 作者:FlyElephant
这篇文章主要介绍了Reactnative-iOS回调Javascript的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Reactnative可以调用原生模块,原生模块也可以给JavaScript发送事件通知.最好的方法是继承RCTEventEmitter.自定义继承自PushEventEmitter的子类RCTEventEmitter.
#import <Foundation/Foundation.h> #import <React/RCTBridgeModule.h> #import <React/RCTEventEmitter.h> @interface PushEventEmitter : RCTEventEmitter <RCTBridgeModule> - (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<NSString *> *)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];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
ios App加载本地HTML网页,点击网页链接跳转到app页面的方法
下面小编就为大家分享一篇ios App加载本地HTML网页,点击网页链接跳转到app页面的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-01-01
iOS利用NSMutableAttributedString实现富文本的方法小结
这篇文章主要给大家介绍了关于iOS利用NSMutableAttributedString如何实现富文本的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2018-05-05


最新评论