ReactNativePermissions.m 5.2KB

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