Skip to content

Instantly share code, notes, and snippets.

@laiso
Created July 26, 2010 05:37
Show Gist options
  • Save laiso/490221 to your computer and use it in GitHub Desktop.
Save laiso/490221 to your computer and use it in GitHub Desktop.
/* @see http://developer.apple.com/iphone/library/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/500-DirectInteraction/DirectInteraction.html#//apple_ref/doc/uid/TP40007744-CH6-SW1 */
ABAddressBookRef addressBook = ABAddressBookCreate();
NSLog(@"アドレスブックに何人登録されているか: %d",
ABAddressBookGetPersonCount(addressBook));
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
kCFAllocatorDefault,
CFArrayGetCount(people),
people
);
CFArraySortValues(
peopleMutable,
CFRangeMake(0, CFArrayGetCount(peopleMutable)),
(CFComparatorFunction)ABPersonComparePeopleByName,
(void *)ABPersonGetSortOrdering()
);
//
CFArrayRef match = ABAddressBookCopyPeopleWithName(addressBook, CFSTR("中曽根"));
NSLog(@"前方一致でマッチしたデータ件数: %d", CFArrayGetCount(match));
//
ABRecordRef person;
CFDataRef image;
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
person = CFArrayGetValueAtIndex(people, i);
NSLog(@"pearson[%d] :%@", i, person);
if(ABPersonHasImageData(person)){
image = ABPersonCopyImageData(person);
NSLog(@"people[%d]のPersonの写真データの長さ :%d bytes", CFDataGetLength(image));
}
CFArrayRef linked = ABPersonCopyArrayOfAllLinkedPeople(person);
NSLog(@"people[%d]にリンクされているperson :%d", i, CFArrayGetCount(linked));
//
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
ABMultiValueRef firstname = ABRecordCopyValue(person, kABPersonFirstNameProperty);
ABMultiValueRef lastname = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSLog(@"people[%d] %@ %@ : %@ ",
i,
firstname,
lastname,
ABMultiValueCopyValueAtIndex(emails, 0));
}
CFRelease(person);
CFRelease(image);
CFRelease(addressBook);
CFRelease(people);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment