MAUI: Save contact to device

Sreejith Sreenivasan 896 Reputation points
2023-11-15T13:25:38.8733333+00:00

I am trying to save a phone number to the local device in MAUI by Conditional Compilation.

Below is my code:

#if ANDROID

using Android.App;
using Android.Content;
using Android.OS;
using Android.Provider;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using MyApp.Droid.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#elif IOS

using AddressBook;
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using UIKit;
using MyApp.iOS.Services;

#endif

namespace MyApp.Renderers
{
    public class SaveContact
    {
        public void SaveContactToPhoneBook(string Name, string Number, string Email)
        {
#if ANDROID
            var activity = Android.App.Application.Context as Activity;
            var intent = new Intent(Intent.ActionInsert).SetFlags(ActivityFlags.ClearTask);
            intent.SetType(ContactsContract.Contacts.ContentType);
            intent.PutExtra(ContactsContract.Intents.Insert.Name, Name);
            intent.PutExtra(ContactsContract.Intents.Insert.Phone, Number);
            intent.PutExtra(ContactsContract.Intents.Insert.Email, Email);
            activity.StartActivity(intent);
#elif IOS
            ABAddressBook ab = new ABAddressBook();
            ABPerson p = new ABPerson();
            p.FirstName = Name;
            ABMutableMultiValue<string> phones = new ABMutableStringMultiValue();
            phones.Add(Number, ABPersonPhoneLabel.Mobile);
            p.SetPhones(phones);
            ABMutableDictionaryMultiValue addresses = new ABMutableDictionaryMultiValue();
            NSMutableDictionary a = new NSMutableDictionary();
            addresses.Add(a, new NSString("Home"));
            p.SetAddresses(addresses);
            ab.Add(p);
            ab.Save();
#endif
        }
    }
}

Then from my contacts save page:

SaveContact saveContact = new SaveContact();
saveContact.SaveContactToPhoneBook("", phone, "");//passing name, phone and email

I am getting exception for this from both Android and iOS platforms.

Android Exception:

System.NullReferenceException: Object reference not set to an instance of an object. at MyProject.Renderers.SaveContact.SaveContactToPhoneBook(String Name, String Number, String Email) in E:\My Projects\MAUI\MyProject-maui\MyProject\Renderers\SaveContact.cs:line 43 [SurfaceComposerClient] XDR setRegion is not supported 0xb4000075d1bc0cf0, 1, 0

iOS Exception:

CoreFoundation.CFException: The operation couldn’t be completed. (ABAddressBookErrorDomain error 1.) at AddressBook.ABAddressBook.Add(ABRecord ) at MyProject.Renderers.SaveContact.SaveContactToPhoneBook(String Name, String Number, String Email) in /Users/MyCompany/Projects/MyProject-maui/MyProject/Renderers/SaveContact.cs:line 55

The codes I have used are from Xamarin Forms Dependency Service and I changed it to Conditional Compilation for MAUI integration.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,729 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,406 Reputation points Microsoft Vendor
    2023-11-16T06:15:13.2533333+00:00

    Hello,

    For Android, please use var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; to replace var activity = Android.App.Application.Context as Activity;

    For iOS, ABAddressBook is deprecated, you can use CNMutableContact to do it

    You can refer to the detailed code from contact-objects, this is a Xamarin.iOS document, but it works in the MAUI Conditional Compilation as well.

    As note: Your app's Info.plist must contain an NSContactsUsageDescription key with a string value explaining to the user how the app uses this data.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.