Since the system's address book provides a new API at IOS9, we use 2 of our frameworks. First we will import the framework:
/// frame before IOS 9 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> /// new Framework for IOS 9 #import <ContactsUI/ContactsUI.h> #define is_up_ios_9 @interface viewcontroller:uiviewcontroller< Abpeoplepickernavigationcontrollerdelegate,cncontactpickerdelegate>
Then add the code in the VC that needs to call the address Book
#pragmaMark----Call the system Address Book-(void) Judgeaddressbookpower {///get Address Book permissions, call system Contacts[Self checkaddressbookauthorization:^ (BOOLIsAuthorized,BOOLisup_ios_9) { if(isauthorized) {[Self calladdressbook:isup_ios_9]; }Else{NSLog (@"Go to Settings > Privacy > Contacts to open Permissions settings for this app"); } }]; } - (void) Checkaddressbookauthorization: (void(^) (BOOLIsAuthorized,BOOLisup_ios_9)) Block {if(is_up_ios_9) {Cncontactstore* Contactstore =[[Cncontactstore alloc]init]; if([cncontactstore authorizationstatusforentitytype:cnentitytypecontacts] = =cnauthorizationstatusnotdetermined) {[Contactstore requestaccessforentitytype:cnentitytypecontacts Completionhandler:^ (BOOL Granted, Nserror *__nullable Error) { if(Error) {NSLog (@"Error:%@", error); } Else if(!granted) {block (No,yes); } Else{block (yes,yes); } }]; } Else if([cncontactstore authorizationstatusforentitytype:cnentitytypecontacts] = =cnauthorizationstatusauthorized) {block (Yes,yes); } Else{NSLog (@"Go to Settings > Privacy > Contacts to open Permissions settings for this app"); } }Else{abaddressbookref AddressBook=abaddressbookcreatewithoptions (null, NULL); Abauthorizationstatus Authstatus=Abaddressbookgetauthorizationstatus (); if(Authstatus = =kabauthorizationstatusnotdetermined) {abaddressbookrequestaccesswithcompletion (addressbook,^(BOOLgranted, cferrorref error) {Dispatch_async (Dispatch_get_main_queue (),^{ if(Error) {NSLog (@"Error:%@", (__bridge Nserror *) error); } Else if(!granted) {block (no,no); } Else{block (yes,no); } }); }); }Else if(Authstatus = =kabauthorizationstatusauthorized) {block (yes,no); }Else{NSLog (@"Go to Settings > Privacy > Contacts to open Permissions settings for this app"); } } } - (void) Calladdressbook: (BOOL) isup_ios_9 {if(isup_ios_9) {Cncontactpickerviewcontroller*contactpicker =[[Cncontactpickerviewcontroller alloc] init]; Contactpicker.Delegate=Self ; Contactpicker.displayedpropertykeys=@[cncontactphonenumberskey]; [Self Presentviewcontroller:contactpicker animated:yes completion:nil]; }Else{Abpeoplepickernavigationcontroller*peoplepicker =[[Abpeoplepickernavigationcontroller alloc] init]; Peoplepicker.peoplepickerdelegate=Self ; [Self Presentviewcontroller:peoplepicker animated:yes completion:nil]; } } #pragmaMark-Cncontactpickerdelegate enters the System contacts page---(void) Contactpicker: (Cncontactpickerviewcontroller *) Picker Didselectcontactproperty: (Cncontactproperty *) Contactproperty {cnphonenumber*phonenumber = (Cnphonenumber *) Contactproperty.value; [Self Dismissviewcontrolleranimated:yes completion:^{ ///Contact PersonNSString *text1 = [NSString stringWithFormat:@"%@%@", Contactproperty.contact.familyname,contactproperty.contact.givenname]; ///TelephoneNSString *text2 =Phonenumber.stringvalue; //Text2 = [Text2 stringbyreplacingoccurrencesofstring:@ "-" withstring:@ ""]; NSLog (@"Contact:%@, Tel:%@", TEXT1,TEXT2); }]; } #pragmaMark-Abpeoplepickernavigationcontrollerdelegate enters the System contacts page---(void) Peoplepickernavigationcontroller: (abpeoplepickernavigationcontroller*) Peoplepicker Didselectperson: (abrecordref) Person property: (Abpropertyid) Property identifier: ( Abmultivalueidentifier) Identifier {abmultivalueref Valuesref=Abrecordcopyvalue (person, kabpersonphoneproperty); Cfindex Index=Abmultivaluegetindexforidentifier (Valuesref,identifier); Cfstringref value=Abmultivaluecopyvalueatindex (Valuesref,index); Cfstringref Anfullname=abrecordcopycompositename (person); [Self Dismissviewcontrolleranimated:yes completion:^{ ///Contact PersonNSString *text1 = [NSString stringWithFormat:@"%@", Anfullname]; ///TelephoneNSString *text2 = (__bridge nsstring*) value; //Text2 = [Text2 stringbyreplacingoccurrencesofstring:@ "-" withstring:@ ""]; NSLog (@"Contact:%@, Tel:%@", TEXT1,TEXT2); }];
}
Finally we can call [self judgeaddressbookpower], and we can simply call the system Address Book.
If we enter a number can also directly determine whether this number is in the Address book, if the user information is transferred,
#pragmaMark--#pragmaMark--Search phone Address Book by phone number---(NSString *) Getnamebytel: (NSString *) telstr {telstr= [Telstr stringbyreplacingoccurrencesofstring:@" "Withstring:@""]; Nsmutablearray* Personarray =[[Nsmutablearray alloc] init]; //Open Phone Book databaseAbaddressbookref addressref=abaddressbookcreate (); NSString*firstname, *lastname, *FullName; //return all contacts to an arrayPersonarray = (__bridge Nsmutablearray *) abaddressbookcopyarrayofallpeople (ADDRESSREF); //return number of contacts//Cfindex personcount = Abaddressbookgetpersoncount (addressref); for(IDPersoninchPersonarray) {FirstName= (__bridge NSString *) Abrecordcopyvalue ((__bridge abrecordref) (person), kabpersonfirstnameproperty); FirstName= [FirstName Stringbyappendingformat:@" "]; LastName= (__bridge NSString *) Abrecordcopyvalue ((__bridge abrecordref) (person), kabpersonlastnameproperty); if(LastName! =Nil) {FullName= [FirstName Stringbyappendingformat:@"%@", LastName]; } Else{fullName=FirstName; } NSLog (@"Contact Person ===%@", FullName); Abmultivalueref Phones=(Abmultivalueref) abrecordcopyvalue ((__bridge abrecordref) (person), kabpersonphoneproperty); for(inti =0; I < abmultivaluegetcount (phones); i++) {NSString*phone = (__bridge NSString *) Abmultivaluecopyvalueatindex (phones, i); Phone= [Phone stringbyreplacingoccurrencesofstring:@"("Withstring:@""]; Phone= [Phone stringbyreplacingoccurrencesofstring:@")"Withstring:@""]; Phone= [Phone stringbyreplacingoccurrencesofstring:@"-"Withstring:@""]; Phone= [Phone stringbyreplacingoccurrencesofstring:@" "Withstring:@""]; NSLog (@"===%@", phone); if([phone isequaltostring:telstr]) {_isno=NO; Nsarray*array = [FullName componentsseparatedbystring:@" "]; FullName= [NSString stringWithFormat:@"%@%@", array[1],array[0]]; _moren=FullName; returnFullName; } Else{_isno=YES; } } } if(_isno = =YES) {_moren=@"Non-contact friends"; return @"Non-contact friends"; } returnNil;}
Tips: If you are adapting to iOS 10, you must add it in the source code mode of the Plist file
<key>NSContactsUsageDescription</key> <string>app requires your consent in order to access the contacts </ String>
iOS Call system Contacts (IOS9, IOS10) (reprint)