ReactNativePermissions.m 7.9KB

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