RNPermissions.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 *)insertHandler:(id<RNPermissionHandler>)handler {
  217. if(_handlers == nil){
  218. _handlers = [NSMutableDictionary new];
  219. }
  220. NSString *randomId = [[NSUUID UUID] UUIDString];
  221. [_handlers setObject:handler forKey:randomId];
  222. return randomId;
  223. }
  224. + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
  225. NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
  226. return requested == nil ? false : [requested containsObject:handlerId];
  227. }
  228. + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
  229. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  230. NSMutableArray *requested = [[userDefaults arrayForKey:SETTING_KEY] mutableCopy];
  231. if (requested == nil) {
  232. requested = [NSMutableArray new];
  233. }
  234. if (![requested containsObject:handlerId]) {
  235. [requested addObject:handlerId];
  236. [userDefaults setObject:requested forKey:SETTING_KEY];
  237. [userDefaults synchronize];
  238. }
  239. }
  240. RCT_REMAP_METHOD(openSettings,
  241. openSettingsWithResolver:(RCTPromiseResolveBlock)resolve
  242. rejecter:(RCTPromiseRejectBlock)reject) {
  243. UIApplication *sharedApplication = [UIApplication sharedApplication];
  244. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  245. if (@available(iOS 10.0, *)) {
  246. [sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
  247. if (success) {
  248. resolve(@(true));
  249. } else {
  250. reject(@"cannot_open_settings", @"Cannot open application settings", nil);
  251. }
  252. }];
  253. } else {
  254. [sharedApplication openURL:url];
  255. resolve(@(true));
  256. }
  257. }
  258. RCT_REMAP_METHOD(check,
  259. checkWithPermission:(RNPermission)permission
  260. resolver:(RCTPromiseResolveBlock)resolve
  261. rejecter:(RCTPromiseRejectBlock)reject) {
  262. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  263. NSString *randomId = [self insertHandler: handler];
  264. [handler checkWithResolver:^(RNPermissionStatus status) {
  265. NSString *strStatus = [self stringForStatus:status];
  266. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  267. resolve(strStatus);
  268. [self.handlers removeObjectForKey:randomId];
  269. } rejecter:^(NSError *error) {
  270. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  271. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  272. [self.handlers removeObjectForKey:randomId];
  273. }];
  274. }
  275. RCT_REMAP_METHOD(request,
  276. requestWithPermission:(RNPermission)permission
  277. resolver:(RCTPromiseResolveBlock)resolve
  278. rejecter:(RCTPromiseRejectBlock)reject) {
  279. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  280. NSString *randomId = [self insertHandler: handler];
  281. [handler requestWithResolver:^(RNPermissionStatus status) {
  282. NSString *strStatus = [self stringForStatus:status];
  283. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  284. resolve(strStatus);
  285. [self.handlers removeObjectForKey:randomId];
  286. } rejecter:^(NSError *error) {
  287. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  288. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  289. [self.handlers removeObjectForKey:randomId];
  290. }];
  291. }
  292. RCT_REMAP_METHOD(checkNotifications,
  293. checkNotificationsWithResolver:(RCTPromiseResolveBlock)resolve
  294. rejecter:(RCTPromiseRejectBlock)reject) {
  295. #if __has_include("RNPermissionHandlerNotifications.h")
  296. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  297. [handler checkWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
  298. NSString *strStatus = [self stringForStatus:status];
  299. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  300. resolve(@{ @"status": strStatus, @"settings": settings });
  301. } rejecter:^(NSError * _Nonnull error) {
  302. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  303. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  304. }];
  305. #else
  306. reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
  307. #endif
  308. }
  309. RCT_REMAP_METHOD(requestNotifications,
  310. requestNotificationsWithOptions:(NSArray<NSString *> * _Nonnull)options
  311. resolver:(RCTPromiseResolveBlock)resolve
  312. rejecter:(RCTPromiseRejectBlock)reject) {
  313. #if __has_include("RNPermissionHandlerNotifications.h")
  314. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  315. [handler requestWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
  316. NSString *strStatus = [self stringForStatus:status];
  317. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  318. resolve(@{ @"status": strStatus, @"settings": settings });
  319. } rejecter:^(NSError * _Nonnull error) {
  320. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  321. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  322. } options:options];
  323. #else
  324. reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
  325. #endif
  326. }
  327. @end