ReactNativePermissions.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #import "RCTBridge.h"
  11. #import "RCTConvert.h"
  12. #import "RCTEventDispatcher.h"
  13. #import "RNPLocation.h"
  14. #import "RNPBluetooth.h"
  15. #import "RNPNotification.h"
  16. #import "RNPAudioVideo.h"
  17. #import "RNPEvent.h"
  18. #import "RNPPhoto.h"
  19. #import "RNPContacts.h"
  20. #import "RNPBackgroundRefresh.h"
  21. @interface ReactNativePermissions()
  22. @property (strong, nonatomic) RNPLocation *locationMgr;
  23. @property (strong, nonatomic) RNPNotification *notificationMgr;
  24. @property (strong, nonatomic) RNPBluetooth *bluetoothMgr;
  25. @end
  26. @implementation ReactNativePermissions
  27. RCT_EXPORT_MODULE();
  28. @synthesize bridge = _bridge;
  29. #pragma mark Initialization
  30. - (instancetype)init
  31. {
  32. if (self = [super init]) {
  33. }
  34. return self;
  35. }
  36. /**
  37. * run on the main queue.
  38. */
  39. - (dispatch_queue_t)methodQueue {
  40. return dispatch_get_main_queue();
  41. }
  42. RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  43. {
  44. resolve(@(UIApplicationOpenSettingsURLString != nil));
  45. }
  46. RCT_EXPORT_METHOD(openSettings)
  47. {
  48. if (@(UIApplicationOpenSettingsURLString != nil)) {
  49. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  50. [[UIApplication sharedApplication] openURL:url];
  51. }
  52. }
  53. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  54. {
  55. NSString *status;
  56. switch (type) {
  57. case RNPTypeLocation:
  58. status = [RNPLocation getStatus];
  59. break;
  60. case RNPTypeCamera:
  61. status = [RNPAudioVideo getStatus:@"video"];
  62. break;
  63. case RNPTypeMicrophone:
  64. status = [RNPAudioVideo getStatus:@"audio"];
  65. break;
  66. case RNPTypePhoto:
  67. status = [RNPPhoto getStatus];
  68. break;
  69. case RNPTypeContacts:
  70. status = [RNPContacts getStatus];
  71. break;
  72. case RNPTypeEvent:
  73. status = [RNPEvent getStatus:@"event"];
  74. break;
  75. case RNPTypeReminder:
  76. status = [RNPEvent getStatus:@"reminder"];
  77. break;
  78. case RNPTypeBluetooth:
  79. status = [RNPBluetooth getStatus];
  80. break;
  81. case RNPTypeNotification:
  82. status = [RNPNotification getStatus];
  83. break;
  84. case RNPTypeBackgroundRefresh:
  85. status = [RNPBackgroundRefresh getStatus];
  86. break;
  87. default:
  88. break;
  89. }
  90. resolve(status);
  91. }
  92. RCT_REMAP_METHOD(requestLocation, requestLocation:(NSString *)type resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  93. {
  94. if (self.locationMgr == nil) {
  95. self.locationMgr = [[RNPLocation alloc] init];
  96. }
  97. [self.locationMgr request:type completionHandler:resolve];
  98. }
  99. RCT_REMAP_METHOD(requestNotification, requestNotification:(NSArray *)typeStrings resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  100. {
  101. UIUserNotificationType types;
  102. if ([typeStrings containsObject:@"alert"])
  103. types = types | UIUserNotificationTypeAlert;
  104. if ([typeStrings containsObject:@"badge"])
  105. types = types | UIUserNotificationTypeBadge;
  106. if ([typeStrings containsObject:@"sound"])
  107. types = types | UIUserNotificationTypeSound;
  108. if (self.notificationMgr == nil) {
  109. self.notificationMgr = [[RNPNotification alloc] init];
  110. }
  111. [self.notificationMgr request:types completionHandler:resolve];
  112. }
  113. RCT_REMAP_METHOD(requestBluetooth, requestBluetooth:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  114. {
  115. if (self.bluetoothMgr == nil) {
  116. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  117. }
  118. [self.bluetoothMgr request:resolve];
  119. }
  120. RCT_REMAP_METHOD(requestCamera, requestCamera:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  121. {
  122. [RNPAudioVideo request:@"video" completionHandler:resolve];
  123. }
  124. RCT_REMAP_METHOD(requestMicrophone, requestMicrophone:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  125. {
  126. [RNPAudioVideo request:@"audio" completionHandler:resolve];
  127. }
  128. RCT_REMAP_METHOD(requestEvent, requestEvents:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  129. {
  130. [RNPEvent request:@"event" completionHandler:resolve];
  131. }
  132. RCT_REMAP_METHOD(requestReminder, requestReminders:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  133. {
  134. [RNPEvent request:@"reminder" completionHandler:resolve];
  135. }
  136. RCT_REMAP_METHOD(requestPhoto, requestPhoto:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  137. {
  138. [RNPPhoto request:resolve];
  139. }
  140. RCT_REMAP_METHOD(requestContacts, requestContacts:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  141. {
  142. [RNPContacts request:resolve];
  143. }
  144. @end