我需要创建一个UITableView
来获取其中的地址簿内容,还需要UISearchBar
来搜索地址簿内容。
我需要像这个项目一样的实现
http://www.appcoda.com/search-bar-tutorial-ios7/
但问题是这里的数据是静态的,我想在其中加载地址簿的数据。
答案 0 :(得分:0)
首先确保您已导入正确的框架:
#import <AddressBook/AddressBook.h>
像这样获取AddressBook的联系人:
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
//further code
}
然后照常填充UITableView(根据您提供的示例)。
谢天谢地,这个框架已被弃用。 iOS 9中新的Contacts API很华丽。
答案 1 :(得分:0)
我创建了ContactData NSObject类。在以下方法中,我为每个联系人创建其对象。最后,您将获得名为contactArr的NSMutableArray。
- (无效)loadContacts {
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//NSLog(@"%@", addressBook);
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++) {
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
ContactData *dataObj = [[ContactData alloc]init];
NSString *Name = @" ";
if ([firstName length]>0) {
if ([lastName length]>0) {
Name = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
}
else{
Name = firstName;
}
}
else{
Name = @" ";
}
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
[[UIDevice currentDevice] name];
//NSLog(@"\n%@\n", [[UIDevice currentDevice] name]);
NSString *phoneNumber = @" ";
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
}
ABMultiValueRef Emails = ABRecordCopyValue(person, kABPersonEmailProperty);
[[UIDevice currentDevice] name];
NSString *Email; //= @" ";
for (CFIndex i = 0; i < ABMultiValueGetCount(Emails); i++) {
Email = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Emails, i);
}
ABMultiValueRef Addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
NSString *address = @" ";
NSMutableDictionary* addressDict = [[NSMutableDictionary alloc]init];
for (CFIndex i = 0; i < ABMultiValueGetCount(Addresses); i++) {
addressDict = (__bridge_transfer NSMutableDictionary *) ABMultiValueCopyValueAtIndex(Addresses, i);
//NSLog(@"address = %@", addressDict);
NSString * street = [addressDict objectForKey:@"Street"];
NSString * city = [addressDict objectForKey:@"City"];
NSString * state = [addressDict objectForKey:@"State"];
NSString *country = [addressDict objectForKey:@"Country"];
if (country.length>0 || state.length>0 || city.length>0 || street.length>0) {
if (country.length>0 && state.length>0) {
address = [NSString stringWithFormat:@"%@, %@", state,country];
}
else if(country.length>0 && city.length>0){
address = [NSString stringWithFormat:@"%@, %@", city,country];
}
else if (state.length>0 && street.length>0){
address = [NSString stringWithFormat:@"%@, %@", street,country];
}
else if (state.length>0 && city.length>0){
address = [NSString stringWithFormat:@"%@, %@", city,state];
}
else if (state.length>0 && street.length>0){
address = [NSString stringWithFormat:@"%@, %@", street,state];
}
else if (city.length>0 && street.length>0){
address = [NSString stringWithFormat:@"%@, %@", street,city];
}
else if (country.length>0){
address = country;
}
else if (state.length>0){
address = state;
}
else if (city.length>0){
address = city;
}
}
else{
address = @" ";
}
}
//NSLog(@"address = %@", address);
NSData *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
dataObj.name = Name;
dataObj.email = Email;
dataObj.phone = phoneNumber;
dataObj.imageData = imgData;
dataObj.address = address;
[contactArr addObject:dataObj];
}
NSLog(@"contacts loaded");
//NSLog(@"count = %lu", (unsigned long)contactArr.count);
}
else {
//Go to settings and allow access to use contacts[GlobalFunction removeWaitView];
}
}