How do you add contacts to the iPhone Address book

2019-01-24 19:22发布

问题:

I need to be able to access the address book in monotouch and add/modify/delete from there within my project. How would I do that?

回答1:

Use ABAddressBook. Here's an example of adding a new contact with a name, address and phone

        ABAddressBook ab = new ABAddressBook();
        ABPerson p = new ABPerson();

        p.FirstName = fname;
        p.LastName = lname;

        ABMutableMultiValue<string> phones = new ABMutableStringMultiValue();
        phones.Add(phone, ABPersonPhoneLabel.Mobile);

        p.SetPhones(phones);

        ABMutableDictionaryMultiValue addresses = new ABMutableDictionaryMultiValue();
        NSMutableDictionary a = new NSMutableDictionary();

        a.Add(new NSString(ABPersonAddressKey.City), new NSString(city));
        a.Add(new NSString(ABPersonAddressKey.State), new NSString(state));
        a.Add(new NSString(ABPersonAddressKey.Zip), new NSString(zip));
        a.Add(new NSString(ABPersonAddressKey.Street), new NSString(addr1));

        addresses.Add(a, new NSString("Home"));
        p.SetAddresses(addresses);

        ab.Add(p);
        ab.Save();


回答2:

For anyone interested, I have created a sample project for inputing contacts in the address book with random phone numbers. Hope this helps other people.

https://github.com/ayoung/monotouch-generate-contacts