ReactNativePermissions.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "RNPMotion.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. #pragma mark Initialization
  50. - (instancetype)init
  51. {
  52. if (self = [super init]) {
  53. }
  54. return self;
  55. }
  56. /**
  57. * run on the main queue.
  58. */
  59. - (dispatch_queue_t)methodQueue {
  60. return dispatch_get_main_queue();
  61. }
  62. RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  63. {
  64. resolve(@(UIApplicationOpenSettingsURLString != nil));
  65. }
  66. RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  67. {
  68. if (@(UIApplicationOpenSettingsURLString != nil)) {
  69. NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
  70. id __block token = [center addObserverForName:UIApplicationDidBecomeActiveNotification
  71. object:nil
  72. queue:nil
  73. usingBlock:^(NSNotification *note) {
  74. [center removeObserver:token];
  75. resolve(@YES);
  76. }];
  77. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  78. [[UIApplication sharedApplication] openURL:url];
  79. }
  80. }
  81. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  82. {
  83. NSString *status;
  84. switch (type) {
  85. case RNPTypeLocation: {
  86. NSString *locationPermissionType = [RCTConvert NSString:json];
  87. status = [RNPLocation getStatusForType:locationPermissionType];
  88. break;
  89. }
  90. case RNPTypeCamera:
  91. status = [RNPAudioVideo getStatus:@"video"];
  92. break;
  93. case RNPTypeMicrophone:
  94. status = [RNPAudioVideo getStatus:@"audio"];
  95. break;
  96. case RNPTypePhoto:
  97. status = [RNPPhoto getStatus];
  98. break;
  99. case RNPTypeContacts:
  100. status = [RNPContacts getStatus];
  101. break;
  102. case RNPTypeEvent:
  103. status = [RNPEvent getStatus:@"event"];
  104. break;
  105. case RNPTypeReminder:
  106. status = [RNPEvent getStatus:@"reminder"];
  107. break;
  108. case RNPTypeBluetooth:
  109. status = [RNPBluetooth getStatus];
  110. break;
  111. case RNPTypeNotification:
  112. status = [RNPNotification getStatus];
  113. break;
  114. case RNPTypeBackgroundRefresh:
  115. status = [RNPBackgroundRefresh getStatus];
  116. break;
  117. case RNPTypeSpeechRecognition:
  118. status = [RNPSpeechRecognition getStatus];
  119. break;
  120. case RNPTypeMotion:
  121. status = [RNPMotion 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. case RNPTypeMotion:
  153. return [RNPMotion request:resolve];
  154. default:
  155. break;
  156. }
  157. }
  158. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  159. {
  160. if (self.locationMgr == nil) {
  161. self.locationMgr = [[RNPLocation alloc] init];
  162. }
  163. NSString *type = [RCTConvert NSString:json];
  164. [self.locationMgr request:type completionHandler:resolve];
  165. }
  166. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  167. {
  168. NSArray *typeStrings = [RCTConvert NSArray:json];
  169. UIUserNotificationType types;
  170. if ([typeStrings containsObject:@"alert"])
  171. types = types | UIUserNotificationTypeAlert;
  172. if ([typeStrings containsObject:@"badge"])
  173. types = types | UIUserNotificationTypeBadge;
  174. if ([typeStrings containsObject:@"sound"])
  175. types = types | UIUserNotificationTypeSound;
  176. if (self.notificationMgr == nil) {
  177. self.notificationMgr = [[RNPNotification alloc] init];
  178. }
  179. [self.notificationMgr request:types completionHandler:resolve];
  180. }
  181. - (void) requestBluetooth:(RCTPromiseResolveBlock)resolve
  182. {
  183. if (self.bluetoothMgr == nil) {
  184. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  185. }
  186. [self.bluetoothMgr request:resolve];
  187. }
  188. @end