RNPermissionHandlerMotion.m 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 isFlaggedAsRequested:[[self class] handlerUniqueId]]) {
  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. [RNPermissions flagAsRequested:[[self class] handlerUniqueId]];
  62. }];
  63. }
  64. @end