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
實(shí)現(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 設(shè)置:
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)用測(cè)試:
PushEventEmitter *eventEmitter = [PushEventEmitter allocWithZone:nil];
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- react axios 跨域訪問(wèn)一個(gè)或多個(gè)域名問(wèn)題
- iOS端React Native差異化增量更新的實(shí)現(xiàn)方法
- react-native組件中NavigatorIOS和ListView結(jié)合使用的方法
- ios原生和react-native各種交互的示例代碼
- React Native第三方平臺(tái)分享的實(shí)例(Android,IOS雙平臺(tái))
- IOS React等Title不顯示問(wèn)題解決辦法
- React-Native Android 與 IOS App使用一份代碼實(shí)現(xiàn)方法
- IOS React Native FlexBox詳解及實(shí)例
- 詳解React Native與IOS端之間的交互
相關(guān)文章
iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能
這篇文章主要為大家詳細(xì)介紹了iOS使用CoreMotion實(shí)現(xiàn)搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
IOS 網(wǎng)絡(luò)請(qǐng)求中設(shè)置cookie
這篇文章主要介紹了IOS 網(wǎng)絡(luò)請(qǐng)求中設(shè)置cookie的相關(guān)資料,需要的朋友可以參考下2017-06-06
iOS開發(fā)之tableView cell的展開收回功能實(shí)現(xiàn)代碼
本文介紹了iOS開發(fā)之tableView cell的展開收回功能實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS登錄時(shí)驗(yàn)證手機(jī)號(hào)與倒計(jì)時(shí)發(fā)送驗(yàn)證碼問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于iOS登錄時(shí)驗(yàn)證手機(jī)號(hào)與倒計(jì)時(shí)發(fā)送驗(yàn)證碼問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01
iOS 生成plist文件,在項(xiàng)目中代碼創(chuàng)建plist的實(shí)例
下面小編就為大家分享一篇iOS 生成plist文件,在項(xiàng)目中代碼創(chuàng)建plist的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

