RNPermissionHandlerLocationAlways.m 2.4KB

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