/** Note: IOS9 will be able to use * first in the project to import Contactsui.framework and contacts.framework two frames * * * Source code link Address * Link:Http://pan.baidu.com/s/1mh9Q0BYPassword: wmj5*/#import "RootViewController.h"#import<Contacts/Contacts.h>#import<ContactsUI/ContactsUI.h>@interfaceRootviewcontroller () <CNContactPickerDelegate>{UITableView*_contacttableview; Nsmutablearray*data;}@end@implementationRootviewcontroller- (void) loadview{[Super Loadview]; UIButton*button =[UIButton Buttonwithtype:uibuttontypecustom]; Button.frame= CGRectMake ( -, -, -, -); [Button Settitle:@"Show Contact person"Forstate:0]; [Button Settitlecolor:[uicolor Cyancolor] forstate:0]; [Button addtarget:self action: @selector (showcontact:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button]; }- (void) viewdidload {[Super viewdidload]; Cnmutablecontact*contact =[self initializecontact]; //Add a contact person//[self addcontact:contact]; //EnquiryNsarray *arr = [Self querycontactwithname:@"Rain Road"]; //turn the immutable cncontact into Cnmutablecontact to perform the following actions//cnmutablecontact *contact = [arr[1] mutablecopy]; //Update//contact.givenname = @ "LF";//[self updatecontact:contact]; //Delete//[self deletecontact:contact];}/** * Query operation * * @return return array*/-(Nsarray *) Querycontactwithname: (NSString *) name{Cncontactstore*store =[[Cncontactstore alloc] init]; //Search CriteriaNspredicate *predicate =[Cncontact Predicateforcontactsmatchingname:name]; //extract Data (Keystofetch:@[cncontactgivennamekey] is the setting of which data is extracted from the contact person)Nsarray *contact =[Store unifiedcontactsmatchingpredicate:predicate Keystofetch:@[cncontactgivennamekey] error:nil]; returnContact ;}/** * Update Contact person * * @param contact updated contacts*/- (void) Updatecontact: (Cnmutablecontact *) contact{//Create a contact requestCnsaverequest *saverequest =[[Cnsaverequest alloc] init]; [Saverequest Updatecontact:contact]; //re-writeCncontactstore *store =[[Cncontactstore alloc] init]; [Store Executesaverequest:saverequest Error:nil];}/** * Delete contacts * * @param contact contacts deleted*/- (void) DeleteContact: (Cnmutablecontact *) contact{//Create a contact requestCnsaverequest *saverequest =[[Cnsaverequest alloc] init]; [Saverequest Deletecontact:contact]; //Write OperationsCncontactstore *store =[[Cncontactstore alloc] init]; [Store Executesaverequest:saverequest Error:nil];}/** * Add contacts * * @param contact person*/- (void) Addcontact: (Cnmutablecontact *) contact{//Create a contact requestCnsaverequest *saverequest =[[Cnsaverequest alloc] init]; [Saverequest addcontact:contact Tocontainerwithidentifier:nil]; //write a contact.Cncontactstore *store =[[Cncontactstore alloc] init]; [Store Executesaverequest:saverequest Error:nil];}/** * Set the basic properties of the contact * * @return Return the object of the contact person*/-(Cnmutablecontact *) initializecontact{//Create a Contact objectCnmutablecontact *contact =[[Cnmutablecontact alloc] init]; //set up a contact's avatarContact.imagedata = Uiimagepngrepresentation ([UIImage imagenamed:@"Animal"]); //set up a contact person's nameContact.givenname =@"Rain Road"; //set Last nameContact.familyname =@"gan"; //set up a contact mailboxCnlabeledvalue *homeemail = [Cnlabeledvalue labeledvaluewithlabel:cnlabelhome value:@"12345qq.com"]; Cnlabeledvalue*workemail = [Cnlabeledvalue labeledvaluewithlabel:cnlabelwork value:@"[email protected]"]; Cnlabeledvalue*otheremail = [Cnlabeledvalue labeledvaluewithlabel:cnlabelother value:@"lanaj.com"]; Contact.emailaddresses=@[homeemail,workemail,otheremail]; //set the organization nameContact.organizationname =@"Internet"; //set up a departmentContact.departmentname =@"Logistics"; //set the name of the jobContact.jobtitle =@"iOS"; //a brief introduction to social settingCnsocialprofile *profile = [[Cnsocialprofile alloc] initwithurlstring:@"12306.cn"Username@"Lily"Useridentifier:nil Service:@"IT Industry"]; Cnlabeledvalue*socialprofile =[Cnlabeledvalue labeledvaluewithlabel:cnsocialprofileservicegamecenter value:profile]; Contact.socialprofiles=@[socialprofile]; //Set Phone numberCnphonenumber *mobilenumber = [[Cnphonenumber alloc] Initwithstringvalue:@"15201596724"]; Cnlabeledvalue*mobilephone =[[Cnlabeledvalue alloc] Initwithlabel:cnlabelphonenumbermobile Value:mobilenumber]; Contact.phonenumbers=@[mobilephone]; //set up a relationship with a contactCncontactrelation *friend = [[Cncontactrelation alloc] Initwithname:@"good friend"]; Cnlabeledvalue*relation =[Cnlabeledvalue labeledvaluewithlabel:cnlabelcontactrelationfriend value:friend]; Contact.contactrelations=@[relation]; //Set BirthdayNsdatecomponents *birthday =[[Nsdatecomponents alloc] init]; Birthday.day=6; Birthday.month=5; Birthday.year= -; Contact.birthday=birthday; returnContact ;}/** * Show contact person*/- (void) Showcontact: (UIButton *) sender{//Initialize CncontactpickerviewcontrollerCncontactpickerviewcontroller *contactpickerviewcontroller =[[Cncontactpickerviewcontroller alloc] init]; //Set up proxyContactpickerviewcontroller.Delegate=Self ; //Show Contact window view[self Presentviewcontroller:contactpickerviewcontroller animated:yes completion:nil];}#pragmaMark--cncontactpickerdelegate agent--/** * Click the Cancel button of the contact controller to execute the method * * @param picker Contact Controller*/- (void) Contactpickerdidcancel: (Cncontactpickerviewcontroller *) picker{NSLog (@"Cancel");}/** * This method is executed when the contact is selected * * @param picker Contact Controller * @param contact person*/- (void) Contactpicker: (Cncontactpickerviewcontroller *) Picker didselectcontact: (Cncontact *) contact{NSLog (@"Contact information:%@", contact); [Self dismissviewcontrolleranimated:yes completion:nil]; //Show contact Details pageCncontactviewcontroller *CONTACTVC =[Cncontactviewcontroller viewcontrollerforcontact:contact]; [Self PRESENTVIEWCONTROLLER:CONTACTVC animated:yes completion:nil];}@end
Cncontact Basic use of contacts (add and revise)