RNPermissionHandlerLocationWhenInUse.m 2.2KB

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