瀏覽代碼

Fix MPMediaLibrary not available on simulators / iOS < 9.3

Mathieu Acthernoene 5 年之前
父節點
當前提交
2ec6dec33b
共有 1 個文件被更改,包括 34 次插入17 次删除
  1. 34
    17
      ios/Permissions/RNPMediaLibrary.m

+ 34
- 17
ios/Permissions/RNPMediaLibrary.m 查看文件

@@ -13,28 +13,45 @@
13 13
 
14 14
 + (NSString *)getStatus
15 15
 {
16
-    switch ([MPMediaLibrary authorizationStatus]) {
17
-        case MPMediaLibraryAuthorizationStatusAuthorized:
18
-            return RNPStatusAuthorized;
19
-        case MPMediaLibraryAuthorizationStatusDenied:
20
-            return RNPStatusDenied;
21
-        case MPMediaLibraryAuthorizationStatusRestricted:
22
-            return RNPStatusRestricted;
23
-        default:
24
-            return RNPStatusUndetermined;
16
+#if TARGET_OS_SIMULATOR
17
+    return RNPStatusRestricted;
18
+#else
19
+    if (@available(iOS 9.3, *)) {
20
+        switch ([MPMediaLibrary authorizationStatus]) {
21
+            case MPMediaLibraryAuthorizationStatusAuthorized:
22
+                return RNPStatusAuthorized;
23
+            case MPMediaLibraryAuthorizationStatusDenied:
24
+                return RNPStatusDenied;
25
+            case MPMediaLibraryAuthorizationStatusRestricted:
26
+                return RNPStatusRestricted;
27
+            default:
28
+                return RNPStatusUndetermined;
29
+        }
30
+    } else {
31
+        return RNPStatusAuthorized;
25 32
     }
33
+#endif
26 34
 }
27 35
 
28 36
 + (void)request:(void (^)(NSString *))completionHandler
29 37
 {
30
-    void (^handler)(void) =  ^(void) {
31
-        dispatch_async(dispatch_get_main_queue(), ^{
32
-            completionHandler([self.class getStatus]);
33
-        });
34
-    };
38
+#if TARGET_OS_SIMULATOR
39
+    completionHandler(RNPStatusRestricted);
40
+#else
41
+    if (@available(iOS 9.3, *)) {
42
+        void (^handler)(void) =  ^(void) {
43
+            dispatch_async(dispatch_get_main_queue(), ^{
44
+                completionHandler([self.class getStatus]);
45
+            });
46
+        };
35 47
 
36
-    [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
37
-        handler();
38
-    }];
48
+        [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
49
+            handler();
50
+        }];
51
+    } else {
52
+        completionHandler(RNPStatusAuthorized);
53
+    }
54
+#endif
39 55
 }
56
+
40 57
 @end