ReactNativePermissions.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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)
  60. {
  61. if (@(UIApplicationOpenSettingsURLString != nil)) {
  62. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  63. [[UIApplication sharedApplication] openURL:url];
  64. }
  65. }
  66. RCT_REMAP_METHOD(getPermissionStatus, getPermissionStatus:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  67. {
  68. NSString *status;
  69. switch (type) {
  70. case RNPTypeLocation: {
  71. NSString *locationPermissionType = [RCTConvert NSString:json];
  72. status = [RNPLocation getStatusForType:locationPermissionType];
  73. break;
  74. }
  75. case RNPTypeCamera:
  76. status = [RNPAudioVideo getStatus:@"video"];
  77. break;
  78. case RNPTypeMicrophone:
  79. status = [RNPAudioVideo getStatus:@"audio"];
  80. break;
  81. case RNPTypePhoto:
  82. status = [RNPPhoto getStatus];
  83. break;
  84. case RNPTypeContacts:
  85. status = [RNPContacts getStatus];
  86. break;
  87. case RNPTypeEvent:
  88. status = [RNPEvent getStatus:@"event"];
  89. break;
  90. case RNPTypeReminder:
  91. status = [RNPEvent getStatus:@"reminder"];
  92. break;
  93. case RNPTypeBluetooth:
  94. status = [RNPBluetooth getStatus];
  95. break;
  96. case RNPTypeNotification:
  97. status = [RNPNotification getStatus];
  98. break;
  99. case RNPTypeBackgroundRefresh:
  100. status = [RNPBackgroundRefresh getStatus];
  101. break;
  102. case RNPTypeSpeechRecognition:
  103. status = [RNPSpeechRecognition getStatus];
  104. break;
  105. default:
  106. break;
  107. }
  108. resolve(status);
  109. }
  110. RCT_REMAP_METHOD(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  111. {
  112. NSString *status;
  113. switch (type) {
  114. case RNPTypeLocation:
  115. return [self requestLocation:json resolve:resolve];
  116. case RNPTypeCamera:
  117. return [RNPAudioVideo request:@"video" completionHandler:resolve];
  118. case RNPTypeMicrophone:
  119. return [RNPAudioVideo request:@"audio" completionHandler:resolve];
  120. case RNPTypePhoto:
  121. return [RNPPhoto request:resolve];
  122. case RNPTypeContacts:
  123. return [RNPContacts request:resolve];
  124. case RNPTypeEvent:
  125. return [RNPEvent request:@"event" completionHandler:resolve];
  126. case RNPTypeReminder:
  127. return [RNPEvent request:@"reminder" completionHandler:resolve];
  128. case RNPTypeBluetooth:
  129. return [self requestBluetooth:resolve];
  130. case RNPTypeNotification:
  131. return [self requestNotification:json resolve:resolve];
  132. case RNPTypeSpeechRecognition:
  133. return [RNPSpeechRecognition request:resolve];
  134. default:
  135. break;
  136. }
  137. }
  138. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  139. {
  140. if (self.locationMgr == nil) {
  141. self.locationMgr = [[RNPLocation alloc] init];
  142. }
  143. NSString *type = [RCTConvert NSString:json];
  144. [self.locationMgr request:type completionHandler:resolve];
  145. }
  146. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  147. {
  148. NSArray *typeStrings = [RCTConvert NSArray:json];
  149. UIUserNotificationType types;
  150. if ([typeStrings containsObject:@"alert"])
  151. types = types | UIUserNotificationTypeAlert;
  152. if ([typeStrings containsObject:@"badge"])
  153. types = types | UIUserNotificationTypeBadge;
  154. if ([typeStrings containsObject:@"sound"])
  155. types = types | UIUserNotificationTypeSound;
  156. if (self.notificationMgr == nil) {
  157. self.notificationMgr = [[RNPNotification alloc] init];
  158. }
  159. [self.notificationMgr request:types completionHandler:resolve];
  160. }
  161. - (void) requestBluetooth:(RCTPromiseResolveBlock)resolve
  162. {
  163. if (self.bluetoothMgr == nil) {
  164. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  165. }
  166. [self.bluetoothMgr request:resolve];
  167. }
  168. @end