Reactnative-iOS回調(diào)Javascript的方法
Reactnative可以調(diào)用原生模塊,原生模塊也可以給JavaScript發(fā)送事件通知.最好的方法是繼承RCTEventEmitter.自定義繼承自PushEventEmitter的子類RCTEventEmitter.
#import <Foundation/Foundation.h> #import <React/RCTBridgeModule.h> #import <React/RCTEventEmitter.h> @interface PushEventEmitter : RCTEventEmitter <RCTBridgeModule> - (void)addEventReminderReceived:(NSNotification *)notification; @end
實現(xiàn)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();// 移除通知
}
調(diào)用測試:
PushEventEmitter *eventEmitter = [PushEventEmitter allocWithZone:nil];
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS開發(fā)中UIWebView的加載本地數(shù)據(jù)的三種方式
這篇文章主要介紹了iOS開發(fā)中UIWebView的加載本地數(shù)據(jù)的三種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
ios App加載本地HTML網(wǎng)頁,點擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法
下面小編就為大家分享一篇ios App加載本地HTML網(wǎng)頁,點擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS實現(xiàn)兩個控制器之間數(shù)據(jù)的雙向傳遞
這篇文章主要為大家詳細介紹了iOS實現(xiàn)兩個控制器之間數(shù)據(jù)的雙向傳遞的相關資料,感興趣的小伙伴們可以參考一下2016-05-05
iOS利用NSMutableAttributedString實現(xiàn)富文本的方法小結(jié)
這篇文章主要給大家介紹了關于iOS利用NSMutableAttributedString如何實現(xiàn)富文本的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-05-05

