Browse Source

Fix Bluetooth permission

Mathieu Acthernoene 6 years ago
parent
commit
a4e415e5d9

+ 1
- 1
index.js View File

@@ -22,7 +22,7 @@ export const ANDROID_PERMISSIONS = {
22 22
 // }
23 23
 
24 24
 export const IOS_PERMISSIONS = {
25
-  BLUETOOTH_PERIPHERICAL: "BLUETOOTH_PERIPHERICAL",
25
+  BLUETOOTH_PERIPHERAL: "BLUETOOTH_PERIPHERAL",
26 26
   CALENDARS: "CALENDARS",
27 27
   CAMERA: "CAMERA",
28 28
   CONTACTS: "CONTACTS",

+ 18
- 6
ios/PermissionHandlers/BluetoothPeripheral/RNPermissionHandlerBluetoothPeripheral.m View File

@@ -12,12 +12,19 @@
12 12
 
13 13
 @implementation RNPermissionHandlerBluetoothPeripheral
14 14
 
15
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
16
-  return [RNPermissionsManager hasBackgroundModeEnabled:@"bluetooth-peripheral"] ? @[@"NSBluetoothPeripheralUsageDescription"] : nil;
15
++ (NSArray<NSString *> *)usageDescriptionKeys {
16
+  return @[@"NSBluetoothPeripheralUsageDescription"];
17 17
 }
18 18
 
19 19
 - (void)checkWithResolver:(void (^)(RNPermissionStatus status))resolve
20 20
              withRejecter:(void (__unused ^)(NSError *error))reject {
21
+#if TARGET_OS_SIMULATOR
22
+  return resolve(RNPermissionStatusNotAvailable);
23
+#else
24
+  if (![RNPermissionsManager hasBackgroundModeEnabled:@"bluetooth-peripheral"]) {
25
+    return resolve(RNPermissionStatusNotAvailable);
26
+  }
27
+
21 28
   switch ([CBPeripheralManager authorizationStatus]) {
22 29
     case CBPeripheralManagerAuthorizationStatusNotDetermined:
23 30
       return resolve(RNPermissionStatusNotDetermined);
@@ -28,27 +35,32 @@
28 35
     case CBPeripheralManagerAuthorizationStatusAuthorized:
29 36
       return resolve(RNPermissionStatusAuthorized);
30 37
   }
38
+#endif
31 39
 }
32 40
 
33 41
 - (void)requestWithOptions:(__unused NSDictionary * _Nullable)options
34 42
               withResolver:(void (^)(RNPermissionStatus status))resolve
35 43
               withRejecter:(void (^)(NSError *error))reject {
44
+  if (![RNPermissionsManager hasBackgroundModeEnabled:@"bluetooth-peripheral"]) {
45
+    return resolve(RNPermissionStatusNotAvailable);
46
+  }
47
+
36 48
   _resolve = resolve;
37 49
   _reject = reject;
38
-  
50
+
39 51
   _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:@{
40 52
     CBPeripheralManagerOptionShowPowerAlertKey: @false,
41 53
   }];
42
-  
54
+
43 55
   [_peripheralManager startAdvertising:@{}];
44 56
 }
45 57
 
46 58
 - (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)peripheral {
47 59
   CBManagerState state = peripheral.state;
48
-  
60
+
49 61
   [_peripheralManager stopAdvertising];
50 62
   _peripheralManager = nil;
51
-  
63
+
52 64
   switch (state) {
53 65
     case CBManagerStatePoweredOn:
54 66
       return [self checkWithResolver:_resolve withRejecter:_reject];

+ 1
- 1
ios/PermissionHandlers/Calendars/RNPermissionHandlerCalendars.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerCalendars
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSCalendarsUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/Camera/RNPermissionHandlerCamera.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerCamera
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSCameraUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/Contacts/RNPermissionHandlerContacts.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerContacts
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSContactsUsageDescription"];
9 9
 }
10 10
 

+ 4
- 4
ios/PermissionHandlers/FaceID/RNPermissionHandlerFaceID.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerFaceID
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSFaceIDUsageDescription"];
9 9
 }
10 10
 
@@ -60,9 +60,9 @@
60 60
       return resolve(RNPermissionStatusAuthorized);
61 61
     }
62 62
 
