ReactNativePermissions.m 5.1KB

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