// Called when a phone number is swiped for deletion. Illustrates how to delete a multivalue property protected void DeletePhoneNumber(int phoneNumberID) { using (ABAddressBook addressBook = new ABAddressBook()) { ABPerson contact = addressBook.GetPerson(contactID); // get the phones and copy them to a mutable set of multivalues (so we can edit) ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue(); // loop backwards and delete the phone number for (int i = phones.Count - 1; i >= 0; i--) { if (phones [i].Identifier == phoneNumberID) { phones.RemoveAt(i); } } // attach the phones back to the contact contact.SetPhones(phones); // save the changes addressBook.Save(); // show an alert, letting the user know the number deletion was successful new UIAlertView("Alert", "Phone Number Deleted", null, "OK", null).Show(); // repopulate the page PopulatePage(contact); } }
protected void BtnAddPhoneNumberTouchUpInside(object sender, EventArgs e) { // get a reference to the contact using (ABAddressBook addressBook = new ABAddressBook()) { ABPerson contact = addressBook.GetPerson(contactID); // get the phones and copy them to a mutable set of multivalues (so we can edit) ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue(); // add the phone number to the phones via the multivalue.Add method phones.Add(new NSString(txtPhoneLabel.Text), new NSString(txtPhoneNumber.Text)); // attach the phones back to the contact contact.SetPhones(phones); // save the address book changes addressBook.Save(); // show an alert, letting the user know the number addition was successful new UIAlertView("Alert", "Phone Number Added", null, "OK", null).Show(); // update the page PopulatePage(contact); // we have to call reload to refresh the table because the action didn't originate // from the table. tblPhoneNumbers.ReloadData(); } }
protected void BtnSaveChangesTouchUpInside(object sender, EventArgs e) { using (ABAddressBook addressBook = new ABAddressBook()) { ABPerson contact = addressBook.GetPerson(contactID); if (contact != null) { // save contact name information contact.FirstName = txtFirstName.Text; contact.LastName = txtLastName.Text; // get the phones and copy them to a mutable set of multivalues (so we can edit) ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue(); // remove all phones data // HACK: Cast nint to int for (int i = (int)phones.Count - 1; i >= 0; i--) { phones.RemoveAt(i); } // add the phone number to the phones from the table data source for (int i = 0; i < PhoneNumberTableSource.labels.Count; i++) { phones.Add(new NSString(PhoneNumberTableSource.numbers [i]), new NSString(PhoneNumberTableSource.labels [i])); } // attach the phones back to the contact contact.SetPhones(phones); // save the address book changes addressBook.Save(); // show an alert, letting the user know information saved successfully new UIAlertView("Alert", "Contact Information Saved!", null, "OK", null).Show(); // update the page PopulatePage(contact); // we have to call reload to refresh the table because the action didn't originate // from the table. tblPhoneNumbers.ReloadData(); } else { new UIAlertView("Alert", "Please select a contact using the top right button", null, "OK", null).Show(); } } }
partial void AddElba(Id sender) { NSMutableDictionary homeAddr, workAddr; ABMutableMultiValue multiValue; // Get the address book - there is only one. ABAddressBook ab = ABAddressBook.SharedAddressBook; // Create a record. ABPerson person = new ABPerson(); // Set value in record for first name property. person.SetValueForProperty((NSString) "Able", AddressBookFramework.kABFirstNameProperty); // Set value in record for last name property. person.SetValueForProperty((NSString) "Elba", AddressBookFramework.kABLastNameProperty); // kABAddressProperty is a multiValue. // It's values, such as kABAddressHomeLabel, have in turn keys, // such as kABAddressStreetKey. // Create and populate a NSDictionary with some kABAddressHomeLabel keys. homeAddr = new NSMutableDictionary(); homeAddr[AddressBookFramework.kABAddressStreetKey] = (NSString) "123 Home Dr."; homeAddr[AddressBookFramework.kABAddressCityKey] = (NSString) "Home City"; homeAddr[AddressBookFramework.kABAddressStateKey] = (NSString) "CA"; homeAddr[AddressBookFramework.kABAddressZIPKey] = (NSString) "94110"; homeAddr[AddressBookFramework.kABAddressCountryKey] = (NSString) "United States"; // Create and populate a NSDictionary with some kABAddressWorkLabel keys. workAddr = new NSMutableDictionary(); workAddr[AddressBookFramework.kABAddressStreetKey] = (NSString) "123 Home Dr."; workAddr[AddressBookFramework.kABAddressCityKey] = (NSString) "Home City"; workAddr[AddressBookFramework.kABAddressStateKey] = (NSString) "CA"; workAddr[AddressBookFramework.kABAddressZIPKey] = (NSString) "94110"; workAddr[AddressBookFramework.kABAddressCountryKey] = (NSString) "United States"; // Create an ABMultivalue and add the kABAddressHomeLabel and // kABAddressWorkLabel NSDictionaries multiValue = new ABMutableMultiValue(); multiValue.AddValueWithLabel(homeAddr, AddressBookFramework.kABAddressHomeLabel); multiValue.AddValueWithLabel(workAddr, AddressBookFramework.kABAddressWorkLabel); // Set value in record for kABAddressProperty. person.SetValueForProperty(multiValue, AddressBookFramework.kABAddressProperty); multiValue.Release(); // kABPhoneProperty is a multivalue. // Create and populate a multiValue. multiValue = new ABMutableMultiValue(); multiValue.AddValueWithLabel((NSString) "408-974-0000", AddressBookFramework.kABPhoneWorkLabel); multiValue.AddValueWithLabel((NSString) "408-974-1111", AddressBookFramework.kABPhoneHomeLabel); multiValue.AddValueWithLabel((NSString) "408-974-2222", AddressBookFramework.kABPhoneMobileLabel); multiValue.AddValueWithLabel((NSString) "408-974-3333", AddressBookFramework.kABPhoneMainLabel); multiValue.AddValueWithLabel((NSString) "408-974-4444", AddressBookFramework.kABPhoneHomeFAXLabel); multiValue.AddValueWithLabel((NSString) "408-974-5555", AddressBookFramework.kABPhoneWorkFAXLabel); multiValue.AddValueWithLabel((NSString) "408-974-6666", AddressBookFramework.kABPhonePagerLabel); // Set value in record for kABPhoneProperty. person.SetValueForProperty(multiValue, AddressBookFramework.kABPhoneProperty); multiValue.Release(); // Add record to the Address Book if (ab.AddRecord(person)) { // Save the Address Book if (ab.Save()) { Console.WriteLine("Success"); } } person.Release(); }