如何使用 Xamarin 中的 CallDirectoryExtension 创建一个提供来电显示的应用程序?

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

我正在按照 Xamarin 指南创建一个 iPhone 应用程序,该应用程序可以阻止 phone 号码或通过创建呼叫目录扩展显示来电显示:

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

Xamarin 文档中的代码未完全更新,但如果您只是在 Xamarin Studio 中为 OS X 创建一个调用目录扩展,您将获得一些示例代码来帮助您入门。

以下是阻止 phone 号码 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) { }
}

从示例代码来看,显示相同号码的来电显示应该同样容易,只需使用方法 AddIdentificationEntry 而不是 AddBlockingEntry,但我无法让它工作。

我错过了什么?

答案简单得令人沮丧。

AddIdentificationEntry() 需要国家代码,AddBlockingEntry() 不需要。

当我在 phone 号码的开头添加 47(挪威的国家/地区代码)时,它起作用了。这是显示挪威 phone 号码 22334455 来电显示的工作代码:

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

  cxContext.AddIdentificationEntry(4722334455, "Telemarketer");

  cxContext.CompleteRequest(null);
}

addBlockingEntry() 使用 22334455 和 4722334455 作为输入。