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