First, add addressbook.framework and addressbookui.framework in the project
Second, get the address Book
1. Define an array in Infterface and initialize it in the Init method
?
| 123456 |
NSMutableArray *addressBookTemp;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ addressBookTemp = [NSMutableArray array];} |
2. Define a model to store the various attributes in the Address Book
Create a new class that inherits from NSObject, in. h
?
| 1234567891011121314 |
@interface TKAddressBook : NSObject { NSInteger sectionNumber; NSInteger recordID; NSString *name; NSString *email; NSString *tel;}@property NSInteger sectionNumber;@property NSInteger recordID;@property (nonatomic, retain) NSString *name;@property (nonatomic, retain) NSString *email;@property (nonatomic, retain) NSString *tel;@end |
Do synthesize in. m files?
| 1234 |
@implementation TKAddressBook@synthesize name, email, tel, recordID, sectionNumber;@end |
3. Get Contacts
After iOS6, getting the address book requires permission
?
| 1234567891011121314151617181920212223242526272829 |
//新建一个通讯录类 ABAddressBookRef addressBooks = nil; if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) { addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); //获取通讯录权限 dispatch_semaphore_t sema = dispatch_semaphore_create(0); ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);}); dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch_release(sema); } else { addressBooks = ABAddressBookCreate(); }//获取通讯录中的所有人CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks); |
?
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667 |
//通讯录中人数CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);//循环,获取每个人的个人信息for (NSInteger i = 0; i < nPeople; i++) { //新建一个addressBook model类 TKAddressBook *addressBook = [[TKAddressBook alloc] init]; //获取个人 ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); //获取个人名字 CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty); CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty); CFStringRef abFullName = ABRecordCopyCompositeName(person); NSString *nameString = (__bridge NSString *)abName; NSString *lastNameString = (__bridge NSString *)abLastName; if ((__bridge id)abFullName != nil) { nameString = (__bridge NSString *)abFullName; } else { if ((__bridge id)abLastName != nil) { nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString]; } } addressBook.name = nameString; addressBook.recordID = (int)ABRecordGetRecordID(person);; ABPropertyID multiProperties[] = { kABPersonPhoneProperty, kABPersonEmailProperty }; NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID); for (NSInteger j = 0; j < multiPropertiesTotal; j++) { ABPropertyID property = multiProperties[j]; ABMultiValueRef valuesRef = ABRecordCopyValue(person, property); NSInteger valuesCount = 0; if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef); if (valuesCount == 0) { CFRelease(valuesRef); continue; } //获取电话号码和email for (NSInteger k = 0; k < valuesCount; k++) { CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k); switch (j) { case 0: {// Phone number addressBook.tel = (__bridge NSString*)value; break; } case 1: {// Email addressBook.email = (__bridge NSString*)value; break; } } CFRelease(value); } CFRelease(valuesRef); } //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息 [addressBookTemp addObject:addressBook]; if (abName) CFRelease(abName); if (abLastName) CFRelease(abLastName); if (abFullName) CFRelease(abFullName); } |
Three, displayed in the table?
| 123456789 |
//行数- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return1;} //列数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return[addressBookTemp count];} |
?
| 12345678910111213141516171819 |
//cell内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier = @"ContactCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row]; cell.textLabel.text = book.name; cell.detailTextLabel.text = book.tel; return cell;} |
List effect
PS: Phone number in the Address book "-" can be stored in the array before processing, belongs to the scope of nsstring processing, there are many solutions, this article does not add more information
[Turn]ios skill to get the contents of the native Address Book, parse the address book source code