Scenario One: Select a contact's phone number directly
There is no need to first get all the contacts to do their own contact list, the direct use of the system comes with the AddressBookUI/ABPeoplePickerNavigationController.h
good.
First you need to introduce the following three files
#import <AddressBookUI/ABPeoplePickerNavigationController.h>#import <AddressBook/ABPerson.h>#import <AddressBookUI/ABPersonViewController.h>
Then initialize the Abpeoplepickernavigationcontroller.
ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc] init];nav.peoplePickerDelegate = self;if(IOS8_OR_LATER){ nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];}[self presentViewController:nav animated:YES completion:nil];
After iOS8, you need to add nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
this piece of code, otherwise select the contact will be directly dismiss, cannot enter the details select phone.
Finally set up the agent
//取消选择- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{ [peoplePicker dismissViewControllerAnimated:YES completion:nil];}
IOS8 under
- (void) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker DidSelectPerson: ( ABRECORDREF): (Abpropertyid) Property identifier: (Abmultivalueidentifier) Identifier {Abmultivalueref Phone = Abrecordcopyvalue (person, kabpersonphoneproperty);Long index = Abmultivaluegetindexforidentifier (phone,identifier);NSString *phoneno = (__bridgeNSString *) Abmultivaluecopyvalueatindex (phone, index);if ([Phoneno hasprefix:@ "+"]) {Phoneno = [Phoneno substringfromindex:3]; } Phoneno = [Phoneno stringbyreplacingoccurrencesofstring:@ "-" withstring:@""]; NSLog (@ "%@", Phoneno); if (phone && [zxvalidatehelper Checktel:phoneno]) {phonenum = Phoneno; [self. TableView Reloaddata]; [Peoplepicker dismissviewcontrolleranimated:YES Completion:nil]; return;}} -(void) Peoplepickernavigationcontroller: (abpeoplepickernavigationcontroller*) peoplepicker DidSelectPerson: ( ABRECORDREF) person{Abpersonviewcontroller *personviewcontroller = [[Abpersonviewcontroller alloc] init]; Personviewcontroller. Displayedperson = person; [Peoplepicker pushviewcontroller:personviewcontroller animated:YES];}
IOS7 under
- (BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref) person{ReturnYES;} - (BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref): (Abpropertyid) Property identifier: ( Abmultivalueidentifier) identifier{Abmultivalueref phone = abrecordcopyvalue (person, kabpersonphoneproperty);Long index = Abmultivaluegetindexforidentifier (phone,identifier);NSString *phoneno = (__bridgeNSString *) Abmultivaluecopyvalueatindex (phone, index);if ([Phoneno hasprefix:@ "+"]) {Phoneno = [Phoneno substringfromindex:3];} Phoneno = [Phoneno Stringbyreplaci Ngoccurrencesofstring:@ "-" withstring:@ ""; NSLog (@ "%@", Phoneno); if (phone && [zxvalidatehelper Checktel:phoneno]) {phonenum = Phoneno; [self. TableView Reloaddata]; [Peoplepicker dismissviewcontrolleranimated:YES Completion:nil]; return NO;} return YES;}
Effect Demo Scenario Two: Reading contacts
You need to read the Address book data here.
First introduce the header file
#import <AddressBook/AddressBook.h>
Next, generate contacts based on permissions
- (void) loadperson{Abaddressbookref addressbookref = abaddressbookcreatewithoptions (NullNULL);if (abaddressbookgetauthorizationstatus () = = kabauthorizationstatusnotdetermined) {abaddressbookrequestaccesswithc Ompletion (Addressbookref, ^ (BOOL Granted,Cferrorref error) {cferrorref *error1 = NULL; Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, Error1); [SelfCopyaddressbook:addressbook];}); } Else if (abaddressbookgetauthorizationstatus () = = kabauthorizationstatusauthorized) { Cferrorref *error = NULL; Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, error); [Selfcopyaddressbook:addressbook];} else { Dispatch_async (Dispatch_get_main_queue (), ^{ //update interface [HUD turntoerror:@ "does not get Address Book permissions"];});}
Then loop through the information for each contact and suggest that you build a model to save it.
- (void) Copyaddressbook: (abaddressbookref) addressbook{Cfindex numberofpeople = Abaddressbookgetpersoncount (addressbook);Cfarrayref people = abaddressbookcopyarrayofallpeople (addressbook);for (int i =0; i < numberofpeople; i++) {Abrecordref person =Cfarraygetvalueatindex (people, I);NSString *firstname = (__bridgeNSString *) (Abrecordcopyvalue (person, kabpersonfirstnameproperty));NSString *lastname = (__bridgeNSString *) (Abrecordcopyvalue (person, kabpersonlastnameproperty));Read MiddleNameNSString *middlename = (nsstring*) Abrecordcopyvalue (person, kabpersonmiddlenameproperty);Read prefix prefixNSString *prefix = (nsstring*) Abrecordcopyvalue (person, kabpersonprefixproperty);Read suffix suffixNSString *suffix = (nsstring*) Abrecordcopyvalue (person, kabpersonsuffixproperty);Read nickname.NSString *nickname = (nsstring*) Abrecordcopyvalue (person, kabpersonnicknameproperty);Read FirstName phonetic transcriptionNSString *firstnamephonetic = (nsstring*) Abrecordcopyvalue (person, kabpersonfirstnamephoneticproperty);Read LastName phonetic transcriptionNSString *lastnamephonetic = (nsstring*) Abrecordcopyvalue (person, kabpersonlastnamephoneticproperty);Read MiddleName phonetic transcriptionNSString *middlenamephonetic = (nsstring*) Abrecordcopyvalue (person, kabpersonmiddlenamephoneticproperty);Read Organization CompanyNSString *organization = (nsstring*) Abrecordcopyvalue (person, kabpersonorganizationproperty);Read JobTitle workNSString *jobtitle = (nsstring*) Abrecordcopyvalue (person, kabpersonjobtitleproperty);Read Department DepartmentNSString *department = (nsstring*) Abrecordcopyvalue (person, kabpersondepartmentproperty);Read Birthday BirthdayNSDate *birthday = (nsdate*) Abrecordcopyvalue (person, kabpersonbirthdayproperty);Read Note MemoNSString *note = (nsstring*) Abrecordcopyvalue (person, kabpersonnoteproperty);The time that the record was first addedNSString *firstknow = (nsstring*) Abrecordcopyvalue (person, kabpersoncreationdateproperty);NSLog (@ "The first time the record was added%@\n", Firstknow);Last time the record was modifiedNSString *lastknow = (nsstring*) Abrecordcopyvalue (person, kabpersonmodificationdateproperty);NSLog (@ "The last time the record was modified%@\n", Lastknow);Get email multi-value Abmultivalueref email = abrecordcopyvalue (person, kabpersonemailproperty);int emailcount = abmultivaluegetcount (email);for (int x =0; x < Emailcount; X + +) {Get email Labelnsstring* Emaillabel = (nsstring*) Abaddressbookcopylocalizedlabel (abmultivaluecopylabelatindex (email, x));Get Email valuensstring* emailcontent = (nsstring*) Abmultivaluecopyvalueatindex (email, x); }Read address multiple values Abmultivalueref address = Abrecordcopyvalue (person, kabpersonaddressproperty);int count = Abmultivaluegetcount (address);ForInt J =0; J < Count; J + +) {Get Address Labelnsstring* Addresslabel = (nsstring*) Abmultivaluecopylabelatindex (address, j);Get the Address 6 property under the labelnsdictionary* personaddress = (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 multivalued abmultivalueref dates = Abrecordcopyvalue (person, kabpersondateproperty);int datescount = Abmultivaluegetcount (dates);for (int y =0; Y < Datescount; y++) {Get dates Labelnsstring* Dateslabel = (nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (dates, y));Get dates Valuensstring* datescontent = (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-valued abmultivalueref instantmessage = Abrecordcopyvalue (person, kabpersoninstantmessageproperty);for (int L =1; L < Abmultivaluegetcount (instantmessage); l++) {Get IM Labelnsstring* Instantmessagelabel = (nsstring*) Abmultivaluecopylabelatindex (Instantmessage, L);Gets the 2 property under the labelnsdictionary* instantmessagecontent = (nsdictionary*) Abmultivaluecopyvalueatindex (Instantmessage, L);nsstring* username = [Instantmessagecontent valueforkey: (NSString *) Kabpersoninstantmessageusernamekey];nsstring* service = [instantmessagecontent Valueforkey: (NSString *) Kabpersoninstantmessageservicekey]; }Read phone multi-value Abmultivalueref phone = abrecordcopyvalue (person, kabpersonphoneproperty);for (int k =0; K<abmultivaluegetcount (phone); k++) {Get the phone labelNSString * Personphonelabel = (nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (phone, k));Get the phone value under the labelNSString * Personphone = (nsstring*) Abmultivaluecopyvalueatindex (phone, k); } //Get URL multi-value abmultivalueref URL = abrecordcopyvalue (person, kabpersonurlproperty); for (int m = 0; m < abmultivaluegetcount (URL); m++) { //Get Phone label nsstring * URLLabel = (Nsstri ng*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (URL, m)); //Get the phone value under the label NSString * urlcontent = (nsstring*) Abmultivaluecopyvalueatindex (url,m);} //Read photo NSData *image = (nsdata*) abpersoncopyimagedata (person);}}
Effect:
2015-04-06 16_30_54.gif
iOS Address Book Development