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. |