RNPermissions.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. bool isEnabled = [modes isKindOfClass:[NSArray class]] && [modes containsObject:mode];
  232. #if RCT_DEV
  233. if (!isEnabled) {
  234. RCTLogWarn(@"Missing \"%@\" in \"UIBackgroundModes\" forces check / request result to \"unavailable\" status", mode);
  235. }
  236. #endif
  237. return isEnabled;
  238. }
  239. RCT_REMAP_METHOD(openSettings,
  240. openSettingsWithResolver:(RCTPromiseResolveBlock)resolve
  241. rejecter:(RCTPromiseRejectBlock)reject) {
  242. UIApplication *sharedApplication = [UIApplication sharedApplication];
  243. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  244. if (@available(iOS 10.0, *)) {
  245. [sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
  246. if (success) {
  247. resolve(@(true));
  248. } else {
  249. reject(@"cannot_open_settings", @"Cannot open application settings", nil);
  250. }
  251. }];
  252. } else {
  253. [sharedApplication openURL:url];
  254. resolve(@(true));
  255. }
  256. }
  257. RCT_REMAP_METHOD(check,
  258. checkWithPermission:(RNPermission)permission
  259. resolver:(RCTPromiseResolveBlock)resolve
  260. rejecter:(RCTPromiseRejectBlock)reject) {
  261. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  262. [handler checkWithResolver:^(RNPermissionStatus status) {
  263. NSString *strStatus = [self stringForStatus:status];
  264. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  265. resolve(strStatus);
  266. } rejecter:^(NSError *error) {
  267. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  268. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  269. }];
  270. }
  271. RCT_REMAP_METHOD(request,
  272. requestWithPermission:(RNPermission)permission
  273. resolver:(RCTPromiseResolveBlock)resolve
  274. rejecter:(RCTPromiseRejectBlock)reject) {
  275. id<RNPermissionHandler> handler = [self handlerForPermission:permission];
  276. [handler requestWithResolver:^(RNPermissionStatus status) {
  277. NSString *strStatus = [self stringForStatus:status];
  278. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  279. resolve(strStatus);
  280. } rejecter:^(NSError *error) {
  281. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  282. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  283. }];
  284. }
  285. RCT_REMAP_METHOD(checkNotifications,
  286. checkNotificationsWithResolver:(RCTPromiseResolveBlock)resolve
  287. rejecter:(RCTPromiseRejectBlock)reject) {
  288. #if __has_include("RNPermissionHandlerNotifications.h")
  289. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  290. [handler checkWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
  291. NSString *strStatus = [self stringForStatus:status];
  292. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  293. resolve(@{ @"status": strStatus, @"settings": settings });
  294. } rejecter:^(NSError * _Nonnull error) {
  295. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  296. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  297. }];
  298. #else
  299. reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
  300. #endif
  301. }
  302. RCT_REMAP_METHOD(requestNotifications,
  303. requestNotificationsWithOptions:(NSArray<NSString *> * _Nonnull)options
  304. resolver:(RCTPromiseResolveBlock)resolve
  305. rejecter:(RCTPromiseRejectBlock)reject) {
  306. #if __has_include("RNPermissionHandlerNotifications.h")
  307. RNPermissionHandlerNotifications *handler = [RNPermissionHandlerNotifications new];
  308. [handler requestWithResolver:^(RNPermissionStatus status, NSDictionary * _Nonnull settings) {
  309. NSString *strStatus = [self stringForStatus:status];
  310. NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
  311. resolve(@{ @"status": strStatus, @"settings": settings });
  312. } rejecter:^(NSError * _Nonnull error) {
  313. NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
  314. reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
  315. } options:options];
  316. #else
  317. reject(@"notifications_pod_missing", @"Notifications permission pod is missing", nil);
  318. #endif
  319. }
  320. @end