63
-    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSFaceIDUsageDescription"] reply:^(BOOL success, NSError * _Nullable evaluationError) {
64
-      if (evaluationError != nil) {
65
-        [self handleError:evaluationError withResolver:resolve withRejecter:reject];
63
+    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSFaceIDUsageDescription"] reply:^(BOOL success, NSError * _Nullable error) {
64
+      if (error != nil) {
65
+        [self handleError:error withResolver:resolve withRejecter:reject];
66 66
       } else {
67 67
         resolve(success ? RNPermissionStatusAuthorized : RNPermissionStatusDenied);
68 68
       }

+ 1
- 1
ios/PermissionHandlers/LocationAlways/RNPermissionHandlerLocationAlways.m View File

@@ -14,7 +14,7 @@
14 14
 
15 15
 @implementation RNPermissionHandlerLocationAlways
16 16
 
17
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
17
++ (NSArray<NSString *> *)usageDescriptionKeys {
18 18
   return @[
19 19
     @"NSLocationAlwaysAndWhenInUseUsageDescription",
20 20
     @"NSLocationAlwaysUsageDescription",

+ 1
- 1
ios/PermissionHandlers/LocationWhenInUse/RNPermissionHandlerLocationWhenInUse.m View File

@@ -13,7 +13,7 @@
13 13
 
14 14
 @implementation RNPermissionHandlerLocationWhenInUse
15 15
 
16
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
16
++ (NSArray<NSString *> *)usageDescriptionKeys {
17 17
   return @[@"NSLocationWhenInUseUsageDescription"];
18 18
 }
19 19
 

+ 1
- 1
ios/PermissionHandlers/MediaLibrary/RNPermissionHandlerMediaLibrary.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerMediaLibrary
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSAppleMusicUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/Microphone/RNPermissionHandlerMicrophone.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerMicrophone
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSMicrophoneUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/Motion/RNPermissionHandlerMotion.m View File

@@ -13,7 +13,7 @@ static NSString* handlerKey = @"motion";
13 13
 
14 14
 @implementation RNPermissionHandlerMotion
15 15
 
16
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
16
++ (NSArray<NSString *> *)usageDescriptionKeys {
17 17
   return @[@"NSMotionUsageDescription"];
18 18
 }
19 19
 

+ 1
- 1
ios/PermissionHandlers/PhotoLibrary/RNPermissionHandlerPhotoLibrary.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerPhotoLibrary
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSPhotoLibraryUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/Reminders/RNPermissionHandlerReminders.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerReminders
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSRemindersUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/Siri/RNPermissionHandlerSiri.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerSiri
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSSiriUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/PermissionHandlers/SpeechRecognition/RNPermissionHandlerSpeechRecognition.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 @implementation RNPermissionHandlerSpeechRecognition
6 6
 
7
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys {
7
++ (NSArray<NSString *> *)usageDescriptionKeys {
8 8
   return @[@"NSSpeechRecognitionUsageDescription"];
9 9
 }
10 10
 

+ 1
- 1
ios/RCTConvert+RNPermission.m View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 RCT_ENUM_CONVERTER(RNPermission, (@{
6 6
 #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
7
-  @"BLUETOOTH_PERIPHERICAL": @(RNPermissionBluetoothPeripheral),
7
+  @"BLUETOOTH_PERIPHERAL": @(RNPermissionBluetoothPeripheral),
8 8
 #endif
9 9
 #if __has_include("RNPermissionHandlerCalendars.h")
10 10
   @"CALENDARS": @(RNPermissionCalendars),

+ 1
- 1
ios/RNPermissionsManager.h View File

@@ -12,7 +12,7 @@ typedef enum {
12 12
 
13 13
 @optional
14 14
 
15
-+ (NSArray<NSString *> * _Nullable)usageDescriptionKeys;
15
++ (NSArray<NSString *> *)usageDescriptionKeys;
16 16
 
17 17
 @required
18 18
 

+ 4
- 6
ios/RNPermissionsManager.m View File

@@ -158,12 +158,10 @@ RCT_EXPORT_MODULE(RNPermissions);
158 158
   if (handler != nil && [[handler class] respondsToSelector:@selector(usageDescriptionKeys)]) {
159 159
     NSArray<NSString *> *usageDescriptionKeys = [[handler class] usageDescriptionKeys];
160 160
 
161
-    if (usageDescriptionKeys != nil) {
162
-      for (NSString *key in usageDescriptionKeys) {
163
-        if (![[NSBundle mainBundle] objectForInfoDictionaryKey:key]) {
164
-          RCTLogError(@"Cannot check or request permission without the required \"%@\" entry in your app \"Info.plist\" file.", key);
165
-          return nil;
166
-        }
161
+    for (NSString *key in usageDescriptionKeys) {
162
+      if (![[NSBundle mainBundle] objectForInfoDictionaryKey:key]) {
163
+        RCTLogError(@"Cannot check or request permission without the required \"%@\" entry in your app \"Info.plist\" file.", key);
164
+        return nil;
167 165
       }
168 166
     }
169 167
   }