Advertisement
Guest User

Old View Controller Snippet

a guest
Jul 8th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Asks for access to Address Book.
  2.     ABAddressBookRef m_addressbook =  ABAddressBookCreateWithOptions(NULL, NULL);
  3.    
  4.     ABAddressBookCopyArrayOfAllPeople(m_addressbook);
  5.    
  6.     NSLog(@"%@", m_addressbook);
  7.    
  8.     __block BOOL accessGranted = NO;
  9.     if (ABAddressBookRequestAccessWithCompletion != NULL) {
  10.         dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  11.         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  12.             @autoreleasepool {
  13.                 // Write your code here...
  14.                 // Fetch data from SQLite DB
  15.             }
  16.         });
  17.        
  18.         ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error)
  19.                                                  
  20.                                                  {
  21.                                                      accessGranted = granted;
  22.                                                      
  23.                                                      dispatch_semaphore_signal(sema);
  24.                                                  });
  25.        
  26.         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  27.     }
  28.    
  29.     if (accessGranted == 0) {
  30.        
  31.        
  32.         [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self];
  33.     }
  34.     else if (accessGranted == 1) {
  35.        
  36.        
  37.         NSArray *allContacts = (__bridge_transfer NSArray
  38.                                 *)ABAddressBookCopyArrayOfAllPeople(m_addressbook);
  39.         //Loop through all contacts.
  40.        
  41.         int z = allContacts.count;
  42.        
  43.         for (int i = 0; i < z; i++) {
  44.             //Place current contact in ABRecordRef
  45.            
  46.             ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
  47.            
  48.             //Create string from current contact's first name property.
  49.            
  50.             NSString *firstName = (__bridge_transfer NSString
  51.                                    *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
  52.            
  53.             //Create phone number from contact's phone number property.
  54.            
  55.             ABMultiValueRef *phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
  56.             NSMutableArray *numbers = [[NSMutableArray alloc] init];
  57.            
  58.             NSString *number = [[NSString alloc] init];
  59.            
  60.             //Loop through all of contact's phone numbers.
  61.             if (phoneNumber) {
  62.                 CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumber);
  63.                
  64.                 for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
  65.                     number = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, i);
  66.                     CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumber, i);
  67.                     if (label) {
  68.                        
  69.                         //Add current number to array.
  70.                         if(firstName.length > 0) {
  71.                            
  72.                             [numbers addObject:number];
  73.                            
  74.                         }
  75.                     }
  76.                     CFRelease(label);
  77.                 }
  78.                 CFRelease(phoneNumber);
  79.             }
  80.            
  81.             int numbersIndex;
  82.            
  83.             //Loop through all of the contact's numbers.
  84.            
  85.             for(numbersIndex = 0; numbersIndex < numbers.count; numbersIndex++) {
  86.                
  87.                 //Set string global to current number.
  88.                
  89.                 self.numberValues = numbers[numbersIndex];
  90.                
  91.                
  92.                 //Add current first name to both arrays. We want them to match for later.
  93.                
  94.                 [self.potentiaFriendsNotInParseFirstNamesArray addObject:firstName];
  95.                
  96.                 //Add current phone number to array.
  97.                
  98.                 [self.potentiaFriendsPhoneNumberArray addObject:self.numberValues];
  99.                
  100.             }
  101.         }
  102.        
  103.            }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement