React Native module on iOS doesn't get location updates

Viewed 64

In my RN application I created a helper module which in turn has to listen for significant location updates . However, it seems that I don't get any location updates in this module.

Module's header file:

//
//  RCTJesusModule.h
//  menora_myway
//
//  Created by Edward Keselman on 06/01/2022.
//

#import <UIKit/UIKit.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <CoreLocation/CoreLocation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTJesusModule : RCTEventEmitter <RCTBridgeModule, CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager* locationManager;
@end

NS_ASSUME_NONNULL_END

Module's m file:

//
//  RCTJesusModule.m
//  menora_myway
//
//  Created by Edward Keselman on 06/01/2022.
//

#import "RCTJesusModule.h"
#import <UserNotifications/UserNotifications.h>

@implementation RCTJesusModule
{
  BOOL didShowNotifications;
}
RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(setupLocationListener:(RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject)
{
  NSLog(@"JesusModule => setupLocationListener");
  self.locationManager = [[CLLocationManager alloc] init];
  [self.locationManager setDelegate:self];
  [self checkPermissions];
  NSMutableDictionary *result = [NSMutableDictionary new];
  result[@"result"] = @"success";
  resolve(result);
}

-(void)checkPermissions {
  NSLog(@"JesusModule => checkPermissions");
  if ([CLLocationManager locationServicesEnabled]) {

      switch ([CLLocationManager authorizationStatus]) {
        case kCLAuthorizationStatusDenied:
            NSLog(@"JesusModule: kCLAuthorizationStatusDenied");
            break;
        case kCLAuthorizationStatusRestricted:
            NSLog(@"JesusModule: kCLAuthorizationStatusRestricted");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"JesusModule: kCLAuthorizationStatusAuthorizedAlways");
            [self.locationManager setAllowsBackgroundLocationUpdates:YES];
            [self.locationManager startUpdatingLocation];
            [self.locationManager startMonitoringSignificantLocationChanges];
          break;
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"JesusModule: kCLAuthorizationStatusAuthorizedWhenInUse");
            break;
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"JesusModule: kCLAuthorizationStatusNotDetermined");
            break;
        default:
            break;
      }
  }
}

- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
  NSLog(@"JesusModule => locationManagerDidChangeAuthorization");
  if (@available(iOS 14.0, *)) {
    NSLog(@"%d", [manager authorizationStatus]);
    switch ([manager authorizationStatus]) {
        
      case kCLAuthorizationStatusNotDetermined:
            
        break;
      case kCLAuthorizationStatusRestricted:
        
        break;
      case kCLAuthorizationStatusDenied:
        
        break;
      case kCLAuthorizationStatusAuthorizedAlways:
        NSLog(@"JesusModule => autherization always");
        [self.locationManager setAllowsBackgroundLocationUpdates:YES];
        [self.locationManager startUpdatingLocation];
        [self.locationManager startMonitoringSignificantLocationChanges];
        break;
      case kCLAuthorizationStatusAuthorizedWhenInUse:
      
        break;
    }
  }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  NSLog(@"JesusModule => didUpdateLocation");
  NSLog(@"JesusModule => Locations %@", locations);
}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"THE_FLOOW_DRIVING_EVENT", @"BLUETOOTH_CONNECTION_EVENT", @"GPS_TRIGGER_EVENT"];
}

@end

After initializing the module from my RN app, I only get logs from setupLocationListener and checkPermissions. I don't understand what is wrong with this setup. Is it possible for a module to implement CLLocationManagerDelegate or is it something that only AppDelegate or a ViewController can do?

0 Answers
Related