Use Cncontact to change Address Book additions and deletions in iOS development
First of all, of course, the cncontact is included in the project:
1 @import Contacts;
1, the following is the increase of the Contact person Program section:
1Cnmutablecontact * Contact =[[Cnmutablecontact alloc]init];2Contact.imagedata = Uiimagepngrepresentation ([UIImage imagenamed:@" A"]);3 //Set name4Contact.givenname =@"three Strong";5 //set Last name6Contact.familyname =@"Money";7Cnlabeledvalue *homeemail = [Cnlabeledvalue labeledvaluewithlabel:cnlabelhome value:@"[email protected]"];8Cnlabeledvalue *workemail =[cnlabeledvalue labeledvaluewithlabel:cnlabelwork value:@"[email protected]"];9Contact.emailaddresses =@[homeemail,workemail];Ten //Family OneContacts_extern NSString *Constcnlabelhome ns_available (10_11, 9_0); A //work -Contacts_extern NSString *Constcnlabelwork ns_available (10_11, 9_0); - //other theContacts_extern NSString *Constcnlabelother ns_available (10_11, 9_0); - - //e-mail address -Contacts_extern NSString *ConstCnlabelemailicloud ns_available (10_11, 9_0); + - //URL address +Contacts_extern NSString *Constcnlabelurladdresshomepage ns_available (10_11, 9_0); A at //Date -Contacts_extern NSString *Constcnlabeldateanniversary ns_available (10_11, 9_0); -Contact.phonenumbers = @[[cnlabeledvalue Labeledvaluewithlabel:cnlabelphonenumberiphone value:[CNPhoneNumber Phonenumberwithstringvalue:@"12344312321"]]]; -cnmutablepostaladdress * homeadress =[[Cnmutablepostaladdress alloc]init]; -Homeadress.street =@"Baker Street"; -Homeadress.city =@"London"; inHomeadress.state =@"United Kingdom"; -Homeadress.postalcode =@"221B"; toContact.postaladdresses =@[[cnlabeledvalue Labeledvaluewithlabel:cnlabelhome value:homeadress]]; +Nsdatecomponents * Birthday =[[Nsdatecomponents alloc]init]; -birthday.day=7; theBirthday.month=5; *Birthday.year=1992; $contact.birthday=birthday;Panax Notoginseng - the + A // //Initialize Method theCnsaverequest * Saverequest =[[Cnsaverequest alloc]init]; + //Add a contact (yes) - [Saverequest addcontact:contact tocontainerwithidentifier:nil]; $ //Write $Cncontactstore * store =[[Cncontactstore alloc]init]; -[Store Executesaverequest:saverequest Error:nil];
2, the following is the removal of the Contact Program section, in the Apple development document is described as follows:
The contact parameter is the one you want to delete, which means the contact is there before the contacts, that is, it's not a new contact object we've created, but a contact object that we've identified from the Address book, so we're going to find the contact. In this case, first create a predicate, then go to find, find and then perform the deletion, the following is the program segment:
1 //Cncontactstore * store = [[Cncontactstore alloc]init];2 // //search criteria, retrieve all names in the name of Givenname is W's contact3 //nspredicate * predicate = [cncontact predicateforcontactsmatchingname:@ "W"];4 // //Extracting Data5 //Nsarray * contacts = [store unifiedcontactsmatchingpredicate:predicate Keystofetch:@[cncontactgivennamekey] Error : nil];6 //cnmutablecontact *contact1 = [Contacts objectatindex:0];7 // 8 // 9 //////Initialize MethodTen //cnsaverequest * saverequest = [[Cnsaverequest alloc]init]; One // A // //Delete a contact (NO) - //[Saverequest Deletecontact:contact1];
3, about updating contacts, temporary problems.
Use Cncontact to change Address Book additions and deletions in iOS development