ReactNativePermissions.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // ReactNativePermissions.m
  3. // ReactNativePermissions
  4. //
  5. // Created by Yonah Forst on 18/02/16.
  6. // Copyright © 2016 Yonah Forst. All rights reserved.
  7. //
  8. @import Contacts;
  9. #import "ReactNativePermissions.h"
  10. #if __has_include(<React/RCTBridge.h>)
  11. #import <React/RCTBridge.h>
  12. #elif __has_include("React/RCTBridge.h")
  13. #import "React/RCTBridge.h"
  14. #else
  15. #import "RCTBridge.h"
  16. #endif
  17. #if __has_include(<React/RCTConvert.h>)
  18. #import <React/RCTConvert.h>
  19. #elif __has_include("React/RCTConvert.h")
  20. #import "React/RCTConvert.h"
  21. #else
  22. #import "RCTConvert.h"
  23. #endif
  24. #if __has_include(<React/RCTEventDispatcher.h>)
  25. #import <React/RCTEventDispatcher.h>
  26. #elif __has_include("React/RCTEventDispatcher.h")
  27. #import "React/RCTEventDispatcher.h"
  28. #else
  29. #import "RCTEventDispatcher.h"
  30. #endif
  31. #import "RNPLocation.h"
  32. #import "RNPBluetooth.h"
  33. #import "RNPNotification.h"
  34. #import "RNPAudioVideo.h"
  35. #import "RNPEvent.h"
  36. #import "RNPPhoto.h"
  37. #import "RNPContacts.h"
  38. #import "RNPBackgroundRefresh.h"
  39. #import "RNPSpeechRecognition.h"
  40. #import "RNPMediaLibrary.h"
  41. #import "RNPMotion.h"
  42. @interface ReactNativePermissions()
  43. @property (strong, nonatomic) RNPLocation *locationMgr;
  44. @property (strong, nonatomic) RNPNotification *notificationMgr;
  45. @property (strong, nonatomic) RNPBluetooth *bluetoothMgr;
  46. @end
  47. @implementation ReactNativePermissions
  48. RCT_EXPORT_MODULE();
  49. @synthesize bridge = _bridge;
  50. + (BOOL)requiresMainQueueSetup
  51. {
  52. return YES;
  53. }
  54. #pragma mark Initialization
  55. - (instancetype)init
  56. {
  57. if (self = [super init]) {
  58. }
  59. return self;
  60. }
  61. /**
  62. * run on the main queue.
  63. */
  64. - (dispatch_queue_t)methodQueue {
  65. return dispatch_get_main_queue();
  66. }
  67. RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  68. {
  69. resolve(@(UIApplicationOpenSettingsURLString != nil));
  70. }
  71. RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  72. {
  73. if (@(UIApplicationOpenSettingsURLString != nil)) {
  74. NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
  75. id __block token = [center addObserverForName:UIApplicationDidBecomeActiveNotification
  76. object:nil
  77. queue:nil
  78. usingBlock:^(NSNotification *note) {
  79. [center removeObserver:token];
  80. resolve(@YES);
  81. }];
  82. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  83. [[UIApplication sharedApplication] openURL:url];
  84. }
  85. }
  86. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  87. {
  88. NSString *status;
  89. switch (type) {
  90. case RNPTypeLocation: {
  91. NSString *locationPermissionType = [RCTConvert NSString:json];
  92. status = [RNPLocation getStatusForType:locationPermissionType];
  93. break;
  94. }
  95. case RNPTypeCamera:
  96. status = [RNPAudioVideo getStatus:@"video"];
  97. break;
  98. case RNPTypeMicrophone:
  99. status = [RNPAudioVideo getStatus:@"audio"];
  100. break;
  101. case RNPTypePhoto:
  102. status = [RNPPhoto getStatus];
  103. break;
  104. case RNPTypeContacts:
  105. status = [RNPContacts getStatus];
  106. break;
  107. case RNPTypeEvent:
  108. status = [RNPEvent getStatus:@"event"];
  109. break;
  110. case RNPTypeReminder:
  111. status = [RNPEvent getStatus:@"reminder"];
  112. break;
  113. case RNPTypeBluetooth:
  114. status = [RNPBluetooth getStatus];
  115. break;
  116. case RNPTypeNotification:
  117. status = [RNPNotification getStatus];
  118. break;
  119. case RNPTypeBackgroundRefresh:
  120. status = [RNPBackgroundRefresh getStatus];
  121. break;
  122. case RNPTypeSpeechRecognition:
  123. status = [RNPSpeechRecognition getStatus];
  124. break;
  125. case RNPTypeMediaLibrary:
  126. status = [RNPMediaLibrary getStatus];
  127. break;
  128. case RNPTypeMotion:
  129. status = [RNPMotion getStatus];
  130. break;
  131. default:
  132. break;
  133. }
  134. resolve(status);
  135. }
  136. RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  137. {
  138. NSString *status;
  139. switch (type) {
  140. case RNPTypeLocation:
  141. return [self requestLocation:json resolve:resolve];
  142. case RNPTypeCamera:
  143. return [RNPAudioVideo request:@"video" completionHandler:resolve];
  144. case RNPTypeMicrophone:
  145. return [RNPAudioVideo request:@"audio" completionHandler:resolve];
  146. case RNPTypePhoto:
  147. return [RNPPhoto request:resolve];
  148. case RNPTypeContacts:
  149. return [RNPContacts request:resolve];
  150. case RNPTypeEvent:
  151. return [RNPEvent request:@"event" completionHandler:resolve];
  152. case RNPTypeReminder:
  153. return [RNPEvent request:@"reminder" completionHandler:resolve];
  154. case RNPTypeBluetooth:
  155. return [self requestBluetooth:resolve];
  156. case RNPTypeNotification:
  157. return [self requestNotification:json resolve:resolve];
  158. case RNPTypeSpeechRecognition:
  159. return [RNPSpeechRecognition request:resolve];
  160. case RNPTypeMediaLibrary:
  161. return [RNPMediaLibrary request:resolve];
  162. case RNPTypeMotion:
  163. return [RNPMotion request:resolve];
  164. default:
  165. break;
  166. }
  167. }
  168. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  169. {
  170. if (self.locationMgr == nil) {
  171. self.locationMgr = [[RNPLocation alloc] init];
  172. }
  173. NSString *type = [RCTConvert NSString:json];
  174. [self.locationMgr request:type completionHandler:resolve];
  175. }
  176. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  177. {
  178. NSArray *typeStrings = [RCTConvert NSArray:json];
  179. UIUserNotificationType types;
  180. if ([typeStrings containsObject:@"alert"])
  181. types = types | UIUserNotificationTypeAlert;
  182. if ([typeStrings containsObject:@"badge"])
  183. types = types | UIUserNotificationTypeBadge;
  184. if ([typeStrings containsObject:@"sound"])
  185. types = types | UIUserNotificationTypeSound;
  186. if (self.notificationMgr == nil) {
  187. self.notificationMgr = [[RNPNotification alloc] init];
  188. }
  189. [self.notificationMgr request:types completionHandler:resolve];
  190. }
  191. - (void) requestBluetooth:(RCTPromiseResolveBlock)resolve
  192. {
  193. if (self.bluetoothMgr == nil) {
  194. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  195. }
  196. [self.bluetoothMgr request:resolve];
  197. }
  198. @end