Bladeren bron

refactor. removed individual methods for checking statusus

Yonah Forst 8 jaren geleden
bovenliggende
commit
24b8ada320
5 gewijzigde bestanden met toevoegingen van 357 en 250 verwijderingen
  1. 25
    0
      PermissionsChecker.h
  2. 243
    0
      PermissionsChecker.m
  3. 65
    2
      ReactNativePermissions.ios.js
  4. 18
    248
      ReactNativePermissions.m
  5. 6
    0
      ReactNativePermissions.xcodeproj/project.pbxproj

+ 25
- 0
PermissionsChecker.h Bestand weergeven

@@ -0,0 +1,25 @@
1
+//
2
+//  PermissionsChecker
3
+//  PermissionsChecker
4
+//
5
+//  Created by Yonah Forst on 18/02/16.
6
+//  Copyright © 2016 Yonah Forst. All rights reserved.
7
+//
8
+#import "RCTConvert+RNPermissionsStatus.h"
9
+
10
+@interface PermissionsChecker : NSObject
11
+
12
++ (BOOL)canOpenSettings;
13
++ (void)openSettings;
14
++ (RNPermissionsStatus)locationPermissionStatus;
15
++ (RNPermissionsStatus)cameraPermissionStatus;
16
++ (RNPermissionsStatus)microphonePermissionStatus;
17
++ (RNPermissionsStatus)photoPermissionStatus;
18
++ (RNPermissionsStatus)contactsPermissionStatus;
19
++ (RNPermissionsStatus)eventPermissionStatus;
20
++ (RNPermissionsStatus)reminderPermissionStatus;
21
++ (RNPermissionsStatus)bluetoothPermissionStatus;
22
++ (RNPermissionsStatus)notificationPermissionStatus;
23
++ (RNPermissionsStatus)backgroundRefreshPermissionStatus;
24
+
25
+@end

+ 243
- 0
PermissionsChecker.m Bestand weergeven

