First, create Address book object
Self.addressbook=abaddressbookcreatewithoptions (null, NULL);
Request access to the user's address book, regardless of whether or not the block will call
Abaddressbookrequestaccesswithcompletion (Self.addressbook, ^ (bool granted, cferrorref error) {
if (!granted) {
NSLog (@ "Not getting Address Book access! ");
}
});
Abpeoplepickernavigationcontroller *peoplepicker = [[Abpeoplepickernavigationcontroller alloc] init];
Peoplepicker.peoplepickerdelegate = self;
[Self Presentviewcontroller:peoplepicker animated:yes completion:nil];
II. implementation of agency agreements
Abpeoplepickernavigationcontrollerdelegate
Third, the implementation of proxy methods
#pragma mark--abpeoplepickernavigationcontrollerdelegate
-(void) Peoplepickernavigationcontroller: (abpeoplepickernavigationcontroller*) peoplepicker DidSelectPerson: ( ABRECORDREF): (Abpropertyid) Property identifier: (Abmultivalueidentifier) Identifier {
Get information from a record
Abmultivalueref valuesref = Abrecordcopyvalue (person, kabpersonphoneproperty);
LastName is the first name, FirstName, and two strings are concatenated to wholename full name.
NSString *firstname= (__bridge NSString *) abrecordcopyvalue (person, kabpersonfirstnameproperty);//Notice that there is a strong turn, You don't have to release resources.
NSString *lastname= (__bridge NSString *) abrecordcopyvalue (person, kabpersonlastnameproperty);
NSString *[email protected] "";
if (firstname.length>0) {
if (lastname.length>0) {
Wholename=[lastname Stringbyappendingstring:firstname];
}
else{
Wholename=firstname;
}
}
else {
if (lastname.length>0)
{
Wholename=[wholename Stringbyappendingstring:lastname];
}
else{
[email protected] "unknown contact";
}
}
Cfindex index = abmultivaluegetindexforidentifier (valuesref,identifier);
NSString *value = (__bridge NSString *) Abmultivaluecopyvalueatindex (valuesref,index);
[Self Dismissviewcontrolleranimated:yes completion:^{
if (value.length!=0| | wholename.length!=0)
{
Generally read the number will be short-term connection format, the following method can remove the number of the horizontal bar
Nsarray *nbarr=[value componentsseparatedbystring:@ "-"];
NSString *[email protected] "";
for (int i=0; i<nbarr.count; i++) {
FINALYNB=[FINALYNB Stringbyappendingstring:[nbarr Objectatindex:i]];
}
NSLog (@ "%@,%@", WHOLENAME,FINALYNB);
}
}];
}
iOS read contacts get friends Address book information [first name (surname + First name), mobile phone number (multiple numbers), etc.]