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