0

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

1 Answers1

0

Solved this by changing to swift language for native modules. It's more clear and similar to modern hight level languages. To configure swift modules just check React Native documentation or this tutorial: https://teabreak.e-spres-oh.com/swift-in-react-native-the-ultimate-guide-part-1-modules-9bb8d054db03#f662