IOS-----add Contacts using AddressBook

Source: Internet
Author: User
<span id="Label3"></p><span style="color: #ff0000; background-color: #ffff99;"><span style="color: #ff0000; background-color: #ffff99;">Add a contact using AddressBook</span></span><p><p></p></p> <table style="width: 97%;" border="1" cellspacing="0" cellpadding="0"> <tbody> <tr> <td colspan="2" valign="top" width="100%"><span style="color: #ff0000; background-color: #ffff99;">the steps to add a contact are as follows</span> :</td> </tr> <tr> <td valign="top" width="10%"><p align="center">1</p></td> <td valign="top" width="89%"><p>Create a abaddressbookref, which gets a reference to the address Book.</p></td> </tr> <tr> <td valign="top" width="10%"><p align="center">2</p></td> <td valign="top" width="89%"><p>Call the Abpersoncreate () function to create an empty abrecordref to get an empty contact record</p></td> </tr> <tr> <td valign="top" width="10%"><p align="center">3</p></td> <td valign="top" width="89%"><p>Set the property value for Abrecordref as Needed.</p><p>To set only simple information such as last name, first name, and so on, call the Abrecordsetvalue () function directly to set the property value for the specified property of abrecordref.</p><p>Set a phone number, e-mail, and so on you can specify a label property, you need to first create a abmutablemultivalueref,</p><p>Then call the Abmultivalueaddvalueandlabel () function to add multiple values to the abmutablemultivalueref, and finally call Abrecordsetvalue () The function sets the property value for the specified property of abrecordref.</p></td> </tr> <tr> <td valign="top" width="10%"><p align="center">4</p></td> <td valign="top" width="89%"><p>Call the Abaddressbookaddrecord () function to add the specified Abrecordref record to the address Book.</p></td> </tr> <tr> <td valign="top" width="10%"><p align="center">5</p></td> <td valign="top" width="89%"><p>Call the Abaddressbooksave () function to save the delete operation to the underlying address Book.</p></td> </tr> <tr> <td valign="top" width="10%"><p align="center">Code Snippets</p></td> <td valign="top" width="89%"><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">it <span style="color: #008000;">is hereby stated that the main storyboard is Used.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload{[super viewdidload];}</span></span>-(ibaction) Add: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) sender{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">name</span></span><span style="color: #000000;"><span style="color: #000000;">NSString</span></span>*firstname =<span style="color: #000000;"><span style="color: #000000;">self.firstnameField.text; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Surname</span></span><span style="color: #000000;"><span style="color: #000000;">NSString</span></span>*lastname =<span style="color: #000000;"><span style="color: #000000;">self.lastnameField.text; NSString</span></span>*homephone = self.homePhoneField.text;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Landline</span></span><span style="color: #000000;"><span style="color: #000000;">NSString</span></span>* Mobilephone = Self.mobilePhoneField.text:<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Mobile Phone</span></span><span style="color: #000000;"><span style="color: #000000;">NSString</span></span>* WorkMail =<span style="color: #000000;"><span style="color: #000000;">self.workMailField.text; NSString</span></span>* Privatemail =<span style="color: #000000;">self <span style="color: #000000;">. privatemailfield.text; NSString</span></span>* Country =<span style="color: #000000;"><span style="color: #000000;">self.countryField.text; NSString</span></span>* State =<span style="color: #000000;">self <span style="color: #000000;">. statefield.text; Cferrorref Error</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create abaddressbook, The 1th parameter of the function is temporarily unused, passing in null directly.</span></span><span style="color: #000000;"><span style="color: #000000;">Abaddressbookref AB</span></span>= Abaddressbookcreatewithoptions (NULL, &<span style="color: #000000;"><span style="color: #000000;">error);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">Error) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">request access to user address Book</span></span><span style="color: #000000;"><span style="color: #000000;">abaddressbookaccesswithcompletion (ab,</span></span>^(<span style="color: #0000ff;"><span style="color: #0000ff;">BOOL</span></span><span style="color: #000000;"><span style="color: #000000;">granted, cferrorref Error) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the user allows access to the address book</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(granted) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">create a new record</span></span><span style="color: #000000;"><span style="color: #000000;">Abrecordref Rec</span></span>=<span style="color: #000000;"><span style="color: #000000;">abpersoncreate ();</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set values for Rec's kabpersonfirstnameproperty (first Name) Property</span></span><span style="color: #000000;"><span style="color: #000000;">abrecordsetvalue (rec, kabpersonfirstnameproperty, (__bridge cfstringref) firstName, NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set values for Rec's kabpresonlastnameproperty (last Name) Property</span></span><span style="color: #000000;"><span style="color: #000000;">abrecordsetvalue (rec, kabpersonlastnameproperty, (__bridge cfstringref) lastName, NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create Abmutablemultivalueref to manage multiple phone numbers</span></span><span style="color: #000000;"><span style="color: #000000;">abmutablemultivalueref Phonevalue</span></span>=<span style="color: #000000;"><span style="color: #000000;">abmultivaluecreatemutable (kabpersonphoneproperty);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add a label to your home phone number</span></span><span style="color: #000000;"><span style="color: #000000;">Abmultivalueaddvalueandlabel (phonevalue, (__bridge cftyperef) homephone), kabhomelabel, NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add a label to a mobile phone number</span></span><span style="color: #000000;"><span style="color: #000000;">Abmultivalueaddvalueandlabel (phonevalue, (__bridge cftyperef) mobilephone), kabpersonphonemobilelabel, NULL); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set values for Rec's kabpersonphoneproperty (phone) Property</span></span><span style="color: #000000;"><span style="color: #000000;">abrecordsetvalue (rec, kabpersonphoneproperty, phonevalue, NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create Abmutablemultivalueref to manage multiple e-mail messages</span></span><span style="color: #000000;"><span style="color: #000000;">abmutablemultivalueref Mailvalue</span></span>=<span style="color: #000000;"><span style="color: #000000;">abmultivaluecreatemutable (kabpersonemailproperty);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add a label to a working e-mail message</span></span><span style="color: #000000;"><span style="color: #000000;">Abmultivalueaddvalueandlabel (mailvalue, (__bridge cfstringref) workMail, (__bridge cfstringref) @ "work", NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add a label to a private e-mail message</span></span><span style="color: #000000;"><span style="color: #000000;">Abmultivalueaddvalueandlabel (mailvalue, (__bridge cfstringref) privatemail, (__bridge CFStringRef) @ "private", NULL); /c0></span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set values for Rec's Kabpersonemailproperty (e-mail) attribute</span></span><span style="color: #000000;"><span style="color: #000000;">abrecordsetvalue (rec, kabpersonmailproperty, mailvalue, NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create Abmutablemultivalueref to manage multiple addresses</span></span><span style="color: #000000;"><span style="color: #000000;">abmutablemultivalueref Addrvalue</span></span>=<span style="color: #000000;"><span style="color: #000000;">abmultivaluecreatemutable (kabpersonaddressproperty);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a new abrecordref record that does not contain any attributes</span></span><span style="color: #000000;"><span style="color: #000000;">nsdictionary</span></span>*addrdict =<span style="color: #000000;"><span style="color: #000000;">[nsdictionary dictionarywithobjectsandkeys:country, kabpersonaddresscountrykey, state, kabpersonaddressstatekey, nil];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add a label to your address</span></span><span style="color: #000000;"><span style="color: #000000;">Abmultivalueaddvalueandlabel (addrvalue, (__bridge cftyperef) addrdict, (__bridge cfstringref) @ "address", NULL);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the value for Rec's kabpersonaddressproperty (address) Property</span></span><span style="color: #000000;"><span style="color: #000000;">abrecordsetvalue (rec, kabpersonaddressproperty, addrvalue, NULL); BOOL result</span></span>=<span style="color: #000000;"><span style="color: #000000;">abaddressbookaddrecord (ab, rec, NULL);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(result) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">save changes made by the program to the Address book if it is saved successfully</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(abaddressbooksave (ab, NULL)) {[self showalert:@ "successfully added new contact"]; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{[self showalert:@ ' Add failed ']; } } } })}</span></span>-(ibaction) Finishedit: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) Sender{[sender resignfirstresponder];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Showalert: (nsstring *<span style="color: #000000;"><span style="color: #000000;">) msg{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">using Uialertview to display msg information</span></span><span style="color: #000000;">[[[ <span style="color: #000000;">uialertview alloc] initwithtitle:@ "hint" message:msg </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span><span style="color: #000000;"><span style="color: #000000;">: nil cancelbuttontitle:@ "ok" otherbuttontitles:nil] show];}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">//note: because iOS system requires address must be {country: country value, province: province value, city: City value} This nsdictionary object, so create a Nsdictionary object as the address value</span></span></pre><p><p></p></p></td> </tr> <tr> <td valign="top" width="10%"><p align="center"></p></td> <td valign="top" width="89%"><p></p></td> </tr> </tbody> </table><p><p>IOS-----add Contacts using AddressBook</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.