locked
How to export information to a contact, xamarin.forms RRS feed

  • Question

  • User395962 posted

    I have a bunch of info and entries, and I want to create a button with a function that exports all that info into a contact and save it.

    Thursday, August 20, 2020 8:17 AM

Answers

  • User382871 posted

    I want to create a button with a function that exports all that info into a contact and save it. For this feature, try to achieve it on each native platform and use DependencyService to invoke the code in Xamarin.Forms.

    Check the code:

    1.Create an interface in the shared project. public interface ISaveToContacts { void SaveContacts(string Name, string Number); } 2.Implement the interface in the required platform projects. [assembly : Dependency(typeof(SaveToContactsClass))] namespace App19F_9.Droid { public class SaveToContactsClass : ISaveToContacts { 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(); } } } 3.Consume the function method in the Xamarin.Forms. DependencyService.Get<ISaveToContacts>().SaveContacts(name_string, number_string);

    For the implementation on ios, you could refer to the following code. 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); }

    Refer to: https://forums.xamarin.com/discussion/89597/write-to-contacts-on-xamarin-forms

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, August 20, 2020 10:51 AM

All replies

  • User382871 posted

    I want to create a button with a function that exports all that info into a contact and save it. For this feature, try to achieve it on each native platform and use DependencyService to invoke the code in Xamarin.Forms.

    Check the code:

    1.Create an interface in the shared project. public interface ISaveToContacts { void SaveContacts(string Name, string Number); } 2.Implement the interface in the required platform projects. [assembly : Dependency(typeof(SaveToContactsClass))] namespace App19F_9.Droid { public class SaveToContactsClass : ISaveToContacts { 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(); } } } 3.Consume the function method in the Xamarin.Forms. DependencyService.Get<ISaveToContacts>().SaveContacts(name_string, number_string);

    For the implementation on ios, you could refer to the following code. 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); }

    Refer to: https://forums.xamarin.com/discussion/89597/write-to-contacts-on-xamarin-forms

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, August 20, 2020 10:51 AM
  • User395962 posted

    @YelinZh thank you so much for the reply. you helped me loads. I created the interface just like you said, and added a string for the email. I then added the service for each of the platforms Android:

    [Obsolete] public void SaveContacts(string fullName, string phone, string email) { var activity = Forms.Context as Activity; var intent = new Intent(Intent.ActionInsert); intent.SetType(ContactsContract.Contacts.ContentType); intent.PutExtra(ContactsContract.Intents.Insert.Name, fullName); intent.PutExtra(ContactsContract.Intents.Insert.Email, email); intent.PutExtra(ContactsContract.Intents.Insert.Phone, phone); activity.StartActivity(intent); //Toast.MakeText(activity, "ContactSaved", ToastLength.Short).Show(); }

    IOS: [Obsolete] public void SaveContacts(string fullName, string phone, string email) { var store = new CNContactStore(); var contact = new CNMutableContact();

            //getting the phone number and converting to an array to add it to the arrays of numbers in contact
    
            var _phone = new[] { new CNLabeledValue<CNPhoneNumber>(CNLabelPhoneNumberKey.Mobile, new CNPhoneNumber(phone)) };
    
            //getting the email and converting to an array to add it to the arrays of numbers in contact
            var _email = new[] { new CNLabeledValue<NSString>("", new NSString(email)) };
    
            contact.PhoneNumbers = _phone;
            contact.GivenName = fullName;
            contact.EmailAddresses = _email;
    
            var saveRequest = new CNSaveRequest();
            saveRequest.AddContact(contact, store.DefaultContainerIdentifier);
        }
    

    at last, I added a button to export the contact:

    private void exportButtonClicked(object sender, EventArgs e) { DependencyService.Get().SaveContacts($"{userDetail.user.name} {_userDetail.user.lastName}", _userDetail.user.phone, _userDetail.user.email); }

    Tuesday, August 25, 2020 8:22 AM