ReactNativePermissions.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "ReactNativePermissions.h"
  9. #import "RCTBridge.h"
  10. #import "RCTConvert.h"
  11. #import "RCTEventDispatcher.h"
  12. #import "RCTConvert+RNPermissionsStatus.h"
  13. #import <AddressBook/AddressBook.h>
  14. #import <AssetsLibrary/AssetsLibrary.h>
  15. #import <EventKit/EventKit.h>
  16. #import <CoreLocation/CoreLocation.h>
  17. #import <AVFoundation/AVFoundation.h>
  18. #import <CoreBluetooth/CoreBluetooth.h>
  19. @interface ReactNativePermissions()
  20. @end
  21. @implementation ReactNativePermissions
  22. RCT_EXPORT_MODULE();
  23. @synthesize bridge = _bridge;
  24. #pragma mark Initialization
  25. - (instancetype)init
  26. {
  27. if (self = [super init]) {
  28. }
  29. return self;
  30. }
  31. - (NSDictionary *)constantsToExport
  32. {
  33. return @{ @"StatusUndetermined" : @(RNPermissionsStatusUndetermined),
  34. @"StatusDenied" : @(RNPermissionsStatusDenied),
  35. @"StatusAuthorized" : @(RNPermissionsStatusAuthorized),
  36. @"StatusRestricted" : @(RNPermissionsStatusRestricted)};
  37. };
  38. RCT_REMAP_METHOD(locationPermissionStatus, locationPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  39. {
  40. int status = [CLLocationManager authorizationStatus];
  41. switch (status) {
  42. case kCLAuthorizationStatusAuthorizedAlways:
  43. case kCLAuthorizationStatusAuthorizedWhenInUse:
  44. return resolve(@(RNPermissionsStatusAuthorized));
  45. case kCLAuthorizationStatusDenied:
  46. return resolve(@(RNPermissionsStatusDenied));
  47. case kCLAuthorizationStatusRestricted:
  48. return resolve(@(RNPermissionsStatusRestricted));
  49. default:
  50. return resolve(@(RNPermissionsStatusUndetermined));
  51. }
  52. }
  53. RCT_REMAP_METHOD(cameraPermissionStatus, cameraPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  54. {
  55. int status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  56. switch (status) {
  57. case AVAuthorizationStatusAuthorized:
  58. return resolve(@(RNPermissionsStatusAuthorized));
  59. case AVAuthorizationStatusDenied:
  60. return resolve(@(RNPermissionsStatusDenied));
  61. case AVAuthorizationStatusRestricted:
  62. return resolve(@(RNPermissionsStatusRestricted));
  63. default:
  64. return resolve(@(RNPermissionsStatusUndetermined));
  65. }
  66. }
  67. RCT_REMAP_METHOD(microphonePermissionStatus, microphonePermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  68. {
  69. int status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  70. switch (status) {
  71. case AVAuthorizationStatusAuthorized:
  72. return resolve(@(RNPermissionsStatusAuthorized));
  73. case AVAuthorizationStatusDenied:
  74. return resolve(@(RNPermissionsStatusDenied));
  75. case AVAuthorizationStatusRestricted:
  76. return resolve(@(RNPermissionsStatusRestricted));
  77. default:
  78. return resolve(@(RNPermissionsStatusUndetermined));
  79. }
  80. }
  81. RCT_REMAP_METHOD(photoPermissionStatus, photoPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  82. {
  83. int status = [ALAssetsLibrary authorizationStatus];
  84. switch (status) {
  85. case ALAuthorizationStatusAuthorized:
  86. return resolve(@(RNPermissionsStatusAuthorized));
  87. case ALAuthorizationStatusDenied:
  88. return resolve(@(RNPermissionsStatusDenied));
  89. case ALAuthorizationStatusRestricted:
  90. return resolve(@(RNPermissionsStatusRestricted));
  91. default:
  92. return resolve(@(RNPermissionsStatusUndetermined));
  93. }
  94. }
  95. RCT_REMAP_METHOD(contactsPermissionStatus, contactsPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  96. {
  97. int status = ABAddressBookGetAuthorizationStatus();
  98. switch (status) {
  99. case kABAuthorizationStatusAuthorized:
  100. return resolve(@(RNPermissionsStatusAuthorized));
  101. case kABAuthorizationStatusDenied:
  102. return resolve(@(RNPermissionsStatusDenied));
  103. case kABAuthorizationStatusRestricted:
  104. return resolve(@(RNPermissionsStatusRestricted));
  105. default:
  106. return resolve(@(RNPermissionsStatusUndetermined));
  107. }
  108. }
  109. RCT_REMAP_METHOD(eventPermissionStatus, eventPermission:(NSString *)eventString resolve:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  110. {
  111. int status;
  112. if ([eventString isEqualToString:@"reminder"]) {
  113. status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
  114. } else if ([eventString isEqualToString:@"event"]) {
  115. status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
  116. } else {
  117. NSError *error = [NSError errorWithDomain:@"invalidOption" code:-1 userInfo:NULL];
  118. return reject(@"-1", @"Type must be 'reminder' or 'event'", error);
  119. }
  120. switch (status) {
  121. case EKAuthorizationStatusAuthorized:
  122. return resolve(@(RNPermissionsStatusAuthorized));
  123. case EKAuthorizationStatusDenied:
  124. return resolve(@(RNPermissionsStatusDenied));
  125. case EKAuthorizationStatusRestricted:
  126. return resolve(@(RNPermissionsStatusRestricted));
  127. default:
  128. return resolve(@(RNPermissionsStatusUndetermined));
  129. }
  130. }
  131. RCT_REMAP_METHOD(bluetoothPermissionStatus, bluetoothPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  132. {
  133. int status = [CBPeripheralManager authorizationStatus];
  134. switch (status) {
  135. case CBPeripheralManagerAuthorizationStatusAuthorized:
  136. return resolve(@(RNPermissionsStatusAuthorized));
  137. case CBPeripheralManagerAuthorizationStatusDenied:
  138. return resolve(@(RNPermissionsStatusDenied));
  139. case CBPeripheralManagerAuthorizationStatusRestricted:
  140. return resolve(@(RNPermissionsStatusRestricted));
  141. default:
  142. return resolve(@(RNPermissionsStatusUndetermined));
  143. }
  144. }
  145. //problem here is that we can only return Authorized or Undetermined
  146. RCT_REMAP_METHOD(notificationPermissionStatus, notificationPermission:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  147. {
  148. if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
  149. // iOS8+
  150. if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
  151. return resolve(@(RNPermissionsStatusAuthorized));
  152. }
  153. else {
  154. return resolve(@(RNPermissionsStatusUndetermined));
  155. }
  156. } else {
  157. if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
  158. return resolve(@(RNPermissionsStatusUndetermined));
  159. }
  160. else {
  161. return resolve(@(RNPermissionsStatusAuthorized));
  162. }
  163. }
  164. }
  165. @end