RNPermissionHandlerMotion.m 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #import "RNPermissionHandlerMotion.h"
  2. @import CoreMotion;
  3. @interface RNPermissionHandlerMotion()
  4. @property (nonatomic, strong) CMMotionActivityManager *activityManager;
  5. @property (nonatomic, strong) NSOperationQueue *operationQueue;
  6. @end
  7. @implementation RNPermissionHandlerMotion
  8. + (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
  9. return @[@"NSMotionUsageDescription"];
  10. }
  11. + (NSString * _Nonnull)handlerUniqueId {
  12. return @"ios.permission.MOTION";
  13. }
  14. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  15. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  16. if (![CMMotionActivityManager isActivityAvailable]) {
  17. return resolve(RNPermissionStatusNotAvailable);
  18. }
  19. if (@available(iOS 11.0, *)) {
  20. switch ([CMMotionActivityManager authorizationStatus]) {
  21. case CMAuthorizationStatusNotDetermined:
  22. return resolve(RNPermissionStatusNotDetermined);
  23. case CMAuthorizationStatusRestricted:
  24. return resolve(RNPermissionStatusRestricted);
  25. case CMAuthorizationStatusDenied:
  26. return resolve(RNPermissionStatusDenied);
  27. case CMAuthorizationStatusAuthorized:
  28. return resolve(RNPermissionStatusAuthorized);
  29. }
  30. }
  31. if (![RNPermissions hasAlreadyBeenRequested:self]) {
  32. return resolve(RNPermissionStatusNotDetermined);
  33. }
  34. [self requestWithResolver:resolve rejecter:reject];
  35. }
  36. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  37. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  38. if (![CMMotionActivityManager isActivityAvailable]) {
  39. return resolve(RNPermissionStatusNotAvailable);
  40. }
  41. _activityManager = [CMMotionActivityManager new];
  42. _operationQueue = [NSOperationQueue new];
  43. [_activityManager queryActivityStartingFromDate:[NSDate distantPast]
  44. toDate:[NSDate date]
  45. toQueue:_operationQueue
  46. withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) {
  47. if (error != nil) {
  48. if (error.code != CMErrorNotAuthorized &&
  49. error.code != CMErrorMotionActivityNotAuthorized) {
  50. reject(error);
  51. } else {
  52. resolve(RNPermissionStatusDenied);
  53. }
  54. } else if (activities) {
  55. resolve(RNPermissionStatusAuthorized);
  56. } else {
  57. resolve(RNPermissionStatusNotDetermined);
  58. }
  59. self->_operationQueue = nil;
  60. self->_activityManager = nil;
  61. }];
  62. }
  63. @end