RNPermissionHandlerLocationAlways.m 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. ];
  15. }
  16. + (NSString * _Nonnull)handlerUniqueId {
  17. return @"ios.permission.LOCATION_ALWAYS";
  18. }
  19. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  20. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  21. if (![CLLocationManager locationServicesEnabled]) {
  22. return resolve(RNPermissionStatusNotAvailable);
  23. }
  24. switch ([CLLocationManager authorizationStatus]) {
  25. case kCLAuthorizationStatusNotDetermined:
  26. return resolve(RNPermissionStatusNotDetermined);
  27. case kCLAuthorizationStatusRestricted:
  28. return resolve(RNPermissionStatusRestricted);
  29. case kCLAuthorizationStatusAuthorizedWhenInUse:
  30. case kCLAuthorizationStatusDenied:
  31. return resolve(RNPermissionStatusDenied);
  32. case kCLAuthorizationStatusAuthorizedAlways:
  33. return resolve(RNPermissionStatusAuthorized);
  34. }
  35. }
  36. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  37. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  38. if (![CLLocationManager locationServicesEnabled]) {
  39. return resolve(RNPermissionStatusNotAvailable);
  40. }
  41. if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined) {
  42. return [self checkWithResolver:resolve rejecter:reject];
  43. }
  44. _resolve = resolve;
  45. _reject = reject;
  46. _locationManager = [CLLocationManager new];
  47. [_locationManager setDelegate:self];
  48. [_locationManager requestAlwaysAuthorization];
  49. }
  50. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  51. if (status != kCLAuthorizationStatusNotDetermined) {
  52. [_locationManager setDelegate:nil];
  53. [self checkWithResolver:_resolve rejecter:_reject];
  54. }
  55. }
  56. @end