ReactNativePermissions.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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(requestPermission, permissionType:(RNPType)type json:(id)json resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  93. {
  94. NSString *status;
  95. switch (type) {
  96. case RNPTypeLocation:
  97. return [self requestLocation:json resolve:resolve];
  98. case RNPTypeCamera:
  99. return [RNPAudioVideo request:@"video" completionHandler:resolve];
  100. case RNPTypeMicrophone:
  101. return [RNPAudioVideo request:@"audio" completionHandler:resolve];
  102. case RNPTypePhoto:
  103. return [RNPPhoto request:resolve];
  104. case RNPTypeContacts:
  105. return [RNPContacts request:resolve];
  106. case RNPTypeEvent:
  107. return [RNPEvent request:@"event" completionHandler:resolve];
  108. case RNPTypeReminder:
  109. return [RNPEvent request:@"reminder" completionHandler:resolve];
  110. case RNPTypeBluetooth:
  111. return [self requestBluetooth:resolve];
  112. case RNPTypeNotification:
  113. return [self requestNotification:json resolve:resolve];
  114. default:
  115. break;
  116. }
  117. }
  118. - (void) requestLocation:(id)json resolve:(RCTPromiseResolveBlock)resolve
  119. {
  120. if (self.locationMgr == nil) {
  121. self.locationMgr = [[RNPLocation alloc] init];
  122. }
  123. NSString *type = [RCTConvert NSString:json];
  124. [self.locationMgr request:type completionHandler:resolve];
  125. }
  126. - (void) requestNotification:(id)json resolve:(RCTPromiseResolveBlock)resolve
  127. {
  128. NSArray *typeStrings = [RCTConvert NSArray:json];
  129. UIUserNotificationType types;
  130. if ([typeStrings containsObject:@"alert"])
  131. types = types | UIUserNotificationTypeAlert;
  132. if ([typeStrings containsObject:@"badge"])
  133. types = types | UIUserNotificationTypeBadge;
  134. if ([typeStrings containsObject:@"sound"])
  135. types = types | UIUserNotificationTypeSound;
  136. if (self.notificationMgr == nil) {
  137. self.notificationMgr = [[RNPNotification alloc] init];
  138. }
  139. [self.notificationMgr request:types completionHandler:resolve];
  140. }
  141. - (void) requestBluetooth:(RCTPromiseResolveBlock)resolve
  142. {
  143. if (self.bluetoothMgr == nil) {
  144. self.bluetoothMgr = [[RNPBluetooth alloc] init];
  145. }
  146. [self.bluetoothMgr request:resolve];
  147. }
  148. @end