RNPermissionHandlerLocationWhenInUse.m 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #import "RNPermissionHandlerLocationWhenInUse.h"
  2. @import CoreLocation;
  3. @interface RNPermissionHandlerLocationWhenInUse() <CLLocationManagerDelegate>
  4. @property (nonatomic, strong) CLLocationManager *locationManager;
  5. @property (nonatomic, strong) void (^resolve)(RNPermissionStatus status);
  6. @property (nonatomic, strong) void (^reject)(NSError *error);
  7. @end
  8. @implementation RNPermissionHandlerLocationWhenInUse
  9. + (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
  10. return @[@"NSLocationWhenInUseUsageDescription"];
  11. }
  12. + (NSString * _Nonnull)handlerUniqueId {
  13. return @"ios.permission.LOCATION_WHEN_IN_USE";
  14. }
  15. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  16. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  17. if (![CLLocationManager locationServicesEnabled]) {
  18. return resolve(RNPermissionStatusNotAvailable);
  19. }
  20. switch ([CLLocationManager authorizationStatus]) {
  21. case kCLAuthorizationStatusNotDetermined:
  22. return resolve(RNPermissionStatusNotDetermined);
  23. case kCLAuthorizationStatusRestricted:
  24. return resolve(RNPermissionStatusRestricted);
  25. case kCLAuthorizationStatusDenied:
  26. return resolve(RNPermissionStatusDenied);
  27. case kCLAuthorizationStatusAuthorizedWhenInUse:
  28. case kCLAuthorizationStatusAuthorizedAlways:
  29. return resolve(RNPermissionStatusAuthorized);
  30. }
  31. }
  32. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  33. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  34. if (![CLLocationManager locationServicesEnabled]) {
  35. return resolve(RNPermissionStatusNotAvailable);
  36. }
  37. if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined) {
  38. return [self checkWithResolver:resolve rejecter:reject];
  39. }
  40. _resolve = resolve;
  41. _reject = reject;
  42. _locationManager = [CLLocationManager new];
  43. [_locationManager setDelegate:self];
  44. [_locationManager requestWhenInUseAuthorization];
  45. }
  46. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  47. if (status != kCLAuthorizationStatusNotDetermined) {
  48. [_locationManager setDelegate:nil];
  49. [self checkWithResolver:_resolve rejecter:_reject];
  50. }
  51. }
  52. @end