RNPContacts.m 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // RNPContacts.m
  3. // ReactNativePermissions
  4. //
  5. // Created by Yonah Forst on 11/07/16.
  6. // Copyright © 2016 Yonah Forst. All rights reserved.
  7. //
  8. #import "RNPContacts.h"
  9. #import <AddressBook/AddressBook.h>
  10. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
  11. @import Contacts;
  12. #endif
  13. @implementation RNPContacts
  14. + (NSString *)getStatus
  15. {
  16. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
  17. int status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
  18. switch (status) {
  19. case CNAuthorizationStatusAuthorized:
  20. return RNPStatusAuthorized;
  21. case CNAuthorizationStatusDenied:
  22. return RNPStatusDenied;
  23. case CNAuthorizationStatusRestricted:
  24. return RNPStatusRestricted;
  25. default:
  26. return RNPStatusUndetermined;
  27. }
  28. #else
  29. int status = ABAddressBookGetAuthorizationStatus();
  30. switch (status) {
  31. case kABAuthorizationStatusAuthorized:
  32. return RNPStatusAuthorized;
  33. case kABAuthorizationStatusDenied:
  34. return RNPStatusDenied;
  35. case kABAuthorizationStatusRestricted:
  36. return RNPStatusRestricted;
  37. default:
  38. return RNPStatusUndetermined;
  39. }
  40. #endif
  41. }
  42. + (void)request:(void (^)(NSString *))completionHandler
  43. {
  44. void (^handler)(BOOL, NSError * _Nullable) = ^(BOOL granted, NSError * _Nullable error) {
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. completionHandler([self.class getStatus]);
  47. });
  48. };
  49. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
  50. CNContactStore *contactStore = [[CNContactStore alloc] init];
  51. [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:handler];
  52. #else
  53. CFErrorRef error = nil;
  54. ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, &error);
  55. ABAddressBookRequestAccessWithCompletion(addressBook, completionHandler);
  56. #endif
  57. }
  58. @end