国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

Reactnative-iOS回調(diào)Javascript的方法

2020-07-26 02:24:06
字體:
供稿:網(wǎng)友

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 設(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 富裕县| 玛曲县| 洛扎县| 务川| 石景山区| 鹿邑县| 关岭| 麻栗坡县| 金堂县| 齐河县| 七台河市| 红桥区| 黎川县| 隆化县| 承德市| 泸溪县| 辉南县| 于田县| 榆社县| 梁山县| 昆山市| 阳泉市| 安图县| 石首市| 宁海县| 柳江县| 普格县| 易门县| 沐川县| 筠连县| 邳州市| 尖扎县| 原阳县| 辰溪县| 屯留县| 彩票| 马边| 芷江| 武功县| 康定县| 崇阳县|