Browse Source

Merge pull request #210 from peterlau/motion-permission-request-fixed

Motion permission request - fixed conflicts
Mathieu Acthernoene 8 years ago
parent
commit
1ad011d04e

+ 3 - 0
README.md

@@ -160,6 +160,7 @@ The current supported permissions are:
 | Push Notifications | `notification`      | ✔️  | ❌      |
 | Background Refresh | `backgroundRefresh` | ✔️  | ❌      |
 | Speech Recognition | `speechRecognition` | ✔️  | ❌      |
+| Motion Activity    | `motion`            | ✔️  | ❌      |
 | Storage            | `storage`           | ❌️ | ✔       |
 | Phone Call         | `callPhone`         | ❌️ | ✔       |
 | Read SMS           | `readSms`           | ❌️ | ✔       |
@@ -238,6 +239,8 @@ So before submitting your app to the App Store, make sure that in your
 <string>Some description</string>
 <key>NSSpeechRecognitionUsageDescription</key>
 <string>Some description</string>
+<key>NSMotionUsageDescription</key>
+<string>Some description</string>
 ```
 
 This is required because during the phase of processing in the App Store

+ 14 - 0
ios/Permissions/RNPMotion.h

@@ -0,0 +1,14 @@
+//
+//  RNPMotion.h
+//  ReactNativePermissions
+//
+
+#import <Foundation/Foundation.h>
+#import "RCTConvert+RNPStatus.h"
+
+@interface RNPMotion : NSObject
+
++ (NSString *)getStatus;
++ (void)request:(void (^)(NSString *))completionHandler;
+
+@end

+ 62 - 0
ios/Permissions/RNPMotion.m

@@ -0,0 +1,62 @@
+//
+//  RNPMotion.m
+//  ReactNativePermissions
+//
+
+#import "RNPMotion.h"
+#import <CoreMotion/CoreMotion.h>
+
+@implementation RNPMotion
+
++ (NSString *)getStatus
+{
+    if (![CMMotionActivityManager isActivityAvailable]) {
+        return RNPStatusRestricted;
+    }
+    
+    if (@available(iOS 11.0, *)) {
+        CMAuthorizationStatus status = [CMMotionActivityManager authorizationStatus];
+        
+        switch (status) {
+            case CMAuthorizationStatusAuthorized:
+                return RNPStatusAuthorized;
+            case CMAuthorizationStatusDenied:
+                return RNPStatusDenied;
+            case CMAuthorizationStatusNotDetermined:
+                return RNPStatusUndetermined;
+            case CMAuthorizationStatusRestricted:
+                return RNPStatusRestricted;
+            default:
+                return RNPStatusUndetermined;
+        }
+    } else {
+        return RNPStatusRestricted;
+    }
+}
+
++ (void)request:(void (^)(NSString *))completionHandler
+{
+    __block NSString *status = [RNPMotion getStatus];
+    
+    if ([status isEqual: RNPStatusUndetermined]) {
+        __block CMMotionActivityManager *activityManager = [[CMMotionActivityManager alloc] init];
+        __block NSOperationQueue *motionActivityQueue = [[NSOperationQueue alloc] init];
+        [activityManager queryActivityStartingFromDate:[NSDate distantPast] toDate:[NSDate date] toQueue:motionActivityQueue withHandler:^(NSArray *activities, NSError *error) {
+            if (error) {
+                status = RNPStatusDenied;
+            } else if (activities || !error) {
+                status = RNPStatusAuthorized;
+            }
+            
+            dispatch_async(dispatch_get_main_queue(), ^{
+                completionHandler(status);
+            });
+            
+            activityManager = nil;
+            motionActivityQueue = nil;
+        }];
+    } else {
+        completionHandler(status);
+    }
+}
+@end

+ 2 - 1
ios/RCTConvert+RNPStatus.h

@@ -32,7 +32,8 @@ typedef NS_ENUM(NSInteger, RNPType) {
     RNPTypeBluetooth,
     RNPTypeNotification,
     RNPTypeBackgroundRefresh,
-    RNPTypeSpeechRecognition
+    RNPTypeSpeechRecognition,
+    RNPTypeMotion
 };
 
 @interface RCTConvert (RNPStatus)

+ 2 - 1
ios/RCTConvert+RNPStatus.m

@@ -20,7 +20,8 @@ RCT_ENUM_CONVERTER(RNPType, (@{ @"location" : @(RNPTypeLocation),
                                 @"bluetooth" : @(RNPTypeBluetooth),
                                 @"notification" : @(RNPTypeNotification),
                                 @"backgroundRefresh": @(RNPTypeBackgroundRefresh),
-                                @"speechRecognition": @(RNPTypeSpeechRecognition)
+                                @"speechRecognition": @(RNPTypeSpeechRecognition),
+                                @"motion": @(RNPTypeMotion)
                                 }),
                                 RNPTypeUnknown, integerValue)
 

+ 6 - 0
ios/ReactNativePermissions.m

@@ -43,6 +43,7 @@
 #import "RNPContacts.h"
 #import "RNPBackgroundRefresh.h"
 #import "RNPSpeechRecognition.h"
+#import "RNPMotion.h"
 
 @interface ReactNativePermissions()
 @property (strong, nonatomic) RNPLocation *locationMgr;
@@ -145,6 +146,9 @@ RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id
         case RNPTypeSpeechRecognition:
             status = [RNPSpeechRecognition getStatus];
             break;
+        case RNPTypeMotion:
+            status = [RNPMotion getStatus];
+            break;
         default:
             break;
     }
@@ -177,6 +181,8 @@ RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json r
             return [self requestNotification:json resolve:resolve];
         case RNPTypeSpeechRecognition:
             return [RNPSpeechRecognition request:resolve];
+        case RNPTypeMotion:
+            return [RNPMotion request:resolve];
         default:
             break;
     }

+ 6 - 0
ios/ReactNativePermissions.xcodeproj/project.pbxproj

@@ -18,6 +18,7 @@
 		669582131FE441A8008596CD /* RNPAudioVideo.m in Sources */ = {isa = PBXBuildFile; fileRef = 669582071FE441A7008596CD /* RNPAudioVideo.m */; };
 		669582141FE441A8008596CD /* RNPContacts.m in Sources */ = {isa = PBXBuildFile; fileRef = 669582081FE441A8008596CD /* RNPContacts.m */; };
 		669582151FE441A8008596CD /* RNPEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = 6695820A1FE441A8008596CD /* RNPEvent.m */; };
+		D0AD62322000657000D89898 /* RNPMotion.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AD62312000657000D89898 /* RNPMotion.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
@@ -56,6 +57,8 @@
 		6695820B1FE441A8008596CD /* RNPSpeechRecognition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPSpeechRecognition.h; sourceTree = "<group>"; };
 		6695820C1FE441A8008596CD /* RNPNotification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPNotification.h; sourceTree = "<group>"; };
 		9D23B34F1C767B80008B4819 /* libReactNativePermissions.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libReactNativePermissions.a; sourceTree = BUILT_PRODUCTS_DIR; };
+		D0AD62302000656F00D89898 /* RNPMotion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPMotion.h; sourceTree = "<group>"; };
+		D0AD62312000657000D89898 /* RNPMotion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNPMotion.m; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -82,6 +85,8 @@
 				669582081FE441A8008596CD /* RNPContacts.m */,
 				669582061FE441A7008596CD /* RNPEvent.h */,
 				6695820A1FE441A8008596CD /* RNPEvent.m */,
+				D0AD62302000656F00D89898 /* RNPMotion.h */,
+				D0AD62312000657000D89898 /* RNPMotion.m */,
 				669582021FE441A7008596CD /* RNPLocation.h */,
 				669581FE1FE441A7008596CD /* RNPLocation.m */,
 				6695820C1FE441A8008596CD /* RNPNotification.h */,
@@ -179,6 +184,7 @@
 				669582141FE441A8008596CD /* RNPContacts.m in Sources */,
 				6695820D1FE441A8008596CD /* RNPSpeechRecognition.m in Sources */,
 				669582131FE441A8008596CD /* RNPAudioVideo.m in Sources */,
+				D0AD62322000657000D89898 /* RNPMotion.m in Sources */,
 				669581F81FE4416B008596CD /* ReactNativePermissions.m in Sources */,
 				669582121FE441A8008596CD /* RNPPhoto.m in Sources */,
 			);

+ 1 - 0
lib/permissions.ios.js

@@ -20,6 +20,7 @@ const permissionTypes = [
   'notification',
   'backgroundRefresh',
   'speechRecognition',
+  'motion'
 ]
 
 const DEFAULTS = {