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)用測試:
PushEventEmitter *eventEmitter = [PushEventEmitter allocWithZone:nil];
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- react axios 跨域訪問一個或多個域名問題
- iOS端React Native差異化增量更新的實(shí)現(xiàn)方法
- react-native組件中NavigatorIOS和ListView結(jié)合使用的方法
- ios原生和react-native各種交互的示例代碼
- React Native第三方平臺分享的實(shí)例(Android,IOS雙平臺)
- IOS React等Title不顯示問題解決辦法
- React-Native Android 與 IOS App使用一份代碼實(shí)現(xiàn)方法
- IOS React Native FlexBox詳解及實(shí)例
- 詳解React Native與IOS端之間的交互
相關(guān)文章
iOS 9 Core Spotlight搜索實(shí)例代碼
本文主要講解 iOS 9 Core Spotlight,在 IOS 開發(fā)的時候有時候會用到搜索功能,這里給大家一個實(shí)例作為參考,有需要的小伙伴可以參考下2016-07-07
iOS開發(fā)中UIWebView的加載本地?cái)?shù)據(jù)的三種方式
這篇文章主要介紹了iOS開發(fā)中UIWebView的加載本地?cái)?shù)據(jù)的三種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法
下面小編就為大家分享一篇ios App加載本地HTML網(wǎng)頁,點(diǎn)擊網(wǎng)頁鏈接跳轉(zhuǎn)到app頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS實(shí)現(xiàn)兩個控制器之間數(shù)據(jù)的雙向傳遞
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)兩個控制器之間數(shù)據(jù)的雙向傳遞的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
iOS中讀寫鎖的簡單實(shí)現(xiàn)方法實(shí)例
讀寫鎖是計(jì)算機(jī)程序的并發(fā)控制的一種同步機(jī)制,也稱“共享-互斥鎖”、多讀者-單寫者鎖,讀操作可并發(fā)重入,寫操作是互斥的,這篇文章主要給大家介紹了關(guān)于iOS中讀寫鎖的簡單實(shí)現(xiàn)方法,需要的朋友可以參考下2021-11-11
iOS實(shí)現(xiàn)手動和自動屏幕旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)手動和自動屏幕旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
iOS利用NSMutableAttributedString實(shí)現(xiàn)富文本的方法小結(jié)
這篇文章主要給大家介紹了關(guān)于iOS利用NSMutableAttributedString如何實(shí)現(xiàn)富文本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

