RNPermissions.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #import "RNPermissions.h"
  2. #import <React/RCTLog.h>
  3. #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
  4. #import "RNPermissionHandlerBluetoothPeripheral.h"
  5. #endif
  6. #if __has_include("RNPermissionHandlerCalendars.h")
  7. #import "RNPermissionHandlerCalendars.h"
  8. #endif
  9. #if __has_include("RNPermissionHandlerCamera.h")
  10. #import "RNPermissionHandlerCamera.h"
  11. #endif
  12. #if __has_include("RNPermissionHandlerContacts.h")
  13. #import "RNPermissionHandlerContacts.h"
  14. #endif
  15. #if __has_include("RNPermissionHandlerFaceID.h")
  16. #import "RNPermissionHandlerFaceID.h"
  17. #endif
  18. #if __has_include("RNPermissionHandlerLocationAlways.h")
  19. #import "RNPermissionHandlerLocationAlways.h"
  20. #endif
  21. #if __has_include("RNPermissionHandlerLocationWhenInUse.h")
  22. #import "RNPermissionHandlerLocationWhenInUse.h"
  23. #endif
  24. #if __has_include("RNPermissionHandlerMediaLibrary.h")
  25. #import "RNPermissionHandlerMediaLibrary.h"
  26. #endif
  27. #if __has_include("RNPermissionHandlerMicrophone.h")
  28. #import "RNPermissionHandlerMicrophone.h"
  29. #endif
  30. #if __has_include("RNPermissionHandlerMotion.h")
  31. #import "RNPermissionHandlerMotion.h"
  32. #endif
  33. #if __has_include("RNPermissionHandlerNotifications.h")
  34. #import "RNPermissionHandlerNotifications.h"
  35. #endif
  36. #if __has_include("RNPermissionHandlerPhotoLibrary.h")
  37. #import "RNPermissionHandlerPhotoLibrary.h"
  38. #endif
  39. #if __has_include("RNPermissionHandlerReminders.h")
  40. #import "RNPermissionHandlerReminders.h"
  41. #endif
  42. #if __has_include("RNPermissionHandlerSiri.h")
  43. #import "RNPermissionHandlerSiri.h"
  44. #endif
  45. #if __has_include("RNPermissionHandlerSpeechRecognition.h")
  46. #import "RNPermissionHandlerSpeechRecognition.h"
  47. #endif
  48. #if __has_include("RNPermissionHandlerStoreKit.h")
  49. #import "RNPermissionHandlerStoreKit.h"
  50. #endif
  51. static NSString* SETTING_KEY = @"@RNPermissions:Requested";
  52. @implementation RCTConvert(RNPermission)
  53. RCT_ENUM_CONVERTER(RNPermission, (@{
  54. #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
  55. [RNPermissionHandlerBluetoothPeripheral handlerUniqueId]: @(RNPermissionBluetoothPeripheral),
  56. #endif
  57. #if __has_include("RNPermissionHandlerCalendars.h")
  58. [RNPermissionHandlerCalendars handlerUniqueId]: @(RNPermissionCalendars),
  59. #endif
  60. #if __has_include("RNPermissionHandlerCamera.h")
  61. [RNPermissionHandlerCamera handlerUniqueId]: @(RNPermissionCamera),
  62. #endif
  63. #if __has_include("RNPermissionHandlerContacts.h")
  64. [RNPermissionHandlerContacts handlerUniqueId]: @(RNPermissionContacts),
  65. #endif
  66. #if __has_include("RNPermissionHandlerFaceID.h")
  67. [RNPermissionHandlerFaceID handlerUniqueId]: @(RNPermissionFaceID),
  68. #endif
  69. #if __has_include("RNPermissionHandlerLocationAlways.h")
  70. [RNPermissionHandlerLocationAlways handlerUniqueId]: @(RNPermissionLocationAlways),
  71. #endif
  72. #if __has_include("RNPermissionHandlerLocationWhenInUse.h")
  73. [RNPermissionHandlerLocationWhenInUse handlerUniqueId]: @(RNPermissionLocationWhenInUse),
  74. #endif
  75. #if __has_include("RNPermissionHandlerMediaLibrary.h")
  76. [RNPermissionHandlerMediaLibrary handlerUniqueId]: @(RNPermissionMediaLibrary),
  77. #endif
  78. #if __has_include("RNPermissionHandlerMicrophone.h")
  79. [RNPermissionHandlerMicrophone handlerUniqueId]: @(RNPermissionMicrophone),
  80. #endif
  81. #if __has_include("RNPermissionHandlerMotion.h")
  82. [RNPermissionHandlerMotion handlerUniqueId]: @(RNPermissionMotion),
  83. #endif
  84. #if __has_include("RNPermissionHandlerPhotoLibrary.h")
  85. [RNPermissionHandlerPhotoLibrary handlerUniqueId]: @(RNPermissionPhotoLibrary),
  86. #endif
  87. #if __has_include("RNPermissionHandlerReminders.h")
  88. [RNPermissionHandlerReminders handlerUniqueId]: @(RNPermissionReminders),
  89. #endif
  90. #if __has_include("RNPermissionHandlerSiri.h")
  91. [RNPermissionHandlerSiri handlerUniqueId]: @(RNPermissionSiri),
  92. #endif
  93. #if __has_include("RNPermissionHandlerSpeechRecognition.h")
  94. [RNPermissionHandlerSpeechRecognition handlerUniqueId]: @(RNPermissionSpeechRecognition),
  95. #endif
  96. #if __has_include("RNPermissionHandlerStoreKit.h")
  97. [RNPermissionHandlerStoreKit handlerUniqueId]: @(RNPermissionStoreKit),
  98. #endif
  99. }), RNPermissionUnknown, integerValue);
  100. @end
  101. @implementation RNPermissions
  102. RCT_EXPORT_MODULE();
  103. + (BOOL)requiresMainQueueSetup {
  104. return YES;
  105. }
  106. - (dispatch_queue_t)methodQueue {
  107. return dispatch_get_main_queue();
  108. }
  109. - (id<RNPermissionHandler> _Nullable)handlerForPermission:(RNPermission)permission {
  110. id<RNPermissionHandler> handler = nil;
  111. switch (permission) {
  112. #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
  113. case RNPermissionBluetoothPeripheral:
  114. handler = [RNPermissionHandlerBluetoothPeripheral new];
  115. break;
  116. #endif
  117. #if __has_include("RNPermissionHandlerCalendars.h")
  118. case RNPermissionCalendars:
  119. handler = [RNPermissionHandlerCalendars new];
  120. break;
  121. #endif
  122. #if __has_include("RNPermissionHandlerCamera.h")
  123. case RNPermissionCamera:
  124. handler = [RNPermissionHandlerCamera new];
  125. break;
  126. #endif
  127. #if __has_include("RNPermissionHandlerContacts.h")
  128. case RNPermissionContacts:
  129. handler = [RNPermissionHandlerContacts new];
  130. break;
  131. #endif
  132. #if __has_include("RNPermissionHandlerFaceID.h")
  133. case RNPermissionFaceID:
  134. handler = [RNPermissionHandlerFaceID new];
  135. break;
  136. #endif
  137. #if __has_include("RNPermissionHandlerLocationAlways.h")
  138. case RNPermissionLocationAlways:
  139. handler = [RNPermissionHandlerLocationAlways new];
  140. break;
  141. #endif
  142. #if __has_include("RNPermissionHandlerLocationWhenInUse.h")
  143. case RNPermissionLocationWhenInUse:
  144. handler = [RNPermissionHandlerLocationWhenInUse new];
  145. break;
  146. #endif
  147. #if __has_include("RNPermissionHandlerMediaLibrary.h")
  148. case RNPermissionMediaLibrary:
  149. handler = [RNPermissionHandlerMediaLibrary new];
  150. break;
  151. #endif
  152. #if __has_include("RNPermissionHandlerMicrophone.h")
  153. case RNPermissionMicrophone:
  154. handler = [RNPermissionHandlerMicrophone new];
  155. break;
  156. #endif
  157. #if __has_include("RNPermissionHandlerMotion.h")
  158. case RNPermissionMotion:
  159. handler = [RNPermissionHandlerMotion new];
  160. break;
  161. #endif
  162. #if __has_include("RNPermissionHandlerPhotoLibrary.h")
  163. case RNPermissionPhotoLibrary:
  164. handler = [RNPermissionHandlerPhotoLibrary new];
  165. break;
  166. #endif
  167. #if __has_include("RNPermissionHandlerReminders.h")
  168. case RNPermissionReminders:
  169. handler = [RNPermissionHandlerReminders new];
  170. break;
  171. #endif
  172. #if __has_include("RNPermissionHandlerSiri.h")
  173. case RNPermissionSiri:
  174. handler = [RNPermissionHandlerSiri new];
  175. break;
  176. #endif
  177. #if __has_include("RNPermissionHandlerSpeechRecognition.h")
  178. case RNPermissionSpeechRecognition:
  179. handler = [RNPermissionHandlerSpeechRecognition new];
  180. break;
  181. #endif
  182. #if __has_include("RNPermissionHandlerStoreKit.h")
  183. case RNPermissionStoreKit:
  184. handler = [RNPermissionHandlerStoreKit new];
  185. break;
  186. #endif
  187. case RNPermissionUnknown:
  188. break; // RCTConvert prevents this case
  189. }
  190. #if RCT_DEV
  191. for (NSString *key in [[handler class] usageDescriptionKeys]) {
  192. if (![[NSBundle mainBundle] objectForInfoDictionaryKey:key]) {
  193. RCTLogError(@"Cannot check or request permission without the required \"%@\" entry in your app \"Info.plist\" file", key);
  194. return nil;
  195. }
  196. }
  197. #endif
  198. return handler;
  199. }
  200. - (NSString *)stringForStatus:(RNPermissionStatus)status {
  201. switch (status) {
  202. case RNPermissionStatusNotAvailable:
  203. case RNPermissionStatusRestricted:
  204. return @"unavailable";
  205. case RNPermissionStatusNotDetermined:
  206. return @"denied";
  207. case RNPermissionStatusDenied:
  208. return @"blocked";
  209. case RNPermissionStatusAuthorized:
  210. return @"granted";
  211. }
  212. }
  213. + (void)setRequested:(id<RNPermissionHandler> _Nonnull)handler {
  214. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  215. NSString *handlerUniqueId = [[handler class] handlerUniqueId];
  216. NSMutableArray *requested = [[userDefaults arrayForKey:SETTING_KEY] mutableCopy];
  217. if (requested == nil) {
  218. requested = [NSMutableArray new];
  219. }
  220. if (![requested containsObject:handlerUniqueId]) {
  221. [requested addObject:handlerUniqueId];
  222. [userDefaults setObject:requested forKey:SETTING_KEY];
  223. [userDefaults synchronize];
  224. }
  225. }
  226. + (bool)hasAlreadyBeenRequested:(id<RNPermissionHandler> _Nonnull)handler {
  227. NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
  228. return requested == nil ? false : [requested containsObject:[[handler class] handlerUniqueId]];
  229. }
  230. + (bool)isBackgroundModeEnabled:(NSString * _Nonnull)mode {
  231. NSArray *modes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
  232. return [modes isKindOfClass:[NSArray class]] && [modes containsObject:mode];
  233. }
  234. RCT_REMAP_METHOD(openSettings,
  235. openSettingsWithResolver:(RCTPromiseResolveBlock)resolve
  236. rejecter:(RCTPromiseRejectBlock)reject) {
  237. UIApplication *sharedApplication = [UIApplication sharedApplication];
  238. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  239. if (@available(iOS 10.0, *)) {
  240. [sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
  241. if (success) {
  242. resolve(@(true));
  243. } else {
  244. reject(@"cannot_open_settings", @"Cannot open application settings", nil);
  245. }
  246. }];
  247. } else {
  248. [sharedApplication openURL:url];
  249. resolve(@(true));
  250. }
  251. }
  252. RCT_REMAP_METHOD(check,
  253. checkWithPermission:(RNPermission)permission
  254. resolver:(RCTPromiseResolveBlock)resolve
  255. rejecter:(RCTPromiseRejectBlock)reject) {
  256. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  257. [handler checkWithResolver:^(RNPermissionStatus status) {
  258. resolve([self stringForStatus:status]);
  259. } rejecter:^(NSError *error) {
  260. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  261. }];
  262. }
  263. RCT_REMAP_METHOD(request,
  264. requestWithPermission:(RNPermission)permission
  265. resolver:(RCTPromiseResolveBlock)resolve
  266. rejecter:(RCTPromiseRejectBlock)reject) {
  267. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  268. [handler requestWithResolver:^(RNPermissionStatus status) {
  269. [RNPermissions setRequested:handler];
  270. resolve([self stringForStatus:status]);
  271. } rejecter:^(NSError *error) {
  272. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  273. }];
  274. }
  275. RCT_REMAP_METHOD(checkNotifications,
  276. checkNotificationsWithResolver:(RCTPromiseResolveBlock)resolve
  277. rejecter:(RCTPromiseRejectBlock)reject) {
  278. #if __has_include("RNPermissionHandlerNotifications.h")
  279. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  280. [handler checkWithResolver:^(RNPermissionStatus status) {
  281. [handler getSettingsWithResolver:^(NSDictionary * _Nonnull settings) {
  282. resolve(@{ @"status": [self stringForStatus:status], @"settings": settings });
  283. }];
  284. } rejecter:^(NSError * _Nonnull error) {
  285. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  286. }];
  287. #else
  288. reject(@"notification_missing", @"Notification permission handler is missing", nil);
  289. #endif
  290. }
  291. RCT_REMAP_METHOD(requestNotifications,
  292. requestNotificationsWithOptions:(NSArray<NSString *> * _Nonnull)options
  293. resolver:(RCTPromiseResolveBlock)resolve
  294. rejecter:(RCTPromiseRejectBlock)reject) {
  295. #if __has_include("RNPermissionHandlerNotifications.h")
  296. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  297. [handler requestWithResolver:^(RNPermissionStatus status) {
  298. [handler getSettingsWithResolver:^(NSDictionary * _Nonnull settings) {
  299. resolve(@{ @"status": [self stringForStatus:status], @"settings": settings });
  300. }];
  301. } rejecter:^(NSError * _Nonnull error) {
  302. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  303. } options:options];
  304. #else
  305. reject(@"notification_missing", @"Notification permission handler is missing", nil);
  306. #endif
  307. }
  308. @end