@@ -0,0 +1,243 @@
1
+//
2
+//  PermissionsChecker
3
+//  PermissionsChecker
4
+//
5
+//  Created by Yonah Forst on 18/02/16.
6
+//  Copyright © 2016 Yonah Forst. All rights reserved.
7
+//
8
+
9
+@import Contacts;
10
+
11
+#import "PermissionsChecker.h"
12
+
13
+#import <AddressBook/AddressBook.h>
14
+#import <AssetsLibrary/AssetsLibrary.h>
15
+#import <EventKit/EventKit.h>
16
+#import <CoreLocation/CoreLocation.h>
17
+#import <AVFoundation/AVFoundation.h>
18
+#import <CoreBluetooth/CoreBluetooth.h>
19
+
20
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
21
+@import Contacts;
22
+@import Photos;
23
+#endif
24
+
25
+
26
+@interface PermissionsChecker()
27
+@end
28
+
29
+@implementation PermissionsChecker
30
+
31
++ (BOOL)canOpenSettings
32
+{
33
+    return UIApplicationOpenSettingsURLString != nil;
34
+}
35
+
36
++ (void)openSettings
37
+{
38
+    if ([self canOpenSettings]) {
39
+        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
40
+        [[UIApplication sharedApplication] openURL:url];
41
+    }
42
+}
43
+
44
+
45
++ (RNPermissionsStatus)locationPermissionStatus
46
+{
47
+    int status = [CLLocationManager authorizationStatus];
48
+    switch (status) {
49
+        case kCLAuthorizationStatusAuthorizedAlways:
50
+        case kCLAuthorizationStatusAuthorizedWhenInUse:
51
+            return RNPermissionsStatusAuthorized;
52
+        case kCLAuthorizationStatusDenied:
53
+            return RNPermissionsStatusDenied;
54
+        case kCLAuthorizationStatusRestricted:
55
+            return RNPermissionsStatusRestricted;
56
+        default:
57
+            return RNPermissionsStatusUndetermined;
58
+    }
59
+}
60
+
61
+
62
+
63
+
64
++ (RNPermissionsStatus)cameraPermissionStatus
65
+{
66
+    int status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
67
+    switch (status) {
68
+        case AVAuthorizationStatusAuthorized:
69
+            return RNPermissionsStatusAuthorized;
70
+        case AVAuthorizationStatusDenied:
71
+            return RNPermissionsStatusDenied;
72
+        case AVAuthorizationStatusRestricted:
73
+            return RNPermissionsStatusRestricted;
74
+        default:
75
+            return RNPermissionsStatusUndetermined;
76
+    }
77
+}
78
+
79
++ (RNPermissionsStatus)microphonePermissionStatus
80
+{
81
+    int status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
82
+    switch (status) {
83
+        case AVAuthorizationStatusAuthorized:
84
+            return RNPermissionsStatusAuthorized;
85
+        case AVAuthorizationStatusDenied:
86
+            return RNPermissionsStatusDenied;
87
+        case AVAuthorizationStatusRestricted:
88
+            return RNPermissionsStatusRestricted;
89
+        default:
90
+            return RNPermissionsStatusUndetermined;
91
+    }
92
+}
93
+
94
++ (RNPermissionsStatus)photoPermissionStatus
95
+{
96
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
97
+    int status = [PHPhotoLibrary authorizationStatus];
98
+    switch (status) {
99
+        case PHAuthorizationStatusAuthorized:
100
+            return RNPermissionsStatusAuthorized;
101
+        case PHAuthorizationStatusDenied:
102
+            return RNPermissionsStatusDenied;
103
+        case PHAuthorizationStatusRestricted:
104
+            return RNPermissionsStatusRestricted;
105
+        default:
106
+            return RNPermissionsStatusUndetermined;
107
+    }
108
+#else
109
+    int status = ABAddressBookGetAuthorizationStatus();
110
+    switch (status) {
111
+        case kABAuthorizationStatusAuthorized:
112
+            return RNPermissionsStatusAuthorized;
113
+        case kABAuthorizationStatusDenied:
114
+            return RNPermissionsStatusDenied;
115
+        case kABAuthorizationStatusRestricted:
116
+            return RNPermissionsStatusRestricted;
117
+        default:
118
+            return RNPermissionsStatusUndetermined;
119
+    }
120
+#endif
121
+}
122
+
123
+
124
++ (RNPermissionsStatus)contactsPermissionStatus
125
+{
126
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
127
+    int status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
128
+    switch (status) {
129
+        case CNAuthorizationStatusAuthorized:
130
+            return RNPermissionsStatusAuthorized;
131
+        case CNAuthorizationStatusDenied:
132
+            return RNPermissionsStatusDenied;
133
+        case CNAuthorizationStatusRestricted:
134
+            return RNPermissionsStatusRestricted;
135
+        default:
136
+            return RNPermissionsStatusUndetermined;
137
+    }
138
+#else
139
+    int status = ABAddressBookGetAuthorizationStatus();
140
+    switch (status) {
141
+        case kABAuthorizationStatusAuthorized:
142
+            return RNPermissionsStatusAuthorized;
143
+        case kABAuthorizationStatusDenied:
144
+            return RNPermissionsStatusDenied;
145
+        case kABAuthorizationStatusRestricted:
146
+            return RNPermissionsStatusRestricted;
147
+        default:
148
+            return RNPermissionsStatusUndetermined;
149
+    }
150
+#endif
151
+}
152
+
153
+
154
++ (RNPermissionsStatus)eventPermissionStatus
155
+{
156
+    int status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
157
+    switch (status) {
158
+        case EKAuthorizationStatusAuthorized:
159
+            return RNPermissionsStatusAuthorized;
160
+        case EKAuthorizationStatusDenied:
161
+            return RNPermissionsStatusDenied;
162
+        case EKAuthorizationStatusRestricted:
163
+            return RNPermissionsStatusRestricted;
164
+        default:
165
+            return RNPermissionsStatusUndetermined;
166
+    }
167
+}
168
+
169
++ (RNPermissionsStatus)reminderPermissionStatus
170
+{
171
+    int status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
172
+    switch (status) {
173
+        case EKAuthorizationStatusAuthorized:
174
+            return RNPermissionsStatusAuthorized;
175
+        case EKAuthorizationStatusDenied:
176
+            return RNPermissionsStatusDenied;
177
+        case EKAuthorizationStatusRestricted:
178
+            return RNPermissionsStatusRestricted;
179
+        default:
180
+            return RNPermissionsStatusUndetermined;
181
+    }
182
+}
183
+
184
+
185
++ (RNPermissionsStatus)bluetoothPermissionStatus
186
+{
187
+    int status = [CBPeripheralManager authorizationStatus];
188
+    switch (status) {
189
+        case CBPeripheralManagerAuthorizationStatusAuthorized:
190
+            return RNPermissionsStatusAuthorized;
191
+        case CBPeripheralManagerAuthorizationStatusDenied:
192
+            return RNPermissionsStatusDenied;
193
+        case CBPeripheralManagerAuthorizationStatusRestricted:
194
+            return RNPermissionsStatusRestricted;
195
+        default:
196
+            return RNPermissionsStatusUndetermined;
197
+    }
198
+}
199
+
200
+//problem here is that we can only return Authorized or Undetermined
201
++ (RNPermissionsStatus)notificationPermissionStatus
202
+{
203
+    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
204
+        // iOS8+
205
+        BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
206
+        BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
207
+        if (isRegistered || isEnabled) {
208
+            return isEnabled ? RNPermissionsStatusAuthorized : RNPermissionsStatusDenied;
209
+        }
210
+        else {
211
+            return RNPermissionsStatusUndetermined;
212
+        }
213
+    } else {
214
+#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
215
+        if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
216
+            return RNPermissionsStatusUndetermined;
217
+        }
218
+        else {
219
+            return RNPermissionsStatusAuthorized;
220
+        }
221
+#else
222
+        return RNPermissionsStatusUndetermined;
223
+#endif
224
+        
225
+    }
226
+}
227
+
228
++ (RNPermissionsStatus)backgroundRefreshPermissionStatus
229
+{
230
+    int status = [[UIApplication sharedApplication] backgroundRefreshStatus];
231
+    switch (status) {
232
+        case UIBackgroundRefreshStatusAvailable:
233
+            return RNPermissionsStatusAuthorized;
234
+        case UIBackgroundRefreshStatusDenied:
235
+            return RNPermissionsStatusDenied;
236
+        case UIBackgroundRefreshStatusRestricted:
237
+            return RNPermissionsStatusRestricted;
238
+        default:
239
+            return RNPermissionsStatusUndetermined;
240
+    }
241
+}
242
+
243
+@end

+ 65
- 2
ReactNativePermissions.ios.js Bestand weergeven

@@ -1,6 +1,69 @@
1 1
 'use strict';
2 2
 
3 3
 var React = require('react-native');
4
-var Heading = React.NativeModules.ReactNativePermissions;
4
+var RNPermissions = React.NativeModules.ReactNativePermissions;
5 5
 
6
-module.exports = Heading;
6
+const permissionTypes = [
7
+	'location',
8
+	'camera',
9
+	'microphone',
10
+	'photo',
11
+	'contacts',
12
+	'event',
13
+	'reminder',
14
+	'bluetooth',
15
+	'notification',
16
+	'backgroundRefresh',
17
+]
18
+
19
+const permissionStatus = [
20
+	'undetermined',
21
+	'denied',
22
+	'authorized',
23
+	'restricted'
24
+]
25
+
26
+class ReactNativePermissions {
27
+	canOpenSettings() {
28
+		return RNPermissions.canOpenSettings()
29
+	}
30
+
31
+	openSettings() {
32
+		return RNPermissions.openSettings()
33
+	}
34
+
35
+	getPermissionStatus(permission) {
36
+		if (permissionTypes.includes(permission)) {
37
+			return RNPermissions.getPermissionStatus(permission)
38
+		} else {
39
+			return Promise.reject(`ReactNativePermissions: ${permission} is not a valid permission type`)
40
+		}
41
+	}
42
+
43
+	//recursive funciton to chain a promises for a list of permissions
44
+	checkMultiplePermissions(permissions) {
45
+		let i = permissions.length
46
+		let that = this
47
+		const obj = {}
48
+		function processNext() {
49
+			i--
50
+			let p = permissions[i]
51
+			
52
+			if (!p) {
53
+				return obj
54
+			}
55
+
56
+			return that.getPermissionStatus(p)
57
+				.then(res => {
58
+					obj[p] = res
59
+					return processNext()
60
+				}).catch(e => {
61
+					console.warn(e)
62
+					return processNext()
63
+				})
64
+		}
65
+		return processNext()
66
+	}
67
+}
68
+
69
+export default new ReactNativePermissions();

+ 18
- 248
ReactNativePermissions.m Bestand weergeven

@@ -16,18 +16,7 @@
16 16
 
17 17
 #import "RCTConvert+RNPermissionsStatus.h"
18 18
 
19
-#import <AddressBook/AddressBook.h>
20
-#import <AssetsLibrary/AssetsLibrary.h>
21
-#import <EventKit/EventKit.h>
22
-#import <CoreLocation/CoreLocation.h>
23
-#import <AVFoundation/AVFoundation.h>
24
-#import <CoreBluetooth/CoreBluetooth.h>
25
-
26
-#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
27
-@import Contacts;
28
-@import Photos;
29
-#endif
30
-
19
+#import "PermissionsChecker.h"
31 20
 
32 21
 @interface ReactNativePermissions()
33 22
 @end
@@ -57,249 +46,30 @@ RCT_EXPORT_MODULE();
57 46
 };
58 47
 
59 48
 
60
-- (BOOL)canOpenSettings {
61
-    return UIApplicationOpenSettingsURLString != nil;
62
-}
63
-
64
-RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
65
-{
66
-    resolve(@([self canOpenSettings]));
67
-}
68
-
69
-RCT_EXPORT_METHOD(openSettings)
70
-{
71
-    if ([self canOpenSettings]) {
72
-        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
73
-        [[UIApplication sharedApplication] openURL:url];
74
-    }
75
-}
76
-
77
-
78
-RCT_REMAP_METHOD(locationPermissionStatus, locationPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
79
-{
80
-    int status = [CLLocationManager authorizationStatus];
81
-    switch (status) {
82
-        case kCLAuthorizationStatusAuthorizedAlways:
83
-        case kCLAuthorizationStatusAuthorizedWhenInUse:
84
-            return resolve(@(RNPermissionsStatusAuthorized));
85
-            
86
-        case kCLAuthorizationStatusDenied:
87
-            return resolve(@(RNPermissionsStatusDenied));
88
-            
89
-        case kCLAuthorizationStatusRestricted:
90
-            return resolve(@(RNPermissionsStatusRestricted));
91
-            
92
-        default:
93
-            return resolve(@(RNPermissionsStatusUndetermined));
94
-    }
95
-}
96
-
97
-
98
-RCT_REMAP_METHOD(cameraPermissionStatus, cameraPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
99
-{
100
-    int status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
101
-    switch (status) {
102
-        case AVAuthorizationStatusAuthorized:
103
-            return resolve(@(RNPermissionsStatusAuthorized));
104
-            
105
-        case AVAuthorizationStatusDenied:
106
-            return resolve(@(RNPermissionsStatusDenied));
107
-            
108
-        case AVAuthorizationStatusRestricted:
109
-            return resolve(@(RNPermissionsStatusRestricted));
110
-            
111
-        default:
112
-            return resolve(@(RNPermissionsStatusUndetermined));
113
-    }
114
-
115
-}
116
-
117
-RCT_REMAP_METHOD(microphonePermissionStatus, microphonePermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
118
-{
119
-    int status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
120
-    switch (status) {
121
-        case AVAuthorizationStatusAuthorized:
122
-            return resolve(@(RNPermissionsStatusAuthorized));
123
-            
124
-        case AVAuthorizationStatusDenied:
125
-            return resolve(@(RNPermissionsStatusDenied));
126
-            
127
-        case AVAuthorizationStatusRestricted:
128
-            return resolve(@(RNPermissionsStatusRestricted));
129
-            
130
-        default:
131
-            return resolve(@(RNPermissionsStatusUndetermined));
132
-    }
133
-
134
-}
135
-
136
-RCT_REMAP_METHOD(photoPermissionStatus, photoPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
137
-
138
-{
139
-    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
140
-        int status = [PHPhotoLibrary authorizationStatus];
141
-        switch (status) {
142
-            case PHAuthorizationStatusAuthorized:
143
-                return resolve(@(RNPermissionsStatusAuthorized));
144
-                
145
-            case PHAuthorizationStatusDenied:
146
-                return resolve(@(RNPermissionsStatusDenied));
147
-                
148
-            case PHAuthorizationStatusRestricted:
149
-                return resolve(@(RNPermissionsStatusRestricted));
150
-                
151
-            default:
152
-                return resolve(@(RNPermissionsStatusUndetermined));
153
-        }
154
-    #else
155
-        int status = ABAddressBookGetAuthorizationStatus();
156
-        switch (status) {
157
-            case kABAuthorizationStatusAuthorized:
158
-                return resolve(@(RNPermissionsStatusAuthorized));
159
-                
160
-            case kABAuthorizationStatusDenied:
161
-                return resolve(@(RNPermissionsStatusDenied));
162
-                
163
-            case kABAuthorizationStatusRestricted:
164
-                return resolve(@(RNPermissionsStatusRestricted));
165
-                
166
-            default:
167
-                return resolve(@(RNPermissionsStatusUndetermined));
168
-        }
169
-    #endif
170
-
171
-}
172
-
173
-RCT_REMAP_METHOD(contactsPermissionStatus, contactsPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
174
-{
175
-    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
176
-        int status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
177
-        switch (status) {
178
-            case CNAuthorizationStatusAuthorized:
179
-                return resolve(@(RNPermissionsStatusAuthorized));
180
-                
181
-            case CNAuthorizationStatusDenied:
182
-                return resolve(@(RNPermissionsStatusDenied));
183
-                
184
-            case CNAuthorizationStatusRestricted:
185
-                return resolve(@(RNPermissionsStatusRestricted));
186
-                
187
-            default:
188
-                return resolve(@(RNPermissionsStatusUndetermined));
189
-        }
190
-    #else
191
-        int status = ABAddressBookGetAuthorizationStatus();
192
-        switch (status) {
193
-            case kABAuthorizationStatusAuthorized:
194
-                return resolve(@(RNPermissionsStatusAuthorized));
195
-                
196
-            case kABAuthorizationStatusDenied:
197
-                return resolve(@(RNPermissionsStatusDenied));
198
-                
199
-            case kABAuthorizationStatusRestricted:
200
-                return resolve(@(RNPermissionsStatusRestricted));
201
-                
202
-            default:
203
-                return resolve(@(RNPermissionsStatusUndetermined));
204
-        }
205
-    #endif
206
-}
207
-
208
-
209
-RCT_REMAP_METHOD(eventPermissionStatus, eventPermission:(NSString *)eventString resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
210
-{
211
-    int status;
212
-    if ([eventString isEqualToString:@"reminder"]) {
213
-        status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
214
-    } else if ([eventString isEqualToString:@"event"]) {
215
-        status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
216
-    } else {
217
-        NSError *error = [NSError errorWithDomain:@"invalidOption" code:-1 userInfo:NULL];
218
-        return reject(@"-1", @"Type must be 'reminder' or 'event'", error);
219
-    }
220
-    
221
-    switch (status) {
222
-        case EKAuthorizationStatusAuthorized:
223
-            return resolve(@(RNPermissionsStatusAuthorized));
224
-            
225
-        case EKAuthorizationStatusDenied:
226
-            return resolve(@(RNPermissionsStatusDenied));
227
-            
228
-        case EKAuthorizationStatusRestricted:
229
-            return resolve(@(RNPermissionsStatusRestricted));
230
-            
231
-        default:
232
-            return resolve(@(RNPermissionsStatusUndetermined));
233
-    }
49
+RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
50
+    resolve(@([PermissionsChecker canOpenSettings]));
234 51
 }
235
-
236
-
237
-
238
-RCT_REMAP_METHOD(bluetoothPermissionStatus, bluetoothPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
239
-{
240
-    int status = [CBPeripheralManager authorizationStatus];
241
-    
242
-    switch (status) {
243
-        case CBPeripheralManagerAuthorizationStatusAuthorized:
244
-            return resolve(@(RNPermissionsStatusAuthorized));
245
-            
246
-        case CBPeripheralManagerAuthorizationStatusDenied:
247
-            return resolve(@(RNPermissionsStatusDenied));
248
-            
249
-        case CBPeripheralManagerAuthorizationStatusRestricted:
250
-            return resolve(@(RNPermissionsStatusRestricted));
251
-            
252
-        default:
253
-            return resolve(@(RNPermissionsStatusUndetermined));
254
-    }
255
-    
52
+RCT_EXPORT_METHOD(openSettings) {
53
+    [PermissionsChecker openSettings];
256 54
 }
257
-
258
-//problem here is that we can we can't know if the user was never prompted for permission, or if they were prompted and deneied
259
-RCT_REMAP_METHOD(notificationPermissionStatus, notificationPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
260
-{
261
-    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
262
-        // iOS8+
263
-        BOOL isRegistered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
264
-        BOOL isEnabled = [[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone;
265
-        if (isRegistered || isEnabled) {
266
-            return resolve(@(isEnabled ? RNPermissionsStatusAuthorized : RNPermissionsStatusDenied));
267
-        }
268
-        else {
269
-            return resolve(@(RNPermissionsStatusUndetermined));
270
-        }
271
-    } else {
272
-        #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
273
-            if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
274
-                return resolve(@(RNPermissionsStatusUndetermined));
275
-            }
276
-            else {
277
-                return resolve(@(RNPermissionsStatusAuthorized));
278
-            }
279
-        #else
280
-            return resolve(@(RNPermissionsStatusUndetermined));
281
-        #endif
282
-    }
55
+RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(NSString *)permission resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
56
+    SEL s = NSSelectorFromString([NSString stringWithFormat:@"%@PermissionStatus", permission]);
57
+    RNPermissionsStatus status = (RNPermissionsStatus)[PermissionsChecker performSelector:s];
58
+    resolve([self stringForStatus:status]);
283 59
 }
284 60
 
285
-RCT_REMAP_METHOD(backgroundRefreshStatus, backgroundRefresh:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
286
-{
287
-    int status = [[UIApplication sharedApplication] backgroundRefreshStatus];
288
-    
61
+- (NSString *)stringForStatus:(RNPermissionsStatus) status{
289 62
     switch (status) {
290
-        case UIBackgroundRefreshStatusAvailable:
291
-            return resolve(@(RNPermissionsStatusAuthorized));
292
-            
293
-        case UIBackgroundRefreshStatusDenied:
294
-            return resolve(@(RNPermissionsStatusDenied));
295
-            
296
-        case UIBackgroundRefreshStatusRestricted:
297
-            return resolve(@(RNPermissionsStatusRestricted));
298
-            
63
+        case RNPermissionsStatusAuthorized:
64
+            return @"authorized";
65
+        case RNPermissionsStatusDenied:
66
+            return @"denied";
67
+        case RNPermissionsStatusRestricted:
68
+            return @"restricted";
69
+        case RNPermissionsStatusUndetermined:
299 70
         default:
300
-            return resolve(@(RNPermissionsStatusUndetermined));
71
+            return @"undetermined";
301 72
     }
302
-    
303 73
 }
304 74
 
305 75
 @end

+ 6
- 0
ReactNativePermissions.xcodeproj/project.pbxproj Bestand weergeven

@@ -7,6 +7,7 @@
7 7
 	objects = {
8 8
 
9 9
 /* Begin PBXBuildFile section */
10
+		9D8FB2701D2D68C500AAFC55 /* PermissionsChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D8FB26F1D2D68C500AAFC55 /* PermissionsChecker.m */; };
10 11
 		9DE8D2821CA3188D009CE8CC /* ReactNativePermissions.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DE8D2811CA3188D009CE8CC /* ReactNativePermissions.m */; };
11 12
 		9DE8D28B1CA31E95009CE8CC /* RCTConvert+RNPermissionsStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DE8D28A1CA31E95009CE8CC /* RCTConvert+RNPermissionsStatus.m */; };
12 13
 /* End PBXBuildFile section */
@@ -25,6 +26,8 @@
25 26
 
26 27
 /* Begin PBXFileReference section */
27 28
 		9D23B34F1C767B80008B4819 /* libReactNativePermissions.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libReactNativePermissions.a; sourceTree = BUILT_PRODUCTS_DIR; };
29
+		9D8FB26E1D2D68C500AAFC55 /* PermissionsChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PermissionsChecker.h; sourceTree = SOURCE_ROOT; };
30
+		9D8FB26F1D2D68C500AAFC55 /* PermissionsChecker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PermissionsChecker.m; sourceTree = SOURCE_ROOT; };
28 31
 		9DE8D2801CA31888009CE8CC /* ReactNativePermissions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReactNativePermissions.h; sourceTree = SOURCE_ROOT; };
29 32
 		9DE8D2811CA3188D009CE8CC /* ReactNativePermissions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativePermissions.m; sourceTree = SOURCE_ROOT; };
30 33
 		9DE8D2891CA31E95009CE8CC /* RCTConvert+RNPermissionsStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+RNPermissionsStatus.h"; sourceTree = SOURCE_ROOT; };
@@ -63,6 +66,8 @@
63 66
 			children = (
64 67
 				9DE8D2891CA31E95009CE8CC /* RCTConvert+RNPermissionsStatus.h */,
65 68
 				9DE8D28A1CA31E95009CE8CC /* RCTConvert+RNPermissionsStatus.m */,
69
+				9D8FB26E1D2D68C500AAFC55 /* PermissionsChecker.h */,
70
+				9D8FB26F1D2D68C500AAFC55 /* PermissionsChecker.m */,
66 71
 				9DE8D2801CA31888009CE8CC /* ReactNativePermissions.h */,
67 72
 				9DE8D2811CA3188D009CE8CC /* ReactNativePermissions.m */,
68 73
 			);
@@ -126,6 +131,7 @@
126 131
 			buildActionMask = 2147483647;
127 132
 			files = (
128 133
 				9DE8D28B1CA31E95009CE8CC /* RCTConvert+RNPermissionsStatus.m in Sources */,
134
+				9D8FB2701D2D68C500AAFC55 /* PermissionsChecker.m in Sources */,
129 135
 				9DE8D2821CA3188D009CE8CC /* ReactNativePermissions.m in Sources */,
130 136
 			);
131 137
 			runOnlyForDeploymentPostprocessing = 0;