ReactNativePermissions.m 6.4KB

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