Hi,
I'm trying to write contact information into their respective address books. I've seen examples on reading contacts (still haven't managed to get it to work, none of the examples compile, have to spend hours resolving bugs to get it to work!) but is there anything out there to write to contacts?
From the AddressbookSample (https://github.com/softsan/AddressbookSample) , Xamarin.Contacts does not work. Xamarin.Mobile does not compile.
Is there any way?
@DeviOS
Firts of all you will have to create an interface and make use of dependency service to do this create an interface with a method.
Nd implement this code in native Android and IOS project
For Android:
public void SaveContacts(string Name, string Number) { var activity = Forms.Context as Activity; var intent = new Intent(Intent.ActionInsert); intent.SetType(ContactsContract.Contacts.ContentType); intent.PutExtra(ContactsContract.Intents.Insert.Name, Name); intent.PutExtra(ContactsContract.Intents.Insert.Phone, Number); activity.StartActivity(intent); Toast.MakeText(activity, "ContactSaved", ToastLength.Short).Show(); }
For IOS:
public void SaveContacts(string Name, string Number) { var store = new CNContactStore(); var contact = new CNMutableContact(); var cellPhone = new CNLabeledValue<CNPhoneNumber>(CNLabelPhoneNumberKey.Mobile, new CNPhoneNumber(Number)); var phoneNumber = new[] { cellPhone }; contact.PhoneNumbers = phoneNumber; contact.GivenName = Name; var saveRequest = new CNSaveRequest(); saveRequest.AddContact(contact, store.DefaultContainerIdentifier); }
Answers
@JamesMontemagno has a pluggin, not sure it's supported but may be you started down the right path. I think it only reads the address book but might give you some ideas on how to get to the API's to write back.
https://github.com/jamesmontemagno/ContactsPlugin
https://www.nuget.org/packages/Xam.Plugin.Contacts
Remember to enable read & write contacts in your Android manifest.
@DeviOS For which platform do you want it??
@NMackay Thank you for your suggestions. I installed Plugin.Contacts and was able to read the contacts. Still working out on how to write to the addressbook..
@VinayakGawas I want it for Android and iOS. Thank you.
@DeviOS
Firts of all you will have to create an interface and make use of dependency service to do this create an interface with a method.
Nd implement this code in native Android and IOS project
For Android:
For IOS:
Thank you @VinayakGawas! You saved my day(s). Its working like it should! Thank you very much again.
@VinayakGawas I tried a variant of this code to save a contact with multiple phone numbers and e-mail addresses, but my app crashes on iOS when trying to run this code. (Android works fine though, thanks for that!)
Here's my method (phones and emails are both lists of strings containing the phone numbers and e-mail addresses):
@TVH I'd suggest you step through the loops and see what's causing the crash. I'm not using a loop coz I save each contact once. But incase it helps, here's my complete iOS code.
Thanks @DeviOS. I think my app crashed previously because it didn't check permissions, so I added your code to do so. However, now the method still isn't working. Sometimes it goes through the method, but the contact isn't saved, sometimes the app crashes when asking for permission.
For reference, here's my full class:
Hey @TVH , It was working fine for me without crashing, until one day. It started throwing a SIGABRT error when asking for permission. Same issue even on Debug and Release builds. However, it doesn't crash using a release build on a Device, only crashes in the simulator. Funny.
hi...
now i am creating simple application in xamrin forms
i have one doubt for me....please help me
my task is , read contact details from phone to my app
i have completed Read contact name and id
but contact number cant able
Hi,
I solved the SIGABRT issue, by adding "Privacy - Contacts usage description" entry in info.plist.
now the code works till saveRequest.AddContact.
I've no error, but the app do nothing, any contact or gui to add it.
On Android it works fine.
Any help?
Hi,
I solved my issue, by adding this code after saveRequest.AddContact
NSError error;
if (store.ExecuteSaveRequest(saveRequest, out error))
{
Console.WriteLine("New contact saved");
}
else
{
Console.WriteLine("Save error: {0}", error);
}
@VinayakGawas
Do you have an example (for android and iOS) how to read a contact with the same approach?
Regards
rubenc
Hello @DeviOS and @VinayakGawas
I have to implement add contact in Address book. so please help me which plugin I have to implement and how. As I am new in xamarin forms.
Thanks you
@Gandhi_123
Could you share the basic demo of adding contact in Address book?
Hello @Tracy320
As of now I have implement read contact code which is given below:
private async void Button_Clicked(object sender, EventArgs e)
{
switch (Device.RuntimePlatform)
{
But now I want to implement Write Contact code. and I don't understand how to start. Please help me.
Thank You
Hello @DeviOS , @VinayakGawas
Thanks for the best solution but I want to know that how to come back in our xamarin forms app after saving contact.
Hello @VinayakGawas , @DeviOS , @TVH
This is the perfect solution for save contact in phone book. I have applied in android and its work perfectly but when i have applied in ios the contcat is not saved . So please help me for come out with this problem.
Thanks in advance!!!!!!
I m try to do this play but it don´t identify Forms.Context, Intent and ContactsContract. Has assembly or using for add? Thanks
@VinayakGawas , @NMackay
Hello my friends, I come to write here on the article because I am looking for a solution to my problem. My problem is that I want to save a contact with the save what method, but first I would like to check if the contact I want to save exists. If anyone has a great idea or link solution to adapt. I think it will make me happy and help me tremendously.
Thank you