SHOW:
|
|
- or go back to the newest paste.
1 | // Create an instance of HALAddressBook | |
2 | HALAddressBook *addressBook = [[HALAddressBook alloc]init]; | |
3 | ||
4 | // Ask for access to the user's address book | |
5 | BOOL result = [addressBook isAccessGranted]; | |
6 | ||
7 | if (!result) { | |
8 | NSLog(@"Access was denied."); | |
9 | } | |
10 | ||
11 | // Access has been granted | |
12 | // Create array of all address book contacts | |
13 | NSArray *contacts = [addressBook contacts]; | |
14 | ||
15 | ||
16 | // Initialize our friends array | |
17 | self.friends = [[NSMutableArray alloc]init]; | |
18 | ||
19 | ||
20 | // Loop through all of the contacts. | |
21 | // Create an instance of HALContact | |
22 | // Set the instance's contactRef, firstName and phoneNumbers properties | |
23 | ||
24 | ||
25 | for (id currentContact in contacts) { | |
26 | // Create an instance of HALContact, | |
27 | // set its contactRef property, | |
28 | // set its firstName property, | |
29 | HALContact *contact = [[HALContact alloc]init]; | |
30 | contact.contactRef = (__bridge ABRecordRef)(currentContact); | |
31 | contact.firstName = (__bridge_transfer NSString | |
32 | *)ABRecordCopyValue(contact.contactRef, kABPersonFirstNameProperty); | |
33 | ||
34 | // If firstName is nil, continue | |
35 | if (!contact.firstName) continue; | |
36 | ||
37 | // Grab all of the phone number values for this contact | |
38 | NSArray *phoneNumberValues = (__bridge NSArray *)(ABRecordCopyValue(contact.contactRef, kABPersonPhoneProperty)); | |
39 | ||
40 | // Place a copy of all phone numbers into contact's | |
41 | // phoneNumber property | |
42 | contact.phoneNumbers = (__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues((__bridge ABMultiValueRef)(phoneNumberValues))); | |
43 | ||
44 | ||
45 | // Check if the contact has multiple phone numbers | |
46 | BOOL result = [contact hasMultiplePhoneNumbers]; | |
47 | ||
48 | // If contact only has one phone number, then add the | |
49 | // number from the phoneNumbers property into the | |
50 | // contact's mainPhoneNumber property | |
51 | if (!result) { | |
52 | contact.mainPhoneNumber = contact.phoneNumbers[0]; | |
53 | // Add contact to friends array | |
54 | [self.friends addObject:contact]; | |
55 | continue; | |
56 | } | |
57 | // Loop through the contacts phone numbers | |
58 | // and for every phone number, create a copy of the contact | |
59 | // and place the current phone number at index in | |
60 | // the copy's mainPhoneNumber property. | |
61 | ||
62 | //So if "Steve" has 2 phone numbers, he'll be added to our | |
63 | // friends array twice, once for each phone number. | |
64 | ||
65 | for (int index = 0; index < contact.phoneNumbers.count; index++) { | |
66 | HALContact *copyOfContact = [[HALContact alloc]init]; | |
67 | - | // Add both contacts to the friends array |
67 | + | |
68 | copyOfContact.firstName = contact.firstName; | |
69 | ||
70 | // Add copyOfContact to the friends array | |
71 | [self.friends addObject:copyOfContact]; | |
72 | } | |
73 | ||
74 | } |