Obtain the content in the address book of the local machine, which is displayed in the list (table). After iOS6, Apple controls the permission to call controls such as the address book calendar in the system, add the request permission to the address book.
1. Add AddressBook. framework and AddressBookUI. framework to the project.
2. Obtain the address book
1. Define an array in infterface and initialize it in the init method.
NSMutableArray *addressBookTemp;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ addressBookTemp = [NSMutableArray array];}
2. define a model to store each attribute in the address book.
Create a new class that inherits from NSObject. In. h
@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
Synthesize in the. m file
@implementation TKAddressBook@synthesize name, email, tel, recordID, sectionNumber;@end
3. Get contacts
After iOS6, you must obtain the permission to obtain the address book.
// Create an address book class ABAddressBookRef addressBooks = nil; if ([UIDevice currentDevice]. systemVersion floatValue]> = 6.0) {addressBooks = abaddressbookcreatewitexceptions (NULL, NULL); // obtain the address book permission eclipsema = dispatch_semaphore_create (0); Combine (addressBooks, ^ (bool granted, CFErrorRef error) {dispatch_semaphore_signal (sema) ;}); then (sema, DISPATCH_TIME_FOREVER); dispatch_release (sema);} else {addressBooks = ABAddressBookCreate ();} // obtain the owner CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople (addressBooks) in the address book );
3. Display in table
// Number of rows-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1;} // Number of columns-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return [addressBookTemp count];}
List Effect