First, add addressbook.framework and addressbookui.framework to the project
Second, access to the Address Book
1. Define the array in Infterface and initialize it in the Init method
Copy Code code as follows:
Nsmutablearray *addressbooktemp;
-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil
{
Addressbooktemp = [Nsmutablearray array];
}
2, the definition of a model, used to store the various attributes in the Address Book
Creates a new class that inherits from NSObject, in. h
Copy Code code as follows:
@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
Copy Code code as follows:
@implementation Tkaddressbook
@synthesize name, email, tel, recordid, Sectionnumber;
@end
3. Get Contact
After iOS6, getting the address book requires permission
Copy Code code as follows:
Create a new Address Book class
Abaddressbookref addressbooks = nil;
if ([[[Uidevice currentdevice].systemversion Floatvalue] >= 6.0)
{
Addressbooks = abaddressbookcreatewithoptions (null, NULL);
Get Address Book permissions
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 ();
}
Get everyone in the address book
Cfarrayref allpeople = abaddressbookcopyarrayofallpeople (addressbooks);
Number of people in the Address book
Cfindex npeople = Abaddressbookgetpersoncount (addressbooks);
Loop to get everyone's personal information
for (Nsinteger i = 0; i < npeople; i++)
{
Create a new AddressBook model class
Tkaddressbook *addressbook = [[Tkaddressbook alloc] init];
Access to Personal
Abrecordref person = cfarraygetvalueatindex (allpeople, i);
Get a personal name
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
}
Get phone numbers and emails
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);
}
Add personal information to the array, and after the loop completes the Addressbooktemp contains information about all the contacts
[Addressbooktemp Addobject:addressbook];
if (abname) cfrelease (abname);
if (ablastname) cfrelease (ablastname);
if (abfullname) cfrelease (abfullname);
}
third, display in table
Copy Code code as follows:
Number of rows
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
return 1;
}
Number of columns
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
return [addressbooktemp Count];
}
Cell contents
-(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 effects
PS: The phone number in the Address book "-" can be stored in the array before processing, belong to the category of nsstring processing, there are many solutions, this article does not add to the description
Four, delete address book data
Copy Code code as follows:
<span style= "White-space:pre" > </span>abrecordref person = objc_unretainedpointer ([myContacts ObjectAtIndex:indexPath.row]);
Cferrorref *error;
Abaddressbookremoverecord (addressbook, person, error);
Abaddressbooksave (addressbook, error);
MyContacts = nil;
[TableView Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationfade];