带有 NSObjectClass 的 UITableView 中的 UIAddressBook
UIAddressBook in UITableView with NSObjectClass
我需要制作一个 UITableView
来获取其中的地址簿内容以及一个 UISearchBar
来搜索地址簿内容。
我需要像这个项目一样的实现
http://www.appcoda.com/search-bar-tutorial-ios7/
但问题是这里的数据是静态的,我想在其中加载地址簿的数据。
首先确保您导入了正确的框架:
#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 中的新联系人 API 非常漂亮。
我已经创建了 ContactData NSObject Class。在下面的方法中,我正在为每个联系人创建它的对象。最后,你会得到名为 contactArr.
的 NSMutableArray
-(void)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];
}
}
我需要制作一个 UITableView
来获取其中的地址簿内容以及一个 UISearchBar
来搜索地址簿内容。
我需要像这个项目一样的实现
http://www.appcoda.com/search-bar-tutorial-ios7/
但问题是这里的数据是静态的,我想在其中加载地址簿的数据。
首先确保您导入了正确的框架:
#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 中的新联系人 API 非常漂亮。
我已经创建了 ContactData NSObject Class。在下面的方法中,我正在为每个联系人创建它的对象。最后,你会得到名为 contactArr.
的 NSMutableArray-(void)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];
}
}