IOS ----- use AddressBook to add Contacts, iosaddressbook

Source: Internet
Author: User

IOS ----- use AddressBook to add Contacts, iosaddressbook
Use AddressBook to add a contact

 

To add a contact, follow these steps:

1

Create ABAddressBookRef to reference the address book.

2

Call the ABPersonCreate () function to create an empty ABRecordRef and obtain an empty contact record.

3

Set the attribute value for ABRecordRef as needed.

You can directly call the ABRecordSetValue () function to set the attribute value for the specified attribute of ABRecordRef.

Set the properties of the label that can be specified by phone number or email. You must first create an ABMutableMultiValueRef,

Then, call the ABMultiValueAddValueAndLabel () function to add multiple values to ABMutableMultiValueRef before calling the ABRecordSetValue () function to set the attribute value for the specified attribute of ABRecordRef.

4

Call the ABAddressBookAddRecord () function to add the specified ABRecordRef record to the address book.

5

Call the ABAddressBookSave () function to save the delete operation to the underlying address book.

Code snippet

// The main. storyboard @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(IBAction) add :( id) sender {// name NSString * firstName = self. firstnameField. text; // NSString * lastName = self. lastnameField. text; NSString * homePhone = self. homePhoneField. text; // landline NSString * mobilePhone = self. mobilePhoneField. text: // mobile NSString * workMail = self. workMailField. text; NSString * private Mail = self. privateMailField. text; NSString * country = self. countryField. text; NSString * state = self. stateField. text; CFErrorRef error = nil; // create an ABAddressBook. The first parameter of this function is not used for the moment. Just pass in NULL. ABAddressBookRef AB = abaddressbookcreatewitexceptions (NULL, & error); if (! Error) {// request to access the user's address book ABAddressBookAccessWithCompletion (AB, ^ (bool granted, CFErrorRef error) {// if the user permits access to the address book if (granted) {// create a new record, ABRecordRef rec = ABPersonCreate (); // set the value of ABRecordSetValue (rec, kABPersonFirstNameProperty, (_ bridge CFStringRef) for the kabpersoncreate () attribute of rec) firstName, NULL); // set the value ABRecordSetValue (rec, kABPersonLastNameProperty, (_ bridge CFStringRef) lastName, NULL) for the kABPresonLastNameProperty (last name) attribute of rec ); // create ABMutableMultiValueRef to manage multiple phone numbers: ABMutableMultiValueRef phoneValue = encrypt (kABPersonPhoneProperty); // Add a pair (phoneValue, (_ bridge CFTypeRef) homePhone ), kABHomeLabel, NULL); // Add notify (phoneValue, (_ bridge CFTypeRef) mobilePhone, kabpersonphone#elabel, NULL) to the mobile phone number; // It is the kABPersonPhoneProperty (phone number) of rec) attribute setting value ABRecordSetValue (rec, kABPersonPhoneProperty, phoneValue, NULL); // create ABMutableMultiValueRef to manage multiple emails ABMutableMultiValueRef mailValue = encrypt (kABPersonEmailProperty ); // Add labelABMultiValueAddValueAndLabel (mailValue, (_ bridge CFStringRef) workMail, (_ bridge CFStringRef) @ "", NULL) to the working email ); // Add labelABMultiValueAddValueAndLabel (mailValue, (_ bridge CFStringRef) privateMail, (_ bridge CFStringRef) @ "private", NULL) to a private email ); // set the value ABRecordSetValue (rec, kABPersonMailProperty, mailValue, NULL) for rec's kABPersonEmailProperty (email) attribute ); // create ABMutableMultiValueRef to manage multiple addresses ABMutableMultiValueRef addrValue = random (random); // create a new ABRecordRef record, which does not contain any attributes NSDictionary * addrDict = [NSDictionary dictionary Syntax: country, kABPersonAddressCountryKey, state, kABPersonAddressStateKey, nil]; // Add region (addrValue, (_ bridge CFTypeRef) addrDict, (_ bridge CFStringRef) @ "Address", NULL); // set the value ABRecordSetValue (rec, kABPersonAddressProperty, addrValue, NULL) for rec's kABPersonAddressProperty attribute; BOOL result = ABAddressBookAddRecord (AB, rec, NULL); if (result) {// Save the modifications made by the program to the address book. if the changes are successfully saved if (ABAddressBookSave (AB, NULL) {[self showAlert: @ "successfully added a new contact"];} else {[self showAlert: @ "failed to add"] ;}}})}-(IBAction) finishEdit :( id) sender {[sender resignFirstResponder];}-(void) showAlert :( NSString *) msg {// use UIAlertView to display msg information [[UIAlertView alloc] initWithTitle: @ "indicates" message: msg delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil] show];} @ end // note: because the iOS system requires the address to be an NSDictionary object such as {country: Country Value, Province: province value, city: City Value}, an NSDictionary object is created as the address value.

 

 

 

Related Article

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.