MAUI: Save contact to device

Sreejith Sreenivasan 611 Reputation points

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

Below is my code:


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;


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

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.

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,160 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 58,251 Reputation points Microsoft Vendor


    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