IOS tips: Get the content in the local address book and parse the address book source code

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.