国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 曲靖市| 太康县| 惠东县| 获嘉县| 维西| 苏尼特左旗| 武穴市| 屏东市| 当涂县| 化隆| 张家港市| 星子县| 桦川县| 绥江县| 长宁县| 武陟县| 桐庐县| 塔城市| 临清市| 如东县| 云和县| 丁青县| 连云港市| 江油市| 乌拉特中旗| 洛南县| 两当县| 泗洪县| 宜丰县| 瑞昌市| 额济纳旗| 屏南县| 明溪县| 邹平县| 稷山县| 洪江市| 佛坪县| 平遥县| 浦城县| 尉氏县| 虎林市|