Bjarte Aune Olsen
Bjarte Aune Olsen

Reputation: 3310

How do I create an app that gives caller id using a CallDirectoryExtension in Xamarin?

I am following the Xamarin guide to create an iPhone app that can block phone numbers or display caller id by creating a Call Directory Extension:

https://developer.xamarin.com/guides/ios/platform_features/introduction-to-ios10/callkit/#Implementing-a-Call-Directory-Extension

The code in Xamarin's documentation is not completely updated, but if you just create a Call Directory Extension in Xamarin Studio for OS X, you get some sample code to get you starting.

Below is the simplest possible code to block phone number 22334455:

[Register("CallDirectoryHandler")]
public class CallDirectoryHandler : CXCallDirectoryProvider, ICXCallDirectoryExtensionContextDelegate
{
    protected CallDirectoryHandler(IntPtr handle) : base(handle) { }

    public override void BeginRequestWithExtensionContext(NSExtensionContext context)
    {
        var cxContext = (CXCallDirectoryExtensionContext)context;
        cxContext.Delegate = this;

        cxContext.AddBlockingEntry(22334455);
        //cxContext.AddIdentificationEntry(22334455, "Telemarketer");

        cxContext.CompleteRequest(null);
    }

    public void RequestFailed(CXCallDirectoryExtensionContext extensionContext, NSError error) { }
}

From the sample code it seems it should be just as easy to display caller id for the same number, simply use the method AddIdentificationEntry instead of AddBlockingEntry, but I cannot get it to work.

What am I missing?

Upvotes: 1

Views: 1427

Answers (1)

Bjarte Aune Olsen
Bjarte Aune Olsen

Reputation: 3310

The answer was frustratingly simple.

AddIdentificationEntry() requires the country code, AddBlockingEntry() does not.

When I added 47 (Norway's country code) to the beginning of the phone number, it worked. Here is the working code to display caller id for Norwegian phone number 22334455:

public override void BeginRequestWithExtensionContext(NSExtensionContext context)
{
  var cxContext = (CXCallDirectoryExtensionContext)context;
  cxContext.Delegate = this;

  cxContext.AddIdentificationEntry(4722334455, "Telemarketer");

  cxContext.CompleteRequest(null);
}

addBlockingEntry() works with both 22334455 and 4722334455 as input.

Upvotes: 3

Related Questions