iOS phone book import code, currently only implement the main box program, details renewal; analysis does not cause a memory leak
Reference
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
Code debugging: xcode4.5,ios6.0
Body Code
- (void) testaddress{abaddressbookref addressbook=Nil; if([[Uidevice currentdevice].systemversion Floatvalue] >=6.0) {AddressBook=abaddressbookcreatewithoptions (null, NULL); //wait for consent and then execute downdispatch_semaphore_t sema = Dispatch_semaphore_create (0); Abaddressbookrequestaccesswithcompletion (AddressBook,^(BOOLgranted, cferrorref error) {dispatch_semaphore_signal (SEMA); }); Dispatch_semaphore_wait (Sema, dispatch_time_forever); Dispatch_release (SEMA); }//Else// {//addressbook = Abaddressbookcreate ();// }cfarrayref Results=abaddressbookcopyarrayofallpeople (AddressBook); NSLog (@"%@", results); intPeoplecount =Cfarraygetcount (results); for(intI=0; i<peoplecount; i++) {Abrecordref record=Cfarraygetvalueatindex (results, i); NSLog (@"%@", record); NSString*fn,*ln,*fullname; FN= LN = FullName =Nil; Cftyperef vtmp=NULL; Vtmp=Abrecordcopyvalue (record, kabpersonfirstnameproperty); if(vtmp) {fn=[NSString stringwithstring:vtmp]; Cfrelease (VTMP); Vtmp=NULL; } vtmp=Abrecordcopyvalue (record, kabpersonlastnameproperty); if(vtmp) {ln=[NSString stringwithstring:vtmp]; Cfrelease (VTMP); Vtmp=NULL; } NSLog (@"%@ ,%@", FN, LN); //Read PhoneAbmultivalueref phones =Abrecordcopyvalue (record, kabpersonphoneproperty); intPhonecount =abmultivaluegetcount (phones); for(intj=0; j<phonecount; J + +) { //labelCfstringref lable =Abmultivaluecopylabelatindex (phones, j); //Phone numberCfstringref PhoneNumber =Abmultivaluecopyvalueatindex (phones, j); //Localize LabelCfstringref LL =Abaddressbookcopylocalizedlabel (lable); NSLog (@"\t%@,%@,%@", (NSString *) lable, (NSString *) ll, (NSString *) PhoneNumber); if(LL) cfrelease (LL); if(lable) cfrelease (lable); if(PhoneNumber) cfrelease (PhoneNumber); } if(phones) cfrelease (phones); Record=NULL; } if(Results) cfrelease (results); Results=Nil; if(AddressBook) cfrelease (addressbook); AddressBook=NULL;}
Go to: http://www.cnblogs.com/GoGoagg/archive/2012/12/20/2826804.html