How do you add contacts to the iPhone Address book

2019-01-24 18:54发布

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?

2条回答
Evening l夕情丶
2楼-- · 2019-01-24 19:32

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();
查看更多
孤傲高冷的网名
3楼-- · 2019-01-24 19:37

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

查看更多
登录 后发表回答