Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Asks for access to Address Book.
- ABAddressBookRef m_addressbook = ABAddressBookCreateWithOptions(NULL, NULL);
- ABAddressBookCopyArrayOfAllPeople(m_addressbook);
- NSLog(@"%@", m_addressbook);
- __block BOOL accessGranted = NO;
- if (ABAddressBookRequestAccessWithCompletion != NULL) {
- dispatch_semaphore_t sema = dispatch_semaphore_create(0);
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- @autoreleasepool {
- // Write your code here...
- // Fetch data from SQLite DB
- }
- });
- ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error)
- {
- accessGranted = granted;
- dispatch_semaphore_signal(sema);
- });
- dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
- }
- if (accessGranted == 0) {
- [self performSegueWithIdentifier:@"addFriendsToMediaCaptureSegue" sender:self];
- }
- else if (accessGranted == 1) {
- NSArray *allContacts = (__bridge_transfer NSArray
- *)ABAddressBookCopyArrayOfAllPeople(m_addressbook);
- //Loop through all contacts.
- int z = allContacts.count;
- for (int i = 0; i < z; i++) {
- //Place current contact in ABRecordRef
- ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
- //Create string from current contact's first name property.
- NSString *firstName = (__bridge_transfer NSString
- *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
- //Create phone number from contact's phone number property.
- ABMultiValueRef *phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
- NSMutableArray *numbers = [[NSMutableArray alloc] init];
- NSString *number = [[NSString alloc] init];
- //Loop through all of contact's phone numbers.
- if (phoneNumber) {
- CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumber);
- for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
- number = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, i);
- CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumber, i);
- if (label) {
- //Add current number to array.
- if(firstName.length > 0) {
- [numbers addObject:number];
- }
- }
- CFRelease(label);
- }
- CFRelease(phoneNumber);
- }
- int numbersIndex;
- //Loop through all of the contact's numbers.
- for(numbersIndex = 0; numbersIndex < numbers.count; numbersIndex++) {
- //Set string global to current number.
- self.numberValues = numbers[numbersIndex];
- //Add current first name to both arrays. We want them to match for later.
- [self.potentiaFriendsNotInParseFirstNamesArray addObject:firstName];
- //Add current phone number to array.
- [self.potentiaFriendsPhoneNumberArray addObject:self.numberValues];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement