Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create an instance of HALAddressBook
- HALAddressBook *addressBook = [[HALAddressBook alloc]init];
- // Ask for access to the user's address book
- BOOL result = [addressBook isAccessGranted];
- if (!result) {
- NSLog(@"Access was denied.");
- }
- // Access has been granted
- // Create array of all address book contacts
- NSArray *contacts = [addressBook contacts];
- // Initialize our friends array
- self.friends = [[NSMutableArray alloc]init];
- // Loop through all of the contacts.
- // Create an instance of HALContact
- // Set the instance's contactRef, firstName and phoneNumbers properties
- for (id currentContact in contacts) {
- // Create an instance of HALContact,
- // set its contactRef property,
- // set its firstName property,
- HALContact *contact = [[HALContact alloc]init];
- contact.contactRef = (__bridge ABRecordRef)(currentContact);
- contact.firstName = (__bridge_transfer NSString
- *)ABRecordCopyValue(contact.contactRef, kABPersonFirstNameProperty);
- // If firstName is nil, continue
- if (!contact.firstName) continue;
- // Grab all of the phone number values for this contact
- NSArray *phoneNumberValues = (__bridge NSArray *)(ABRecordCopyValue(contact.contactRef, kABPersonPhoneProperty));
- // Place a copy of all phone numbers into contact's
- // phoneNumber property
- contact.phoneNumbers = (__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues((__bridge ABMultiValueRef)(phoneNumberValues)));
- // Check if the contact has multiple phone numbers
- BOOL result = [contact hasMultiplePhoneNumbers];
- // If contact only has one phone number, then add the
- // number from the phoneNumbers property into the
- // contact's mainPhoneNumber property
- if (!result) {
- contact.mainPhoneNumber = contact.phoneNumbers[0];
- // Add contact to friends array
- [self.friends addObject:contact];
- continue;
- }
- // Loop through the contacts phone numbers
- // and for every phone number, create a copy of the contact
- // and place the current phone number at index in
- // the copy's mainPhoneNumber property.
- //So if "Steve" has 2 phone numbers, he'll be added to our
- // friends array twice, once for each phone number.
- for (int index = 0; index < contact.phoneNumbers.count; index++) {
- HALContact *copyOfContact = [[HALContact alloc]init];
- copyOfContact.mainPhoneNumber = contact.phoneNumbers[index];
- copyOfContact.firstName = contact.firstName;
- // Add copyOfContact to the friends array
- [self.friends addObject:copyOfContact];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement