|
@@ -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
|