ReactNativePermissions.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. - (NSDictionary *)constantsToExport
  37. {
  38. return @{ @"PermissionTypes" : @[ @"location",
  39. @"camera",
  40. @"microphone",
  41. @"photo",
  42. @"contacts",
  43. @"event",
  44. @"reminder",
  45. @"bluetooth",
  46. @"notification",
  47. @"backgroundRefresh" ]
  48. };
  49. };
  50. RCT_REMAP_METHOD(canOpenSettings, canOpenSettings:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  51. {
  52. resolve(@(UIApplicationOpenSettingsURLString != nil));
  53. }
  54. RCT_EXPORT_METHOD(openSettings)
  55. {
  56. if (@(UIApplicationOpenSettingsURLString != nil)) {
  57. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  58. [[UIApplication sharedApplication] openURL:url];
  59. }
  60. }
  61. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  62. {
  63. NSString *status;
  64. switch (type) {
  65. case RNPTypeLocation:
  66. status = [RNPLocation getStatus];
  67. break;
  68. case RNPTypeCamera:
  69. status = [RNPAudioVideo getStatus:@"video"];
  70. break;
  71. case RNPTypeMicrophone:
  72. status = [RNPAudioVideo getStatus:@"audio"];
  73. break;
  74. case RNPTypePhoto:
  75. status = [RNPPhoto getStatus];
  76. break;
  77. case RNPTypeContacts:
  78. status = [RNPContacts getStatus];
  79. break;
  80. case RNPTypeEvent:
  81. status = [RNPEvent getStatus:@"event"];
  82. break;
  83. case RNPTypeReminder:
  84. status = [RNPEvent getStatus:@"reminder"];
  85. break;
  86. case RNPTypeBluetooth:
  87. status = [RNPBluetooth getStatus];
  88. break;
  89. case RNPTypeNotification:
  90. status = [RNPNotification getStatus];
  91. break;
  92. case RNPTypeBackgroundRefresh:
  93. status = [RNPBackgroundRefresh getStatus];
  94. break;
  95. default:
  96. break;
  97. }
  98. resolve(status);
  99. }
  100. RCT_REMAP_METHOD(requestLocation, requestLocation:(NSString *)type resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  101. {
  102. if (self.locationMgr == nil) {
  103. self.locationMgr = [[RNPLocation alloc] init];
  104. }
  105. [self.locationMgr request:type completionHandler:resolve];
  106. }
  107. RCT_REMAP_METHOD(requestNotification, requestNotification:(NSArray *)typeStrings resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  108. {
  109. UIUserNotificationType types;
  110. if ([typeStrings containsObject:@"alert"])
  111. types = types | UIUserNotificationTypeAlert;
  112. if ([typeStrings containsObject:@"badge"])
  113. types = types | UIUserNotificationTypeBadge;
  114. if ([typeStrings containsObject:@"sound"])
  115. types = types | UIUserNotificationTypeSound;
  116. if (self.notificationMgr == nil) {
  117. self.notificationMgr = [[RNPNotification alloc] init];
  118. }
  119. [self.notificationMgr request:types completionHandler:resolve];
  120. }
  121. RCT_REMAP_METHOD(requestBluetooth, requestBluetooth:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  122. {
  123. if (self.bluetoothMgr == nil) {
  124. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  125. }
  126. [self.bluetoothMgr request:resolve];
  127. }
  128. RCT_REMAP_METHOD(requestCamera, requestCamera:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  129. {
  130. [RNPAudioVideo request:@"video" completionHandler:resolve];
  131. }
  132. RCT_REMAP_METHOD(requestMicrophone, requestMicrophone:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  133. {
  134. [RNPAudioVideo request:@"audio" completionHandler:resolve];
  135. }
  136. RCT_REMAP_METHOD(requestEvent, requestEvents:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  137. {
  138. [RNPEvent request:@"event" completionHandler:resolve];
  139. }
  140. RCT_REMAP_METHOD(requestReminder, requestReminders:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  141. {
  142. [RNPEvent request:@"reminder" completionHandler:resolve];
  143. }
  144. RCT_REMAP_METHOD(requestPhoto, requestPhoto:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  145. {
  146. [RNPPhoto request:resolve];
  147. }
  148. RCT_REMAP_METHOD(requestContacts, requestContacts:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  149. {
  150. [RNPContacts request:resolve];
  151. }
  152. @end