RNPermissionHandlerLocationWhenInUse.m 2.6KB

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