ReactNativePermissions.m 6.2KB

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