I'm trying to make native module for react native to listen proximity sensor as many open source packages are not supported now. I tried with React Native documentation for IOS but I don't know obj-c and I don't have enough time to learn all this staff. Can someone tell me what I'm doing wrong in code bellow and provide some better solution?
#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