ReactNativePermissions.m 6.4KB

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