IOS ----- use addressBook to manage the contact to modify the contact, iosaddressbook

Source: Internet
Author: User

IOS ----- use addressBook to manage the contact to modify the contact, iosaddressbook
Use addressBook to manage contacts and modify contacts

Modify a contact first loads an ABRecordRef record from the underlying address book, and then modifies the attribute value of this ABRecordRef record. After modification, save the modified ABRecordRef record to the address book.

Follow these steps to modify a contact:

1

Obtains the ABRecordRef record of existing contacts in the underlying address book.

2

Call the ABRecordSetValue () function to modify the attribute value specified in the ABRecordRef Record Based on the attribute to be modified.

3

After the modification, call the ABAddressBookSave () function to save the modification to the underlying address book.

 

Generation

Code

Slice

Segment

# Import <AddressBook/AddressBook. h> # import "LCUpdatePersonViewController. h "# define PHONE_PROPERTY_KEY @" phone "# define MAIL_PROPERTY_KEY @" mail "@ interface LCUpdatePersonViewController () {ABAddressBookRef AB; // define a variable of the ABRecordRef type to save the currently updated record of the ABRecordRef rec; // use this variable to define the Y coordinate NSInteger curLineY of the currently dynamically added row; // define an NSMUtableDictionary to save all dynamically added text boxes NSMUtableDictionary * textFields; // define the ABMutableMultiValueRef variable to record the phone number attribute value being modified ABMutableMultiValueRef phoneValue; // define the ABMutableMultiValueRef variable to record the email attribute value being modified (ABMutableMultiValueRef mailValue;} @ end @ implementation values-(void) viewDidLoad {[super viewDidLoad]; textFields = [NSMutableDictionary dictionary]; curLineY = 120; CFErrorRef error = nil; // create an ABAddressBook. The first parameter of this function is not used currently. If NULL is passed in, AB = ABAddressBookRequestAccessWithCompletion (AB, ^ (bool granted, CFErrorRef error) {// if the user is allowed to access the address book if (granted) {// obtain the ABRecordRef record rec = ABAddressBookGetPersonWithRecordID (AB, 1) with ID 1 ); // obtain the value of the kABPersonFirstNameProperty (name) attribute in the rec record NSString * firstName = (_ bridge NSString *) ABRecordCopyValue (rec, kABPersonFirstNameProperty ); // obtain the property value NSString * lastName = (_ bridge NSString *) ABRecordCopyValue (rec, kABPersonLastNameProperty) in the rec record ); // The control interface displays the property value self of the last name and name in the rec record in the text box. firstNameField. text = firstName; self. lastNameField. text = lastName; // obtain the kABPersonPhoneProperty (phone number) attribute value phoneValue = ABRecordCopyValue (rec, kABPersonPhoneProperty) in the rec record; // call addLabelAndTextField: propertyKey: The method displays the phone number: phoneValue propertyKey: PHONE_PROPERTY_KEY]; // obtain the attribute value mailValue = ABRecordCopyValue (rec, kABPersonEmailProperty) in the rec record. // call addLabelAndTextField: propertyKey: method to display the email [self addLabelAndTextField: mailValue propertyKey: MAIL_PROPERTY_KEY] ;}}}- (void) addLabelAndTextField :( ABMutableMultiValueRef) multiValue propertyKey :( NSString *) property {// obtain the number of data entries contained in multiValue NSInteger num = ABMultiValueGetCount (multiValue); NSMutableArray * textFieldArray = [NSMutableArray array]; // traverse each piece of data contained in multiValue in sequence for (int I = 0; I <num; I ++) {curLineY + = 38; // create UILabel, label UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (20, curLineY, 70, 30)]; NSString * labelStr = (_ bridge NSString *) ABAddressBookCopyLocalizedLabel (ABMultiValueCopyLabelAtIndex (multiValue, I); label. text = labelStr; // Add the UILabel to the program interface [self. view Details: @ selector (addSubview :) withObject: label waitUntilDone: YES]; // create UITextField to display the value UITextField * textField = [UITextField alloc] initWithFrame: CGRectMake (98, curline, 202, 30)]; textField. borderStyle = UITextBorderStyleRoundedRect; NSString * valueStr = (_ bridge NSString *) ABMultiValueCopyValueAtIndex (multiValue, I); textField. text = valueStr; [textField addTarget: self action: @ selector (finishEdit :) forControlEvents: Inputs]; // use the NSArray set to save the dynamically created UITextField control [textFieldArray addObject: textField]; // Add UITextField to the program interface [self. view multiple mselecdomainmainthread: @ selector (addSubview :) withObject: textField waitUntilDone: YES];} // save all textFields dynamically generated by the program to NSMutableDictionary [textFields setValue: textFieldArray forKey: property];}-(void) updateMultiValue: (ABMutableMultiValueRef) multiValue propertyKey :( NSString *) propertyKey property :( ABPropertyID) property {// retrieve the NSArray * textFieldArray = textFields [propertyKey] composed of all uitextviews corresponding to this property; NSInteger num = textFieldArray. count; // create a new ABMutableMultiValueRef newMulti = ABMultiValueCreateMutable (property); // traverse each UITextField Control for (int I = 0; I <num; I ++) {// obtain the string in the UITextField control I, which is used as the new value NSString * value = (UITextField *) textFieldArray [I]). text; // obtain the original label CFStringRef label = ABMultiValueCopyLabelAtIndex (multiValue, 0) of the data entry I; // Add a new value and the original label (the label does not need to be modified) ABMultiValueAddValueAndLabel (newMutli, (_ bridgeCFStringRef) value, label, NULL);} ABRecordSetValue (rec, property, newMutli, NULL);}-(IBAction) update :( id) sender {NSString * firstName = self. firstnameField. text; NSString * lastName = self. lastnameField. text; // modify the attributes of kABPersonFirstNameProperty and kABPersonLastNameProperty in the rec record. ABRecordSetValue (rec, kABPersonFirstNameProperty, (_ bridge CFStringRef) firstName, NULL); ABRecordSetValue, (_ bridge CFStringRef) lastName, NULL); // call updateMultiValue: propertyKey: property: Method to modify the attributes of ABRecordRef // kABPersonPhoneProperty [self updateMultiValue: phoneValue propertyKey: custom property: callback]; // call updateMultiValue: propertyKey: property: Method to modify the attributes of ABRecordRef // kABPersonEmailProperty [self updateMultiValue: mailValue propertyKey: Primary property: kABPersonEmailProperty]; if (ABAddressBookSave, NULL) {[self showAlert: @ "modified successfully"];} else {[self showAlert: @ "An error occurred while modifying"] ;}}-(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:

To enable the interface to dynamically display each piece of data contained in ABMutableMultiValueRef, the program defines addLabelAndTextField: propertyKey: Method to dynamically load each piece of data in ABMutableMultiValueRef-the program adds a row for each piece of data, use UILabel to display the label of the data, and use UITextField to display the value of the data.

 

:

 

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.