RNPMotion.m 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // RNPMotion.m
  3. // ReactNativePermissions
  4. //
  5. #import "RNPMotion.h"
  6. #import <CoreMotion/CoreMotion.h>
  7. @implementation RNPMotion
  8. + (NSString *)getStatus
  9. {
  10. if (![CMMotionActivityManager isActivityAvailable]) {
  11. return RNPStatusRestricted;
  12. }
  13. if (@available(iOS 11.0, *)) {
  14. CMAuthorizationStatus status = [CMMotionActivityManager authorizationStatus];
  15. switch (status) {
  16. case CMAuthorizationStatusAuthorized:
  17. return RNPStatusAuthorized;
  18. case CMAuthorizationStatusDenied:
  19. return RNPStatusDenied;
  20. case CMAuthorizationStatusNotDetermined:
  21. return RNPStatusUndetermined;
  22. case CMAuthorizationStatusRestricted:
  23. return RNPStatusRestricted;
  24. default:
  25. return RNPStatusUndetermined;
  26. }
  27. } else {
  28. return RNPStatusRestricted;
  29. }
  30. }
  31. + (void)request:(void (^)(NSString *))completionHandler
  32. {
  33. __block NSString *status = [RNPMotion getStatus];
  34. if ([status isEqual: RNPStatusUndetermined]) {
  35. __block CMMotionActivityManager *activityManager = [[CMMotionActivityManager alloc] init];
  36. __block NSOperationQueue *motionActivityQueue = [[NSOperationQueue alloc] init];
  37. [activityManager queryActivityStartingFromDate:[NSDate distantPast] toDate:[NSDate date] toQueue:motionActivityQueue withHandler:^(NSArray *activities, NSError *error) {
  38. if (error) {
  39. status = RNPStatusDenied;
  40. } else if (activities || !error) {
  41. status = RNPStatusAuthorized;
  42. }
  43. dispatch_async(dispatch_get_main_queue(), ^{
  44. completionHandler(status);
  45. });
  46. activityManager = nil;
  47. motionActivityQueue = nil;
  48. }];
  49. } else {
  50. completionHandler(status);
  51. }
  52. }
  53. @end