react native enables geographic location authorization

When using RN to obtain longitude and latitude, it is found that the geographic location is configured in info, but the user is not prompted for authorization,
Then we added active authorization in the native
Native part code:
In AppDelegate.m:
Cllocationmanager * locationmanager is preferred;
The instance should be changed to global, otherwise the authorization pop-up box will flash by
info configuration item

<key>NSLocationAlwaysUsageDescription</key>
    < string > always access geographic location < / String >
    <key>NSLocationWhenInUseUsageDescription</key>
    < string > access geographic location during use < / String >
    <key>NSPhotoLibraryUsageDescription</key>

Method:

- (void) getUserLocation {
  int status=[CLLocationManager authorizationStatus];
  if (![CLLocationManager locationServicesEnabled] || status < 3) {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)
    {
      locationManager = [[CLLocationManager alloc] init];
      [locationManager requestAlwaysAuthorization];
      [locationManager requestWhenInUseAuthorization];
    }
  }
}

Called during initialization

rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  [RNSplashScreen show];  //Display startup screen
  [self getUserLocation];
  return YES;
}

Active trigger related interaction class. m file code



#import "openGps.h"
#import <CoreLocation/CoreLocation.h>
#import <WebKit/WebKit.h>

@implementation openGps
CLLocationManager  *locationManager;
RCT_EXPORT_MODULE(openGps)

RCT_EXPORT_METHOD(openGPS){
  
  BOOL enable=[CLLocationManager locationServicesEnabled];
  NSInteger status=[CLLocationManager authorizationStatus];
  if(!enable || status<3)
  {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)
    {
      locationManager = [[CLLocationManager alloc] init];
      [locationManager requestAlwaysAuthorization];
      [locationManager requestWhenInUseAuthorization];
    }
    UIAlertController *alertView=[UIAlertController alertControllerWithTitle:@"APP Want to access your geographic location for order review" message:@"Location service is not enabled, please enter the system[Set up]> [Privacy] > [Location service]Turn on the switch and allow the location service to be used" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"Open immediately" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertView addAction:sureAction];
    [alertView addAction:cancelAction];

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIViewController *mainViewController = keyWindow.rootViewController;

    [mainViewController presentViewController:alertView animated:YES completion:nil];
  }
  

}
@end

Call on rn side:

   NativeModules.openGps.openGPS()

Posted by safrica on Mon, 30 Dec 2019 21:10:59 -0800