浏览代码

mediaLibrary ios added

Rory Pickering 8 年之前
父节点
当前提交
78f993bc6a

+ 2 - 0
README.md

@@ -160,6 +160,7 @@ The current supported permissions are:
 | Push Notifications | `notification`      | ✔️  | ❌      |
 | Background Refresh | `backgroundRefresh` | ✔️  | ❌      |
 | Speech Recognition | `speechRecognition` | ✔️  | ❌      |
+| mediaLibrary       | `mediaLibrary`      | ✔️  | ❌      |
 | Storage            | `storage`           | ❌️ | ✔       |
 | Phone Call         | `callPhone`         | ❌️ | ✔       |
 | Read SMS           | `readSms`           | ❌️ | ✔       |
@@ -186,6 +187,7 @@ The current supported permissions are:
 * Permission type `notification` accepts a second parameter for `request()`. The
   second parameter is an array with the desired alert types. Any combination of
   `alert`, `badge` and `sound` (default requests all three).
+* If you are not requesting mediaLibrary then you can remove MediaPlayer.framework from the xcode project
 
 ```js
 // example

+ 17 - 0
ios/Permissions/RNPMediaLibrary.h

@@ -0,0 +1,17 @@
+//
+//  RNPMediaLibrary.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 RNPMediaLibrary : NSObject
+
++ (NSString *)getStatus;
++ (void)request:(void (^)(NSString *))completionHandler;
+
+@end

+ 41 - 0
ios/Permissions/RNPMediaLibrary.m

@@ -0,0 +1,41 @@
+//
+//  RNPPhoto.m
+//  ReactNativePermissions
+//
+//  Created by Yonah Forst on 11/07/16.
+//  Copyright © 2016 Yonah Forst. All rights reserved.
+//
+
+#import "RNPMediaLibrary.h"
+#import <MediaPlayer/MediaPlayer.h>
+
+@implementation RNPMediaLibrary
+
++ (NSString *)getStatus
+{
+    int status = [MPMediaLibrary authorizationStatus];
+    switch (status) {
+        case MPMediaLibraryAuthorizationStatusAuthorized:
+            return RNPStatusAuthorized;
+        case MPMediaLibraryAuthorizationStatusDenied:
+            return RNPStatusDenied;
+        case MPMediaLibraryAuthorizationStatusRestricted:
+            return RNPStatusRestricted;
+        default:
+            return RNPStatusUndetermined;
+    }
+}
+
++ (void)request:(void (^)(NSString *))completionHandler
+{
+    void (^handler)(void) =  ^(void) {
+        dispatch_async(dispatch_get_main_queue(), ^{
+            completionHandler([self.class getStatus]);
+        });
+    };
+    
+    [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
+        handler();
+    }];
+}
+@end

+ 2 - 1
ios/RCTConvert+RNPStatus.h

@@ -32,7 +32,8 @@ typedef NS_ENUM(NSInteger, RNPType) {
     RNPTypeBluetooth,
     RNPTypeNotification,
     RNPTypeBackgroundRefresh,
-    RNPTypeSpeechRecognition
+    RNPTypeSpeechRecognition,
+    RNPTypeMediaLibrary
 };
 
 @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),
+                                @"mediaLibrary": @(RNPTypeMediaLibrary)
                                 }),
                                 RNPTypeUnknown, integerValue)
 

+ 6 - 0
ios/ReactNativePermissions.m

@@ -43,6 +43,7 @@
 #import "RNPContacts.h"
 #import "RNPBackgroundRefresh.h"
 #import "RNPSpeechRecognition.h"
+#import "RNPMediaLibrary.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 RNPTypeMediaLibrary:
+            status = [RNPMediaLibrary 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 RNPTypeMediaLibrary:
+            return [RNPMediaLibrary request:resolve];
         default:
             break;
     }

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

@@ -7,6 +7,8 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		488FE29C200BC8A100E05AB0 /* RNPMediaLibrary.m in Sources */ = {isa = PBXBuildFile; fileRef = 488FE29B200BC8A100E05AB0 /* RNPMediaLibrary.m */; };
+		488FE2A2200BCED100E05AB0 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 488FE2A1200BCEC900E05AB0 /* MediaPlayer.framework */; };
 		669581F71FE4416B008596CD /* RCTConvert+RNPStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581F41FE4416B008596CD /* RCTConvert+RNPStatus.m */; };
 		669581F81FE4416B008596CD /* ReactNativePermissions.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581F51FE4416B008596CD /* ReactNativePermissions.m */; };
 		6695820D1FE441A8008596CD /* RNPSpeechRecognition.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581FD1FE441A7008596CD /* RNPSpeechRecognition.m */; };
@@ -33,6 +35,9 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
+		488FE29B200BC8A100E05AB0 /* RNPMediaLibrary.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNPMediaLibrary.m; sourceTree = "<group>"; };
+		488FE29D200BC8D200E05AB0 /* RNPMediaLibrary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNPMediaLibrary.h; sourceTree = "<group>"; };
+		488FE2A1200BCEC900E05AB0 /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
 		669581F31FE4416B008596CD /* ReactNativePermissions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReactNativePermissions.h; sourceTree = "<group>"; };
 		669581F41FE4416B008596CD /* RCTConvert+RNPStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+RNPStatus.m"; sourceTree = "<group>"; };
 		669581F51FE4416B008596CD /* ReactNativePermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativePermissions.m; sourceTree = "<group>"; };
@@ -63,12 +68,21 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				488FE2A2200BCED100E05AB0 /* MediaPlayer.framework in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
+		488FE2A0200BCEC900E05AB0 /* Frameworks */ = {
+			isa = PBXGroup;
+			children = (
+				488FE2A1200BCEC900E05AB0 /* MediaPlayer.framework */,
+			);
+			name = Frameworks;
+			sourceTree = "<group>";
+		};
 		669581FA1FE44191008596CD /* Permissions */ = {
 			isa = PBXGroup;
 			children = (
@@ -78,6 +92,8 @@
 				669582001FE441A7008596CD /* RNPBackgroundRefresh.m */,
 				669582091FE441A8008596CD /* RNPBluetooth.h */,
 				669581FF1FE441A7008596CD /* RNPBluetooth.m */,
+				488FE29D200BC8D200E05AB0 /* RNPMediaLibrary.h */,
+				488FE29B200BC8A100E05AB0 /* RNPMediaLibrary.m */,
 				669581FB1FE441A7008596CD /* RNPContacts.h */,
 				669582081FE441A8008596CD /* RNPContacts.m */,
 				669582061FE441A7008596CD /* RNPEvent.h */,
@@ -103,6 +119,7 @@
 				669581F31FE4416B008596CD /* ReactNativePermissions.h */,
 				669581F51FE4416B008596CD /* ReactNativePermissions.m */,
 				9D23B3501C767B80008B4819 /* Products */,
+				488FE2A0200BCEC900E05AB0 /* Frameworks */,
 			);
 			sourceTree = "<group>";
 		};
@@ -171,6 +188,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				669582111FE441A8008596CD /* RNPNotification.m in Sources */,
+				488FE29C200BC8A100E05AB0 /* RNPMediaLibrary.m in Sources */,
 				669582151FE441A8008596CD /* RNPEvent.m in Sources */,
 				669582101FE441A8008596CD /* RNPBackgroundRefresh.m in Sources */,
 				669581F71FE4416B008596CD /* RCTConvert+RNPStatus.m in Sources */,

+ 1 - 0
lib/permissions.ios.js

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