Reactnative-iOS回調Javascript的方法
更新時間:2018年09月03日 10:07:31 作者:FlyElephant
這篇文章主要介紹了Reactnative-iOS回調Javascript的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
Reactnative可以調用原生模塊,原生模塊也可以給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
實現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開發(fā)中UIWebView的加載本地數據的三種方式
這篇文章主要介紹了iOS開發(fā)中UIWebView的加載本地數據的三種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
ios App加載本地HTML網頁,點擊網頁鏈接跳轉到app頁面的方法
下面小編就為大家分享一篇ios App加載本地HTML網頁,點擊網頁鏈接跳轉到app頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS利用NSMutableAttributedString實現富文本的方法小結
這篇文章主要給大家介紹了關于iOS利用NSMutableAttributedString如何實現富文本的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-05-05

