ReactNativePermissions.m 6.0KB

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