RNPermissions.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. @interface RNPermissions()
  102. @property (nonatomic, strong) NSMutableDictionary<NSString *, id<RNPermissionHandler>> *_Nonnull handlers;
  103. @end
  104. @implementation RNPermissions
  105. RCT_EXPORT_MODULE();
  106. + (BOOL)requiresMainQueueSetup {
  107. return YES;
  108. }
  109. - (dispatch_queue_t)methodQueue {
  110. return dispatch_get_main_queue();
  111. }
  112. - (id<RNPermissionHandler> _Nullable)handlerForPermission:(RNPermission)permission {
  113. id<RNPermissionHandler> handler = nil;
  114. switch (permission) {
  115. #if __has_include("RNPermissionHandlerBluetoothPeripheral.h")
  116. case RNPermissionBluetoothPeripheral:
  117. handler = [RNPermissionHandlerBluetoothPeripheral new];
  118. break;
  119. #endif
  120. #if __has_include("RNPermissionHandlerCalendars.h")
  121. case RNPermissionCalendars:
  122. handler = [RNPermissionHandlerCalendars new];
  123. break;
  124. #endif
  125. #if __has_include("RNPermissionHandlerCamera.h")
  126. case RNPermissionCamera:
  127. handler = [RNPermissionHandlerCamera new];
  128. break;
  129. #endif
  130. #if __has_include("RNPermissionHandlerContacts.h")
  131. case RNPermissionContacts:
  132. handler = [RNPermissionHandlerContacts new];
  133. break;
  134. #endif
  135. #if __has_include("RNPermissionHandlerFaceID.h")
  136. case RNPermissionFaceID:
  137. handler = [RNPermissionHandlerFaceID new];
  138. break;
  139. #endif
  140. #if __has_include("RNPermissionHandlerLocationAlways.h")
  141. case RNPermissionLocationAlways:
  142. handler = [RNPermissionHandlerLocationAlways new];
  143. break;
  144. #endif
  145. #if __has_include("RNPermissionHandlerLocationWhenInUse.h")
  146. case RNPermissionLocationWhenInUse:
  147. handler = [RNPermissionHandlerLocationWhenInUse new];
  148. break;
  149. #endif
  150. #if __has_include("RNPermissionHandlerMediaLibrary.h")
  151. case RNPermissionMediaLibrary:
  152. handler = [RNPermissionHandlerMediaLibrary new];
  153. break;
  154. #endif
  155. #if __has_include("RNPermissionHandlerMicrophone.h")
  156. case RNPermissionMicrophone:
  157. handler = [RNPermissionHandlerMicrophone new];
  158. break;
  159. #endif
  160. #if __has_include("RNPermissionHandlerMotion.h")
  161. case RNPermissionMotion:
  162. handler = [RNPermissionHandlerMotion new];
  163. break;
  164. #endif
  165. #if __has_include("RNPermissionHandlerPhotoLibrary.h")
  166. case RNPermissionPhotoLibrary:
  167. handler = [RNPermissionHandlerPhotoLibrary new];
  168. break;
  169. #endif
  170. #if __has_include("RNPermissionHandlerReminders.h")
  171. case RNPermissionReminders:
  172. handler = [RNPermissionHandlerReminders new];
  173. break;
  174. #endif
  175. #if __has_include("RNPermissionHandlerSiri.h")
  176. case RNPermissionSiri:
  177. handler = [RNPermissionHandlerSiri new];
  178. break;
  179. #endif
  180. #if __has_include("RNPermissionHandlerSpeechRecognition.h")
  181. case RNPermissionSpeechRecognition:
  182. handler = [RNPermissionHandlerSpeechRecognition new];
  183. break;
  184. #endif
  185. #if __has_include("RNPermissionHandlerStoreKit.h")
  186. case RNPermissionStoreKit:
  187. handler = [RNPermissionHandlerStoreKit new];
  188. break;
  189. #endif
  190. case RNPermissionUnknown:
  191. break; // RCTConvert prevents this case
  192. }
  193. #if RCT_DEV
  194. for (NSString *key in [[handler class] usageDescriptionKeys]) {
  195. if (![[NSBundle mainBundle] objectForInfoDictionaryKey:key]) {
  196. RCTLogError(@"Cannot check or request permission without the required \"%@\" entry in your app \"Info.plist\" file", key);
  197. return nil;
  198. }
  199. }
  200. #endif
  201. return handler;
  202. }
  203. - (NSString *)stringForStatus:(RNPermissionStatus)status {
  204. switch (status) {
  205. case RNPermissionStatusNotAvailable:
  206. case RNPermissionStatusRestricted:
  207. return @"unavailable";
  208. case RNPermissionStatusNotDetermined:
  209. return @"denied";
  210. case RNPermissionStatusDenied:
  211. return @"blocked";
  212. case RNPermissionStatusAuthorized:
  213. return @"granted";
  214. }
  215. }
  216. - (NSString *)lockHandler:(id<RNPermissionHandler>)handler {
  217. if (_handlers == nil) {
  218. _handlers = [NSMutableDictionary new];
  219. }
  220. NSString *lockId = [[NSUUID UUID] UUIDString];
  221. [_handlers setObject:handler forKey:lockId];
  222. return lockId;
  223. }
  224. - (void)unlockHandler:(NSString * _Nonnull)lockId {
  225. if (_handlers != nil) {
  226. [self.handlers removeObjectForKey:lockId];
  227. }
  228. }
  229. + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
  230. NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
  231. return requested == nil ? false : [requested containsObject:handlerId];
  232. }
  233. + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
  234. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  235. NSMutableArray *requested = [[userDefaults arrayForKey:SETTING_KEY] mutableCopy];
  236. if (requested == nil) {
  237. requested = [NSMutableArray new];
  238. }
  239. if (![requested containsObject:handlerId]) {
  240. [requested addObject:handlerId];
  241. [userDefaults setObject:requested forKey:SETTING_KEY];
  242. [userDefaults synchronize];
  243. }
  244. }
  245. RCT_REMAP_METHOD(openSettings,
  246. openSettingsWithResolver:(RCTPromiseResolveBlock)resolve
  247. rejecter:(RCTPromiseRejectBlock)reject) {
  248. UIApplication *sharedApplication = [UIApplication sharedApplication];
  249. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  250. if (@available(iOS 10.0, *)) {
  251. [sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
  252. if (success) {
  253. resolve(@(true));
  254. } else {
  255. reject(@"cannot_open_settings", @"Cannot open application settings", nil);
  256. }
  257. }];
  258. } else {
  259. [sharedApplication openURL:url];
  260. resolve(@(true));
  261. }
  262. }
  263. RCT_REMAP_METHOD(check,
  264. checkWithPermission:(RNPermission)permission
  265. resolver:(RCTPromiseResolveBlock)resolve
  266. rejecter:(RCTPromiseRejectBlock)reject) {
  267. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  268. NSString *lockId = [self lockHandler:handler];
  269. [handler checkWithResolver:^(RNPermissionStatus status) {
  270. NSString *strStatus = [self stringForStatus:status];
  271. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  272. resolve(strStatus);
  273. [self unlockHandler:lockId];
  274. } rejecter:^(NSError *error) {
  275. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  276. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  277. [self unlockHandler:lockId];
  278. }];
  279. }
  280. RCT_REMAP_METHOD(request,
  281. requestWithPermission:(RNPermission)permission
  282. resolver:(RCTPromiseResolveBlock)resolve
  283. rejecter:(RCTPromiseRejectBlock)reject) {
  284. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  285. NSString *lockId = [self lockHandler:handler];
  286. [handler requestWithResolver:^(RNPermissionStatus status) {
  287. NSString *strStatus = [self stringForStatus:status];
  288. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  289. resolve(strStatus);
  290. [self unlockHandler:lockId];
  291. } rejecter:^(NSError *error) {
  292. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  293. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  294. [self unlockHandler:lockId];
  295. }];
  296. }
  297. RCT_REMAP_METHOD(checkNotifications,
  298. checkNotificationsWithResolver:(RCTPromiseResolveBlock)resolve
  299. rejecter:(RCTPromiseRejectBlock)reject) {
  300. #if __has_include("RNPermissionHandlerNotifications.h")
  301. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  302. NSString *lockId = [self lockHandler:(id<RNPermissionHandler>)handler];
  303. [handler checkWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
  304. NSString *strStatus = [self stringForStatus:status];
  305. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  306. resolve(@{ @"status": strStatus, @"settings": settings });
  307. [self unlockHandler:lockId];
  308. } rejecter:^(NSError * _Nonnull error) {
  309. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  310. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  311. [self unlockHandler:lockId];
  312. }];
  313. #else
  314. reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
  315. #endif
  316. }
  317. RCT_REMAP_METHOD(requestNotifications,
  318. requestNotificationsWithOptions:(NSArray<NSString *> * _Nonnull)options
  319. resolver:(RCTPromiseResolveBlock)resolve
  320. rejecter:(RCTPromiseRejectBlock)reject) {
  321. #if __has_include("RNPermissionHandlerNotifications.h")
  322. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  323. NSString *lockId = [self lockHandler:(id<RNPermissionHandler>)handler];
  324. [handler requestWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
  325. NSString *strStatus = [self stringForStatus:status];
  326. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  327. resolve(@{ @"status": strStatus, @"settings": settings });
  328. [self unlockHandler:lockId];
  329. } rejecter:^(NSError * _Nonnull error) {
  330. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  331. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  332. [self unlockHandler:lockId];
  333. } options:options];
  334. #else
  335. reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
  336. #endif
  337. }
  338. @end