ReactNativePermissions.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. case RNPTypeMotion:
  128. status = [RNPMotion getStatus];
  129. break;
  130. default:
  131. break;
  132. }
  133. resolve(status);
  134. }
  135. RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  136. {
  137. NSString *status;
  138. switch (type) {
  139. case RNPTypeLocation:
  140. return [self requestLocation:json resolve:resolve];
  141. case RNPTypeCamera:
  142. return [RNPAudioVideo request:@"video" completionHandler:resolve];
  143. case RNPTypeMicrophone:
  144. return [RNPAudioVideo request:@"audio" completionHandler:resolve];
  145. case RNPTypePhoto:
  146. return [RNPPhoto request:resolve];
  147. case RNPTypeContacts:
  148. return [RNPContacts request:resolve];
  149. case RNPTypeEvent:
  150. return [RNPEvent request:@"event" completionHandler:resolve];
  151. case RNPTypeReminder:
  152. return [RNPEvent request:@"reminder" completionHandler:resolve];
  153. case RNPTypeBluetooth:
  154. return [self requestBluetooth:resolve];
  155. case RNPTypeNotification:
  156. return [self requestNotification:json resolve:resolve];
  157. case RNPTypeSpeechRecognition:
  158. return [RNPSpeechRecognition request:resolve];
  159. case RNPTypeMediaLibrary:
  160. return [RNPMediaLibrary request:resolve];
  161. case RNPTypeMotion:
  162. return [RNPMotion request:resolve];
  163. default:
  164. break;
  165. }
  166. }
  167. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  168. {
  169. if (self.locationMgr == nil) {
  170. self.locationMgr = [[RNPLocation alloc] init];
  171. }
  172. NSString *type = [RCTConvert NSString:json];
  173. [self.locationMgr request:type completionHandler:resolve];
  174. }
  175. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  176. {
  177. NSArray *typeStrings = [RCTConvert NSArray:json];
  178. UIUserNotificationType types;
  179. if ([typeStrings containsObject:@"alert"])
  180. types = types | UIUserNotificationTypeAlert;
  181. if ([typeStrings containsObject:@"badge"])
  182. types = types | UIUserNotificationTypeBadge;
  183. if ([typeStrings containsObject:@"sound"])
  184. types = types | UIUserNotificationTypeSound;
  185. if (self.notificationMgr == nil) {
  186. self.notificationMgr = [[RNPNotification alloc] init];
  187. }
  188. [self.notificationMgr request:types completionHandler:resolve];
  189. }
  190. - (void) requestBluetooth:(RCTPromiseResolveBlock)resolve
  191. {
  192. if (self.bluetoothMgr == nil) {
  193. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  194. }
  195. [self.bluetoothMgr request:resolve];
  196. }
  197. @end