【问题标题】:React Native Native Module for Proximity用于接近的 React Native Native 模块
【发布时间】:2021-11-24 16:42:06
【问题描述】:

我正在尝试制作本机模块以响应本机来监听接近传感器,因为现在不支持许多开源软件包。我尝试使用 IOS 的 React Native 文档,但我不知道 obj-c,而且我没有足够的时间来学习所有这些人员。有人可以告诉我我在下面的代码中做错了什么并提供更好的解决方案吗?

#import "RCTProximityModule.h"
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
@implementation RCTProximityModule

- (NSArray<NSString *> *)supportedEvents {
    return @[@"proximityChange"];
}
- (void) activateProximitySensor {
    UIDevice *device = [UIDevice currentDevice];
    device.proximityMonitoringEnabled = YES;
    if (device.proximityMonitoringEnabled == YES) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:device];
    }
}

- (void) proximityChanged:(NSNotification *)notification {
    UIDevice *device = [notification object];
  bool proximityState = device.proximityState;
  RCTLogInfo(@"prox state");
  RCTLogInfo(proximityState ? @"prox true" : @"prox false");
  [self sendEventWithName:@"proximityChange" body:@{@"name": proximityState}];
}

RCT_EXPORT_MODULE(ProximityModule);
@end

【问题讨论】:

    标签: ios objective-c react-native native-module


    【解决方案1】:

    通过将本机模块更改为 swift 语言解决了这个问题。它更清晰,更类似于现代高级语言。要配置 swift 模块,只需查看 React Native 文档或本教程:https://teabreak.e-spres-oh.com/swift-in-react-native-the-ultimate-guide-part-1-modules-9bb8d054db03#f662

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2018-11-03
      • 2020-01-18
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多