Advertisement
Guest User

HALAddressBook.m

a guest
Jul 8th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "HALAddressBook.h"
  2.  
  3. @interface HALAddressBook ()
  4.  
  5. // Redeclare the readonly properties
  6. @property ABAddressBookRef m_addressbook;
  7. @property (readwrite) ABMultiValueRef contactPhoneNumber;
  8.  
  9. @end
  10.  
  11. @implementation HALAddressBook
  12.  
  13. #pragma mark - Request Access
  14.  
  15. - (BOOL)requestAccess
  16. {
  17.     //Asks for access to Address Book.
  18.     self.m_addressbook =  ABAddressBookCreateWithOptions(NULL, NULL);
  19.    
  20.     ABAddressBookCopyArrayOfAllPeople(self.m_addressbook);
  21.    
  22.     __block BOOL accessGranted = NO;
  23.     if (ABAddressBookRequestAccessWithCompletion != NULL) {
  24.         dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  25.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  26.             @autoreleasepool {
  27.                 // Write your code here...
  28.                 // Fetch data from SQLite DB
  29.             }
  30.         });
  31.        
  32.         ABAddressBookRequestAccessWithCompletion(self.m_addressbook, ^(bool granted, CFErrorRef error)
  33.            
  34.                                                  {
  35.                                                      accessGranted = granted;
  36.                                                      
  37.                                                      dispatch_semaphore_signal(sema);
  38.                                                  });
  39.        
  40.         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  41.     }
  42.    
  43.     if (accessGranted) {
  44.         // Access has been granted
  45.         return YES;
  46.     }
  47.     // Access has not been granted
  48.     return NO;
  49. }
  50.  
  51. #pragma mark Accessor Methods
  52.  
  53. - (NSArray *)allContacts
  54. {
  55.     if (_allContacts.count == 0) {
  56.        
  57.         _allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(self.m_addressbook);
  58.     }
  59.    
  60.   return _allContacts;
  61. }
  62.  
  63. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement