123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #import "RNPermissionHandlerContacts.h"
  2. @import Contacts;
  3. @implementation RNPermissionHandlerContacts
  4. + (NSArray<NSString *> * _Nonnull)usageDescriptionKeys {
  5. return @[@"NSContactsUsageDescription"];
  6. }
  7. + (NSString * _Nonnull)handlerUniqueId {
  8. return @"ios.permission.CONTACTS";
  9. }
  10. - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  11. rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
  12. switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
  13. case CNAuthorizationStatusNotDetermined:
  14. return resolve(RNPermissionStatusNotDetermined);
  15. case CNAuthorizationStatusRestricted:
  16. return resolve(RNPermissionStatusRestricted);
  17. case CNAuthorizationStatusDenied:
  18. return resolve(RNPermissionStatusDenied);
  19. case CNAuthorizationStatusAuthorized:
  20. return resolve(RNPermissionStatusAuthorized);
  21. }
  22. }
  23. - (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
  24. rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
  25. [[CNContactStore new] requestAccessForEntityType:CNEntityTypeContacts
  26. completionHandler:^(__unused BOOL granted, NSError * _Nullable error) {
  27. if (error != nil && error.code != 100) { // error code 100 is permission denied
  28. reject(error);
  29. } else {
  30. [self checkWithResolver:resolve rejecter:reject];
  31. }
  32. }];
  33. }
  34. @end