ReactNativePermissions.m 6.3KB

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