0

我正在尝试制作本机模块以响应本机以侦听接近传感器,因为现在不支持许多开源软件包。我尝试使用 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
4

1 回答 1

0

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

于 2021-10-04T14:10:18.637 回答