ReactNativePermissions.m 6.6KB

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