ReactNativePermissions.m 5.3KB

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