How to create an iOS call directory extension in Xamarin for blocking thousands of phone numbers

A call directory extension is used to block or identify phone numbers on an iPhone, phone numbers the user don’t have in their contact list.

TL;DR: Because iOS app extensions have very limited resources, it is difficult to load many thousand phone numbers into the call directory without the extension crashing. This is my attempt to get around this issue.

This post expands on Xamarin’s tutorial for how to create a call directory extension, which you probably should read first, here:
Xamarin: Implementing a Call Directory Extension

The end result of that tutorial is a call directory extension that can block or identify two hard coded phone numbers.

Based on Xamarin’s sample code, I have further made an accompanying app that saves some thousand phone numbers in text files. The app saves the phone numbers in files called phonenumbers.0.txt, phonenumbers.1.txt, etc., with a few hundred phone numbers in each file.

It is really important that the phone numbers are in numerical order, smallest number first. You don’t want to do any sorting or filtering in the extension, do all that kind of memory intensive tasks in the app when you create the text files.

Another important nugget of information, is that a phone number has to be saved including the country code. So American phone numbers always start with 1, and Norwegian phone numbers always start with 47.

This is my modified method to add phone numbers from the text files to the phone’s identification directory:

7 thoughts on “How to create an iOS call directory extension in Xamarin for blocking thousands of phone numbers”

  1. Nevermind .. got it 🙂

    Naive question .. can I send a local notification when it does the identification? or can I send any kind of trigger to container app?

      1. Can you please explain App Group creation required for sharing files between extension and the app?

      2. Hi, Pooran

        You ask how to create an app group. It is not straightforward, but it’s done in Apple’s developer portal. This is from Apple’s documentation: “To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app.”

        The full documentation here: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

  2. Please help us. Call Directory Extension – Error Enabling Extension. Failed to request data
    Our app is using the caller id feature of Call Directory Extension(Xamarin Form like you shows). It seems to work well for most users. However 1 case in iphone 7 plus that i cannot enable the extension in Settings > Phone > Call Blocking & Identification. I gets the message “Error Enabling Extension. Failed to request data for appName. You may try enabling the extension again…”. What is a root cause of this error. Please let me know. Thanks so much.

    1. Hi, thingng!

      Thanks for your question.

      I think any exception inside your extension can cause this error.

      When you go into your phone’s settings and clicking Enabled (green) for your extension, you are immediately running the code in your extension. Any exceptions will cause the extension to crash, and the phone turns the setting back to Disabled (grey).

      Since you cannot directly attach a debugger to the code that is running inside the extension, you must instead log a lot. I log to a text file.

      If you have no idea where the code crashes, start by logging the first line of code, and then continue through your code, logging each step the extension performs until you reach the point where the extension crashes, and therefore does not log anymore.

      Hope that was helpful. I’m afraid debugging extensions is a bit difficult.

      Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *