Skip to content

Commit 78f993b

Browse files
Rory PickeringRory Pickering
authored andcommitted
mediaLibrary ios added
1 parent f80a885 commit 78f993b

File tree

8 files changed

+89
-2
lines changed

8 files changed

+89
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ The current supported permissions are:
160160
| Push Notifications | `notification` | ✔️ ||
161161
| Background Refresh | `backgroundRefresh` | ✔️ ||
162162
| Speech Recognition | `speechRecognition` | ✔️ ||
163+
| mediaLibrary | `mediaLibrary` | ✔️ ||
163164
| Storage | `storage` | ❌️ ||
164165
| Phone Call | `callPhone` | ❌️ ||
165166
| Read SMS | `readSms` | ❌️ ||
@@ -186,6 +187,7 @@ The current supported permissions are:
186187
* Permission type `notification` accepts a second parameter for `request()`. The
187188
second parameter is an array with the desired alert types. Any combination of
188189
`alert`, `badge` and `sound` (default requests all three).
190+
* If you are not requesting mediaLibrary then you can remove MediaPlayer.framework from the xcode project
189191

190192
```js
191193
// example

ios/Permissions/RNPMediaLibrary.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// RNPMediaLibrary.h
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 11/07/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "RCTConvert+RNPStatus.h"
11+
12+
@interface RNPMediaLibrary : NSObject
13+
14+
+ (NSString *)getStatus;
15+
+ (void)request:(void (^)(NSString *))completionHandler;
16+
17+
@end

ios/Permissions/RNPMediaLibrary.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// RNPPhoto.m
3+
// ReactNativePermissions
4+
//
5+
// Created by Yonah Forst on 11/07/16.
6+
// Copyright © 2016 Yonah Forst. All rights reserved.
7+
//
8+
9+
#import "RNPMediaLibrary.h"
10+
#import <MediaPlayer/MediaPlayer.h>
11+
12+
@implementation RNPMediaLibrary
13+
14+
+ (NSString *)getStatus
15+
{
16+
int status = [MPMediaLibrary authorizationStatus];
17+
switch (status) {
18+
case MPMediaLibraryAuthorizationStatusAuthorized:
19+
return RNPStatusAuthorized;
20+
case MPMediaLibraryAuthorizationStatusDenied:
21+
return RNPStatusDenied;
22+
case MPMediaLibraryAuthorizationStatusRestricted:
23+
return RNPStatusRestricted;
24+
default:
25+
return RNPStatusUndetermined;
26+
}
27+
}
28+
29+
+ (void)request:(void (^)(NSString *))completionHandler
30+
{
31+
void (^handler)(void) = ^(void) {
32+
dispatch_async(dispatch_get_main_queue(), ^{
33+
completionHandler([self.class getStatus]);
34+
});
35+
};
36+
37+
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
38+
handler();
39+
}];
40+
}
41+
@end

ios/RCTConvert+RNPStatus.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ typedef NS_ENUM(NSInteger, RNPType) {
3232
RNPTypeBluetooth,
3333
RNPTypeNotification,
3434
RNPTypeBackgroundRefresh,
35-
RNPTypeSpeechRecognition
35+
RNPTypeSpeechRecognition,
36+
RNPTypeMediaLibrary
3637
};
3738

3839
@interface RCTConvert (RNPStatus)

ios/RCTConvert+RNPStatus.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ @implementation RCTConvert (RNPStatus)
2020
@"bluetooth" : @(RNPTypeBluetooth),
2121
@"notification" : @(RNPTypeNotification),
2222
@"backgroundRefresh": @(RNPTypeBackgroundRefresh),
23-
@"speechRecognition": @(RNPTypeSpeechRecognition)
23+
@"speechRecognition": @(RNPTypeSpeechRecognition),
24+
@"mediaLibrary": @(RNPTypeMediaLibrary)
2425
}),
2526
RNPTypeUnknown, integerValue)
2627

ios/ReactNativePermissions.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#import "RNPContacts.h"
4444
#import "RNPBackgroundRefresh.h"
4545
#import "RNPSpeechRecognition.h"
46+
#import "RNPMediaLibrary.h"
4647

4748
@interface ReactNativePermissions()
4849
@property (strong, nonatomic) RNPLocation *locationMgr;
@@ -145,6 +146,9 @@ - (dispatch_queue_t)methodQueue {
145146
case RNPTypeSpeechRecognition:
146147
status = [RNPSpeechRecognition getStatus];
147148
break;
149+
case RNPTypeMediaLibrary:
150+
status = [RNPMediaLibrary getStatus];
151+
break;
148152
default:
149153
break;
150154
}
@@ -177,6 +181,8 @@ - (dispatch_queue_t)methodQueue {
177181
return [self requestNotification:json resolve:resolve];
178182
case RNPTypeSpeechRecognition:
179183
return [RNPSpeechRecognition request:resolve];
184+
case RNPTypeMediaLibrary:
185+
return [RNPMediaLibrary request:resolve];
180186
default:
181187
break;
182188
}

ios/ReactNativePermissions.xcodeproj/project.pbxproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
488FE29C200BC8A100E05AB0 /* RNPMediaLibrary.m in Sources */ = {isa = PBXBuildFile; fileRef = 488FE29B200BC8A100E05AB0 /* RNPMediaLibrary.m */; };
11+
488FE2A2200BCED100E05AB0 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 488FE2A1200BCEC900E05AB0 /* MediaPlayer.framework */; };
1012
669581F71FE4416B008596CD /* RCTConvert+RNPStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581F41FE4416B008596CD /* RCTConvert+RNPStatus.m */; };
1113
669581F81FE4416B008596CD /* ReactNativePermissions.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581F51FE4416B008596CD /* ReactNativePermissions.m */; };
1214
6695820D1FE441A8008596CD /* RNPSpeechRecognition.m in Sources */ = {isa = PBXBuildFile; fileRef = 669581FD1FE441A7008596CD /* RNPSpeechRecognition.m */; };
@@ -33,6 +35,9 @@
3335
/* End PBXCopyFilesBuildPhase section */
3436

3537
/* Begin PBXFileReference section */
38+
488FE29B200BC8A100E05AB0 /* RNPMediaLibrary.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNPMediaLibrary.m; sourceTree = "<group>"; };
39+
488FE29D200BC8D200E05AB0 /* RNPMediaLibrary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNPMediaLibrary.h; sourceTree = "<group>"; };
40+
488FE2A1200BCEC900E05AB0 /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
3641
669581F31FE4416B008596CD /* ReactNativePermissions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReactNativePermissions.h; sourceTree = "<group>"; };
3742
669581F41FE4416B008596CD /* RCTConvert+RNPStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+RNPStatus.m"; sourceTree = "<group>"; };
3843
669581F51FE4416B008596CD /* ReactNativePermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativePermissions.m; sourceTree = "<group>"; };
@@ -63,12 +68,21 @@
6368
isa = PBXFrameworksBuildPhase;
6469
buildActionMask = 2147483647;
6570
files = (
71+
488FE2A2200BCED100E05AB0 /* MediaPlayer.framework in Frameworks */,
6672
);
6773
runOnlyForDeploymentPostprocessing = 0;
6874
};
6975
/* End PBXFrameworksBuildPhase section */
7076

7177
/* Begin PBXGroup section */
78+
488FE2A0200BCEC900E05AB0 /* Frameworks */ = {
79+
isa = PBXGroup;
80+
children = (
81+
488FE2A1200BCEC900E05AB0 /* MediaPlayer.framework */,
82+
);
83+
name = Frameworks;
84+
sourceTree = "<group>";
85+
};
7286
669581FA1FE44191008596CD /* Permissions */ = {
7387
isa = PBXGroup;
7488
children = (
@@ -78,6 +92,8 @@
7892
669582001FE441A7008596CD /* RNPBackgroundRefresh.m */,
7993
669582091FE441A8008596CD /* RNPBluetooth.h */,
8094
669581FF1FE441A7008596CD /* RNPBluetooth.m */,
95+
488FE29D200BC8D200E05AB0 /* RNPMediaLibrary.h */,
96+
488FE29B200BC8A100E05AB0 /* RNPMediaLibrary.m */,
8197
669581FB1FE441A7008596CD /* RNPContacts.h */,
8298
669582081FE441A8008596CD /* RNPContacts.m */,
8399
669582061FE441A7008596CD /* RNPEvent.h */,
@@ -103,6 +119,7 @@
103119
669581F31FE4416B008596CD /* ReactNativePermissions.h */,
104120
669581F51FE4416B008596CD /* ReactNativePermissions.m */,
105121
9D23B3501C767B80008B4819 /* Products */,
122+
488FE2A0200BCEC900E05AB0 /* Frameworks */,
106123
);
107124
sourceTree = "<group>";
108125
};
@@ -171,6 +188,7 @@
171188
buildActionMask = 2147483647;
172189
files = (
173190
669582111FE441A8008596CD /* RNPNotification.m in Sources */,
191+
488FE29C200BC8A100E05AB0 /* RNPMediaLibrary.m in Sources */,
174192
669582151FE441A8008596CD /* RNPEvent.m in Sources */,
175193
669582101FE441A8008596CD /* RNPBackgroundRefresh.m in Sources */,
176194
669581F71FE4416B008596CD /* RCTConvert+RNPStatus.m in Sources */,

lib/permissions.ios.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const permissionTypes = [
2020
'notification',
2121
'backgroundRefresh',
2222
'speechRecognition',
23+
'mediaLibrary'
2324
]
2425

2526
const DEFAULTS = {

0 commit comments

Comments
 (0)