First import the header file
#import <AddressBook/AddressBook.h>
Get permission to read contacts
- (void) loadperson{abaddressbookref addressbookref=abaddressbookcreatewithoptions (null, NULL); if(Abaddressbookgetauthorizationstatus () = =kabauthorizationstatusnotdetermined) {abaddressbookrequestaccesswithcompletion (Addressbookref,^(BOOLgranted, cferrorref error) {Cferrorref*error1 =NULL; Abaddressbookref AddressBook=abaddressbookcreatewithoptions (NULL, Error1); [Self copyaddressbook:addressbook]; }); } Else if(Abaddressbookgetauthorizationstatus () = =kabauthorizationstatusauthorized) {Cferrorref*error =NULL; Abaddressbookref AddressBook=abaddressbookcreatewithoptions (NULL, error); [Self copyaddressbook:addressbook]; } Else{Dispatch_async (Dispatch_get_main_queue (),^{ //user does not give permission }); }}
Getting information about each contact can be stored in a model
- (void) Copyaddressbook: (abaddressbookref) addressbook{cfindex numberofpeople=Abaddressbookgetpersoncount (AddressBook); Cfarrayref people=abaddressbookcopyarrayofallpeople (AddressBook); for(inti =0; i < numberofpeople; i++) {Abrecordref person=Cfarraygetvalueatindex (people, I); NSString*firstname = (__bridge NSString *) (Abrecordcopyvalue (person, kabpersonfirstnameproperty)); NSString*lastname = (__bridge NSString *) (Abrecordcopyvalue (person, kabpersonlastnameproperty)); //Read MiddleNameNSString *middlename = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonmiddlenameproperty); NSLog (@"%@", MiddleName); //Read prefix prefixNSString *prefix = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonprefixproperty); //read suffix suffixNSString *suffix = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonsuffixproperty); //Read nickname.NSString *nickname = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonnicknameproperty); NSLog (@"%@", nickname); //Read FirstName phonetic transcriptionNSString *firstnamephonetic = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonfirstnamephoneticproperty); //Read LastName phonetic transcriptionNSString *lastnamephonetic = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonlastnamephoneticproperty); //Read MiddleName phonetic transcriptionNSString *middlenamephonetic = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonmiddlenamephoneticproperty);//NSLog (@ "%@", middlenamephonetic)//Read Organization companyNSString *organization = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonorganizationproperty); //Read JobTitle workNSString *jobtitle = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonjobtitleproperty); //Read Department DepartmentNSString *department = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersondepartmentproperty); //Read Birthday birthdayNSDate *birthday = (__bridge nsdate*) Abrecordcopyvalue (person, kabpersonbirthdayproperty); //Read Note MemoNSString *note = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonnoteproperty); //The time that the record was first addedNSString *firstknow = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersoncreationdateproperty); NSLog (@"The first time the record was added%@\n", Firstknow); //last time the record was modifiedNSString *lastknow = (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonmodificationdateproperty); NSLog (@"the last time the record was modified%@\n", Lastknow); //Get email multi-valueAbmultivalueref email =Abrecordcopyvalue (person, kabpersonemailproperty); intEmailcount =abmultivaluegetcount (email); for(intx =0; x < Emailcount; X + +) { //Get email Labelnsstring* Emaillabel = (__bridge nsstring*) Abaddressbookcopylocalizedlabel (abmultivaluecopylabelatindex (email, x)); //Get Email valuensstring* emailcontent = (__bridge nsstring*) Abmultivaluecopyvalueatindex (email, x); } //read address multi-valueAbmultivalueref address =Abrecordcopyvalue (person, kabpersonaddressproperty); intCount =Abmultivaluegetcount (address); for(intj =0; J < Count; J + +) { //Get Address Labelnsstring* Addresslabel = (__bridge nsstring*) Abmultivaluecopylabelatindex (address, j); //Get the Address 6 property under the labelnsdictionary* personaddress = (__bridge nsdictionary*) Abmultivaluecopyvalueatindex (address, j); NSString* Country = [personaddress valueforkey: (NSString *) Kabpersonaddresscountrykey]; NSString* City = [personaddress valueforkey: (NSString *) Kabpersonaddresscitykey]; NSString* state = [personaddress valueforkey: (NSString *) Kabpersonaddressstatekey]; NSString* Street = [personaddress valueforkey: (NSString *) Kabpersonaddressstreetkey]; NSString* Zip = [personaddress valueforkey: (NSString *) Kabpersonaddresszipkey]; NSString* Coutntrycode = [personaddress valueforkey: (NSString *) Kabpersonaddresscountrycodekey]; } //get dates Multi-valueAbmultivalueref dates =Abrecordcopyvalue (person, kabpersondateproperty); intDatescount =Abmultivaluegetcount (dates); for(inty =0; Y < Datescount; y++) { //Get dates Labelnsstring* Dateslabel = (__bridge nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (dates, y)); //Get dates Valuensstring* datescontent = (__bridge nsstring*) Abmultivaluecopyvalueatindex (dates, y); } //Get Kind valueCfnumberref RecordType =Abrecordcopyvalue (person, kabpersonkindproperty); if(RecordType = =kabpersonkindorganization) { //it ' s a companyNSLog (@"it ' s a company\n"); } Else { //it ' s a person, resource, orNSLog (@"it ' s a person, resource, or room\n"); } //get im Multi-valueAbmultivalueref Instantmessage =Abrecordcopyvalue (person, kabpersoninstantmessageproperty); for(intL =1; L < Abmultivaluegetcount (instantmessage); l++) { //Get im Labelnsstring* Instantmessagelabel = (__bridge nsstring*) Abmultivaluecopylabelatindex (Instantmessage, L); //gets the 2 property under the labelnsdictionary* instantmessagecontent = (__bridge nsdictionary*) Abmultivaluecopyvalueatindex (Instantmessage, L); NSString* Username = [Instantmessagecontent valueforkey: (NSString *) Kabpersoninstantmessageusernamekey]; NSString* Service = [instantmessagecontent valueforkey: (NSString *) Kabpersoninstantmessageservicekey]; } //read phone multi-valueAbmultivalueref phone =Abrecordcopyvalue (person, kabpersonphoneproperty); for(intK =0; K<abmultivaluegetcount (phone); k++) { //get the phone labelNSString * Personphonelabel = (__bridge nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (phone, k)); //get the phone value under the labelNSString * Personphone = (__bridge nsstring*) Abmultivaluecopyvalueatindex (phone, k); NSLog (@"%@", Personphone); } //get URL Multi-valueAbmultivalueref URL =Abrecordcopyvalue (person, kabpersonurlproperty); for(intm =0; M < Abmultivaluegetcount (URL); m++) { //get the phone labelNSString * URLLabel = (__bridge nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (URL, m)); //get the phone value under the labelNSString * urlcontent = (__bridge nsstring*) Abmultivaluecopyvalueatindex (url,m); NSLog (@"%@", urlcontent); } //Reading PhotosNSData *image = (__bridge nsdata*) Abpersoncopyimagedata (person); }}
OK, key to everyone.
Release
Read iOS contacts