CoreFoundation
平行于Foundation,基础类都有对应的类,Toll-Free Bridging = 基础数据类型和NS类的转换是没有损耗的
1 2 3 4 5 6 7
| CFArrayRef array = ABAddressBookCopyPeopleWithName (... );
NSLog (@“ %d”, [(NSArray *)array count ]);
NSMutableArray *mutableArray = [(NSArray *)array mutableCopy ];
[mutableArray release ];
if (array ) {
CFRelease (array );
} |
CoreFoundation使用NULL而不是nil
1 2 3 4 5
| CFStringRef string = CreateSomeCFString...;
if (string != NULL) {
DoSomethingWith(string);
CFRelease(string);
} |
1 2 3
| NSString *string = (NSString *)CreateSomeCFString...;
NSLog (@“ %@”, [string lowercaseString ]);
[string autorelease ]; // Even use autorelease! |
ABAddressBookRef获得联系人名单
1 2
| ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyPeopleWithName(ab, name); |
ABRecordRef
包括属性
■ First and last name
■ Image
■ Phone numbers, emails, etc…
单一属性,包括:
First Name, last name, birthday, etc…
get使用ABRecordCopyValue(…)方法
1 2
| CFStringRef first =
ABRecordCopyValue(person, kABPersonFirstNameProperty); |
set使用ABRecordSetValue(…)方法
1 2
| CFDateRef date = CFDateCreate(…);
ABRecordSetValue(person, kABPersonBirthdayProperty, date, &error); |
多重属性(ABMultiValueRef),包括:
Phones, emails, URLs, etc…
类型用ABMultiValueRef表示,label/value
获得count
1
| CFIndex count = ABMultiValueGetCount(multiValue); |
获得value
1
| CFTypeRef value = ABMultiValueCopyValueAtIndex(mv, index); |
获得label
1
| CFStringRef label = ABMultiValueCopyLabelAtIndex(mv, index); |
获得identifier
1
| CFIndex identifier = ABMultiValueGetIdentifierAtIndex(mv, index); |
更新流程:
• 获取多重属性控制
• 增加值
• 将值赋予联系人
• 保存通讯录
1 2 3 4 5
| ABMultiValueRef urls = ABRecordCopyValue(person, kABPersonURLProperty);
ABMutableMultiValueRef urlCopy = ABMultiValueCreateMutableCopy(urls);
ABMultiValueAddValueAndLabel(urlCopy, "the url", "social", NULL);
ABRecordSetValue(person, urlCopy, kABPersonURLProperty);
ABAddressBookSave(ab, &err); |
排序
可使用内置的ABPersonGetSortOrdering,ABPersonComparePeopleByName,作为排序方法
1 2 3 4 5
| CFMutableArrayRef people = // obtain an array of people
CFRange fullRange = CFRangeMake(0, CFArrayGetCount(people));
ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();
CFArraySortValues(people, fullRange, ABPersonComparePeopleByName,
(void*)sortOrdering); |
或者使用objc的风格(下例中people是NSMutableArray)
1
| [people sortUsingFunction:ABPersonComparePeopleByName context:(void*)sortOrdering]; |
获得联系人名字
1 2 3
| ABRecordRef person = // get a person
CFStringRef name = ABRecordCopyCompositeName(person);
// do something clever with that person’s name |
或
1 2 3
| ABRecordRef person = // get a person
NSString *name = (NSString*)ABRecordCopyCompositeName (person );
// do something clever with that person’s name |
ABPersonViewController属性
■ displayedPerson
■ displayedProperties
■ allowsEditing
ABUnknownPersonViewController属性
■ displayedPerson
■ allowsAddingToAddressBook
■ delegate
ABPeoplePickerNavigationController
1 2 3 4 5 6
| ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc ] init ];
ppnc.peoplePickerDelegate = self;
ppnc.displayedProperties = [NSArray arrayWithObject :[NSNumber numberWithInt :kABPersonEmailProperty ]];
ppnc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController :ppnc animated :YES];
[ppnc release ]; |
1 2 3 4 5 6 7 8
| // cancel选择
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
// 当选择了通讯录的的某个联络人
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
// 当选择了通讯录的某个联络人的某个属性
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier |