Yesterday because the project needs to access the system Address Book to get the phone number, so from the ignorance, began to Daoteng, Daoteng an afternoon, finally finished. Write this blog is to record, the next time you do not make a mistake. At the same time, I have the same rookie can avoid a detour.
Well, to get started, to access the system's address book, you first need to add AddressBook.framework
and AddressBookUI.framework
two frames to the "Link Binary with Libraries" in your project build phase, and then you can start.
First we need to create a controller: Viewcontroller, import the header file in the. h file:<AddressBook/AddressBook.h>, <addressbookui/ Addressbookui.h>,
#import <AddressBook/AddressBook.h>#import <AddressBookUI/AddressBookUI.h>
then the controller implements abpeoplepickernavigationcontrollerdelegate, uinavigationcontrollerdelegate protocol
@interface Viewcontroller () <abpeoplepickernavigationcontrollerdelegate, uinavigationcontrollerdelegate>
Create Abpeoplepickernavigationcontroller in the Viewdidappear method and set Viewcontroller as the delegate object
-(void) viewdidappear: (BOOL) animated{ [Super viewdidappear:animated]; *PNC = [[Abpeoplepickernavigationcontroller alloc] init]; = self ; [Self PRESENTVIEWCONTROLLER:PNC animated:yes completion:nil];}
Next you need to implement the Abpeoplepickernavigationcontrollerdelegate protocol
#pragmamark-abpeoplepickernavigationcontrollerdelegate-(void) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker Didselectperson: (abrecordref) Person property: (Abpropertyid) Property identifier: ( Abmultivalueidentifier) identifier {abmultivalueref phone=Abrecordcopyvalue (person, kabpersonphoneproperty); Longindex =Abmultivaluegetindexforidentifier (Phone,identifier); NSString*phoneno = (__bridge NSString *) Abmultivaluecopyvalueatindex (phone, index); Phoneno= [Phoneno stringbyreplacingoccurrencesofstring:@"-"Withstring:@""]; NSLog (@"%@", Phoneno); if(Phone && Phoneno.length = = One) {[Peoplepicker dismissviewcontrolleranimated:yes completion:nil]; return; }Else{Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Error Hints"Message@"Please select the correct phone number" Delegate: Self Cancelbuttontitle:@"Determine"Otherbuttontitles:nil]; [Alertview show]; }}- (void) Peoplepickernavigationcontroller: (abpeoplepickernavigationcontroller*) Peoplepicker Didselectperson: (abrecordref) person Ns_available_ios (8_0) {Abpersonviewcontroller*personviewcontroller =[[Abpersonviewcontroller alloc] init]; Personviewcontroller.displayedperson=Person ; [Peoplepicker Pushviewcontroller:personviewcontroller Animated:yes]; }- (void) Peoplepickernavigationcontrollerdidcancel: (Abpeoplepickernavigationcontroller *) peoplepicker{[Peoplepicker dismissviewcontrolleranimated:yes completion:nil];}-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker Shouldcontinueafterselectingperson: (abrecordref) person Ns_deprecated_ios (2_0, 8_0) {returnYES;}-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker Shouldcontinueafterselectingperson: (abrecordref): (Abpropertyid) property Identifier: (abmultivalueidentifier) identifier Ns_deprecated_ios (2_0, 8_0) {abmultivalueref phone=Abrecordcopyvalue (person, kabpersonphoneproperty); Longindex =Abmultivaluegetindexforidentifier (Phone,identifier); NSString*phoneno = (__bridge NSString *) Abmultivaluecopyvalueatindex (phone, index); Phoneno= [Phoneno stringbyreplacingoccurrencesofstring:@"-"Withstring:@""]; NSLog (@"%@", Phoneno); if(Phone && Phoneno.length = = One) {[Peoplepicker dismissviewcontrolleranimated:yes completion:nil]; returnNO; }Else{Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Error Hints"Message@"Please select the correct phone number" Delegate: Self Cancelbuttontitle:@"Determine"Otherbuttontitles:nil]; [Alertview show]; } returnYES;}@end
I thought it was done in iOS7, but there was no problem in the iOS8, but there was a problem with the pit Daddy, that is, after choosing the contact person
Abpeoplepickernavigationcontroller will automatically dismiss out, this problem can pit me. Asked Google and Niang, in Stackvoerflow found a similar problem, but have not been resolved, in feel no way, and began to see ABPeoplePickerNavigationController.h's head file, found
Predicateforselectionofperson attribute, the following code is added to the Viewdidappear method:
if 8.0 { = [Nspredicate predicatewithvalue:false]; }
Run the program and you are done.
Ios:abpeoplepickernavigationcontroller System Address Book usage