RNPermissionsManager.m 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #import "RNPermissionsManager.h"
  2. #import "RCTConvert+RNPermission.h"
  3. #import <React/RCTLog.h>
  4. #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
  5. #import "RNPermissionHandlerBluetoothPeripheral.h"
  6. #endif
  7. #if __has_include("RNPermissionHandlerCalendars.h")
  8. #import "RNPermissionHandlerCalendars.h"
  9. #endif
  10. #if __has_include("RNPermissionHandlerCamera.h")
  11. #import "RNPermissionHandlerCamera.h"
  12. #endif
  13. #if __has_include("RNPermissionHandlerContacts.h")
  14. #import "RNPermissionHandlerContacts.h"
  15. #endif
  16. #if __has_include("RNPermissionHandlerFaceID.h")
  17. #import "RNPermissionHandlerFaceID.h"
  18. #endif
  19. #if __has_include("RNPermissionHandlerLocationAlways.h")
  20. #import "RNPermissionHandlerLocationAlways.h"
  21. #endif
  22. #if __has_include("RNPermissionHandlerLocationWhenInUse.h")
  23. #import "RNPermissionHandlerLocationWhenInUse.h"
  24. #endif
  25. #if __has_include("RNPermissionHandlerMediaLibrary.h")
  26. #import "RNPermissionHandlerMediaLibrary.h"
  27. #endif
  28. #if __has_include("RNPermissionHandlerMicrophone.h")
  29. #import "RNPermissionHandlerMicrophone.h"
  30. #endif
  31. #if __has_include("RNPermissionHandlerMotion.h")
  32. #import "RNPermissionHandlerMotion.h"
  33. #endif
  34. #if __has_include("RNPermissionHandlerNotifications.h")
  35. #import "RNPermissionHandlerNotifications.h"
  36. #endif
  37. #if __has_include("RNPermissionHandlerPhotoLibrary.h")
  38. #import "RNPermissionHandlerPhotoLibrary.h"
  39. #endif
  40. #if __has_include("RNPermissionHandlerReminders.h")
  41. #import "RNPermissionHandlerReminders.h"
  42. #endif
  43. #if __has_include("RNPermissionHandlerSiri.h")
  44. #import "RNPermissionHandlerSiri.h"
  45. #endif
  46. #if __has_include("RNPermissionHandlerSpeechRecognition.h")
  47. #import "RNPermissionHandlerSpeechRecognition.h"
  48. #endif
  49. #if __has_include("RNPermissionHandlerStoreKit.h")
  50. #import "RNPermissionHandlerStoreKit.h"
  51. #endif
  52. static NSString* requestedKey = @"@RNPermissions:requested";
  53. @implementation RNPermissionsManager
  54. RCT_EXPORT_MODULE(RNPermissions);
  55. + (BOOL)requiresMainQueueSetup {
  56. return YES;
  57. }
  58. - (dispatch_queue_t)methodQueue {
  59. return dispatch_get_main_queue();
  60. }
  61. - (id<RNPermissionHandler> _Nullable)handlerForPermission:(RNPermission)permission {
  62. id<RNPermissionHandler> handler = nil;
  63. switch (permission) {
  64. #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
  65. case RNPermissionBluetoothPeripheral:
  66. handler = [RNPermissionHandlerBluetoothPeripheral new];
  67. break;
  68. #endif
  69. #if __has_include("RNPermissionHandlerCalendars.h")
  70. case RNPermissionCalendars:
  71. handler = [RNPermissionHandlerCalendars new];
  72. break;
  73. #endif
  74. #if __has_include("RNPermissionHandlerCamera.h")
  75. case RNPermissionCamera:
  76. handler = [RNPermissionHandlerCamera new];
  77. break;
  78. #endif
  79. #if __has_include("RNPermissionHandlerContacts.h")
  80. case RNPermissionContacts:
  81. handler = [RNPermissionHandlerContacts new];
  82. break;
  83. #endif
  84. #if __has_include("RNPermissionHandlerFaceID.h")
  85. case RNPermissionFaceID:
  86. handler = [RNPermissionHandlerFaceID new];
  87. break;
  88. #endif
  89. #if __has_include("RNPermissionHandlerLocationAlways.h")
  90. case RNPermissionLocationAlways:
  91. handler = [RNPermissionHandlerLocationAlways new];
  92. break;
  93. #endif
  94. #if __has_include("RNPermissionHandlerLocationWhenInUse.h")
  95. case RNPermissionLocationWhenInUse:
  96. handler = [RNPermissionHandlerLocationWhenInUse new];
  97. break;
  98. #endif
  99. #if __has_include("RNPermissionHandlerMediaLibrary.h")
  100. case RNPermissionMediaLibrary:
  101. handler = [RNPermissionHandlerMediaLibrary new];
  102. break;
  103. #endif
  104. #if __has_include("RNPermissionHandlerMicrophone.h")
  105. case RNPermissionMicrophone:
  106. handler = [RNPermissionHandlerMicrophone new];
  107. break;
  108. #endif
  109. #if __has_include("RNPermissionHandlerMotion.h")
  110. case RNPermissionMotion:
  111. handler = [RNPermissionHandlerMotion new];
  112. break;
  113. #endif
  114. #if __has_include("RNPermissionHandlerNotifications.h")
  115. case RNPermissionNotifications:
  116. handler = [RNPermissionHandlerNotifications new];
  117. break;
  118. #endif
  119. #if __has_include("RNPermissionHandlerPhotoLibrary.h")
  120. case RNPermissionPhotoLibrary:
  121. handler = [RNPermissionHandlerPhotoLibrary new];
  122. break;
  123. #endif
  124. #if __has_include("RNPermissionHandlerReminders.h")
  125. case RNPermissionReminders:
  126. handler = [RNPermissionHandlerReminders new];
  127. break;
  128. #endif
  129. #if __has_include("RNPermissionHandlerSiri.h")
  130. case RNPermissionSiri:
  131. handler = [RNPermissionHandlerSiri new];
  132. break;
  133. #endif
  134. #if __has_include("RNPermissionHandlerSpeechRecognition.h")
  135. case RNPermissionSpeechRecognition:
  136. handler = [RNPermissionHandlerSpeechRecognition new];
  137. break;
  138. #endif
  139. #if __has_include("RNPermissionHandlerStoreKit.h")
  140. case RNPermissionStoreKit:
  141. handler = [RNPermissionHandlerStoreKit new];
  142. break;
  143. #endif
  144. case RNPermissionUnknown:
  145. break; // RCTConvert prevents this case
  146. }
  147. #if RCT_DEV
  148. if (handler != nil && [[handler class] respondsToSelector:@selector(usageDescriptionKeys)]) {
  149. NSArray<NSString *> *usageDescriptionKeys = [[handler class] usageDescriptionKeys];
  150. for (NSString *key in usageDescriptionKeys) {
  151. if (![[NSBundle mainBundle] objectForInfoDictionaryKey:key]) {
  152. RCTLogError(@"Cannot check or request permission without the required \"%@\" entry in your app \"Info.plist\" file.", key);
  153. return nil;
  154. }
  155. }
  156. }
  157. #endif
  158. return handler;
  159. }
  160. - (NSString *)stringForStatus:(RNPermissionStatus)status {
  161. switch (status) {
  162. case RNPermissionStatusNotAvailable:
  163. case RNPermissionStatusRestricted:
  164. return @"unavailable";
  165. case RNPermissionStatusNotDetermined:
  166. return @"denied";
  167. case RNPermissionStatusDenied:
  168. return @"never_ask_again";
  169. case RNPermissionStatusAuthorized:
  170. return @"granted";
  171. }
  172. }
  173. + (bool)hasBackgroundModeEnabled:(NSString *)mode {
  174. NSArray *modes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
  175. return [modes isKindOfClass:[NSArray class]] && [modes containsObject:mode];
  176. }
  177. + (void)logErrorMessage:(NSString *)message {
  178. RCTLogError(@"%@", message);
  179. }
  180. + (bool)hasBeenRequestedOnce:(id<RNPermissionHandler>)handler {
  181. NSArray *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:requestedKey];
  182. return [requested containsObject:NSStringFromClass([handler class])];
  183. }
  184. RCT_REMAP_METHOD(openSettings,
  185. openSettingsWithResolver:(RCTPromiseResolveBlock)resolve
  186. withRejecter:(RCTPromiseRejectBlock)reject) {
  187. UIApplication *sharedApplication = [UIApplication sharedApplication];
  188. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  189. if ([sharedApplication canOpenURL:url]) {
  190. [sharedApplication openURL:url];
  191. resolve(@(true));
  192. } else {
  193. reject(@"cannot_open_settings", @"Cannot open application settings.", nil);
  194. }
  195. }
  196. RCT_REMAP_METHOD(check,
  197. checkWithPermission:(RNPermission)permission
  198. withResolver:(RCTPromiseResolveBlock)resolve
  199. withRejecter:(RCTPromiseRejectBlock)reject) {
  200. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  201. [handler checkWithResolver:^(RNPermissionStatus status) {
  202. resolve([self stringForStatus:status]);
  203. } withRejecter:^(NSError *error) {
  204. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  205. }];
  206. }
  207. RCT_REMAP_METHOD(request,
  208. requestWithPermission:(RNPermission)permission
  209. withOptions:(NSDictionary * _Nullable)options
  210. withResolver:(RCTPromiseResolveBlock)resolve
  211. withRejecter:(RCTPromiseRejectBlock)reject) {
  212. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  213. [handler requestWithOptions:options withResolver:^(RNPermissionStatus status) {
  214. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  215. NSString *handlerClassName = NSStringFromClass([handler class]);
  216. NSMutableArray *requested = [[userDefaults arrayForKey:requestedKey] mutableCopy];
  217. if (requested == nil) {
  218. requested = [NSMutableArray new];
  219. }
  220. if (![requested containsObject:handlerClassName]) {
  221. [requested addObject:handlerClassName];
  222. [userDefaults setObject:requested forKey:requestedKey];
  223. [userDefaults synchronize];
  224. }
  225. resolve([self stringForStatus:status]);
  226. } withRejecter:^(NSError *error) {
  227. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  228. }];
  229. }
  230. @end