Browse Source

Merge branch 'add_camera_check' into 'master'

添加对Camera的权限支持

See merge request BA/react-native-permissions!2
wd 7 years ago
parent
commit
238142c807

+ 17 - 0
ios/Permissions/RNPAudioVideo.h

@@ -0,0 +1,17 @@
+//
+//  RNPAudioVideo.h
+//  ReactNativePermissions
+//
+//  Created by Yonah Forst on 11/07/16.
+//  Copyright © 2016 Yonah Forst. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "RCTConvert+RNPStatus.h"
+
+@interface RNPAudioVideo : NSObject
+
++ (NSString *)getStatus:(NSString *)type;
++ (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler;
+
+@end

+ 48 - 0
ios/Permissions/RNPAudioVideo.m

@@ -0,0 +1,48 @@
+//
+//  RNPCamera.m
+//  ReactNativePermissions
+//
+//  Created by Yonah Forst on 11/07/16.
+//  Copyright © 2016 Yonah Forst. All rights reserved.
+//
+
+#import "RNPAudioVideo.h"
+
+#import <AVFoundation/AVFoundation.h>
+
+@implementation RNPAudioVideo
+
++ (NSString *)getStatus:(NSString *)type
+{
+    int status = [AVCaptureDevice authorizationStatusForMediaType:[self typeFromString:type]];
+    switch (status) {
+        case AVAuthorizationStatusAuthorized:
+            return RNPStatusAuthorized;
+        case AVAuthorizationStatusDenied:
+            return RNPStatusDenied;
+        case AVAuthorizationStatusRestricted:
+            return RNPStatusRestricted;
+        default:
+            return RNPStatusUndetermined;
+    }
+}
+
++ (void)request:(NSString *)type completionHandler:(void (^)(NSString *))completionHandler
+{
+    [AVCaptureDevice requestAccessForMediaType:[self typeFromString:type]
+                             completionHandler:^(BOOL granted) {
+                                 dispatch_async(dispatch_get_main_queue(), ^{
+                                     completionHandler([RNPAudioVideo getStatus:type]);
+                                 });
+                             }];
+}
+
++ (NSString *)typeFromString:(NSString *)string {
+    if ([string isEqualToString:@"audio"]) {
+        return AVMediaTypeAudio;
+    } else {
+        return AVMediaTypeVideo;
+    }
+}
+
+@end

+ 1 - 0
ios/RCTConvert+RNPStatus.h

@@ -23,6 +23,7 @@ static NSString* RNPStatusRestricted = @"restricted";
 typedef NS_ENUM(NSInteger, RNPType) {
     RNPTypeUnknown,
     RNPTypeLocation,
+    RNPTypeCamera,
     RNPTypePhoto,
     RNPTypeNotification,
 };

+ 1 - 0
ios/RCTConvert+RNPStatus.m

@@ -11,6 +11,7 @@
 @implementation RCTConvert (RNPStatus)
 
 RCT_ENUM_CONVERTER(RNPType, (@{ @"location" : @(RNPTypeLocation),
+                                @"camera" : @(RNPTypeCamera),
                                 @"photo" : @(RNPTypePhoto),
                                 @"notification" : @(RNPTypeNotification)
                                 }),

+ 6 - 0
ios/ReactNativePermissions.m

@@ -36,6 +36,7 @@
 
 #import "RNPLocation.h"
 #import "RNPNotification.h"
+#import "RNPAudioVideo.h"
 #import "RNPPhoto.h"
 
 
@@ -109,6 +110,9 @@ RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id
             status = [RNPLocation getStatusForType:locationPermissionType];
             break;
         }
+        case RNPTypeCamera:
+            status = [RNPAudioVideo getStatus:@"video"];
+            break;
         case RNPTypePhoto:
             status = [RNPPhoto getStatus];
             break;
@@ -129,6 +133,8 @@ RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json r
     switch (type) {
         case RNPTypeLocation:
             return [self requestLocation:json resolve:resolve];
+        case RNPTypeCamera:
+            return [RNPAudioVideo request:@"video" completionHandler:resolve];
         case RNPTypePhoto:
             return [RNPPhoto request:resolve];
         case RNPTypeNotification:

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

@@ -12,6 +12,7 @@
 		6695820E1FE441A8008596CD /* RNPLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581FE1FE441A7008596CD /* RNPLocation.m */; };
 		669582111FE441A8008596CD /* RNPNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = 669582011FE441A7008596CD /* RNPNotification.m */; };
 		669582121FE441A8008596CD /* RNPPhoto.m in Sources */ = {isa = PBXBuildFile; fileRef = 669582031FE441A7008596CD /* RNPPhoto.m */; };
+		669582131FE441A8008596CD /* RNPAudioVideo.m in Sources */ = {isa = PBXBuildFile; fileRef = 669582071FE441A7008596CD /* RNPAudioVideo.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
@@ -37,6 +38,7 @@
 		669582011FE441A7008596CD /* RNPNotification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNPNotification.m; sourceTree = "<group>"; };
 		669582021FE441A7008596CD /* RNPLocation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPLocation.h; sourceTree = "<group>"; };
 		669582031FE441A7008596CD /* RNPPhoto.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNPPhoto.m; sourceTree = "<group>"; };
+		669582041FE441A7008596CD /* RNPAudioVideo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNPAudioVideo.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; };
 /* End PBXFileReference section */
@@ -63,6 +65,8 @@
 		669581FA1FE44191008596CD /* Permissions */ = {
 			isa = PBXGroup;
 			children = (
+				669582041FE441A7008596CD /* RNPAudioVideo.h */,
+				669582071FE441A7008596CD /* RNPAudioVideo.m */,
 				669582021FE441A7008596CD /* RNPLocation.h */,
 				669581FE1FE441A7008596CD /* RNPLocation.m */,
 				6695820C1FE441A8008596CD /* RNPNotification.h */,

+ 1 - 0
lib/permissions.ios.js

@@ -10,6 +10,7 @@ type RequestOptions = string | { type: string, rationale?: Rationale }
 
 const permissionTypes = [
   'location',
+  'camera',
   'photo',
   'notification'
 ]