iOS使用Call Directory Extension实现来
最近项目中需要增加一个新的需求,来电身份识别.之前没有做过相关功能的开发.因此在网上搜索资料查到了一个关键的东西叫
Call Directory Extension.
Call Directory Extension.jpg
先看一张最终效果图
15.jpg
在开发过程中我主要参考了这篇文章:iOS用CallKit实现来电识别、来电拦截
文章中有实现的方法,以及demo可供下载.
在此感谢原作者.
而我主要是分享一下我在开发中遇到的一些问题供大家参考.以及原文中没有提及的一些东西做一下补充.
注:本文实现的方式为读取本地
csv
文件写入APP Groups
共享空间.并在Call Directory Extension, 并在- (void)addAllIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context;
中读取APP Groups
存储的文件.
具体创建Call Directory Extension
的方法原文中有提及,在这里我就不赘述了.但需要注意一点Call Directory Extension
target的工程的命名规则.
举个例子主工程名字com.company.test
那么你的Call Directory Extension
的名字应为com.company.test.xxxxxx
(xxxxxx为可替换的名字)
另需注意:Call Directory Extension的工程也需要在
apple developer
开发者网站注册 app id 与创建相关的生产与测试配置文件.
APP Groups
创建

1.前往Apple Developer创建APP Groups
命名方式为
group.com.company.test
(以上面为例)
其中group
为系统添加的标识不可删除与更改.后面的com.company.test
可随意更改.但一般都是以组织+工程的方式命名,方便管理.
2.创建主工程与Call Directory Extension工程的App IDs.在创建过程中需要勾选APP Groups
(注:以下操作两个app ids都需要)

创建完你会发现APP Groups不可用.别方,正常现象.

点击下面的edit进入编辑



到这里就已经开启了app groups的权限了(
再次注意:主工程与Call Directory Extension的工程都要设置2.操作
)还需要配置相应的Provisioning Profiles并下载到本地.
3.在工程中的设置如下


到此就已经配置完了
遇到的一些问题
当你在设置中开启身份识别权限或者在主工程中调用 - (void)reloadExtensionWithIdentifier:(NSString *)identifier completionHandler:(nullable void (^)(NSError *_Nullable error))completion;
系统将自动调用下面这个方法
- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context {
context.delegate = self;
// Check whether this is an "incremental" data request. If so, only provide the set of phone number blocking
// and identification entries which have been added or removed since the last time this extension's data was loaded.
// But the extension must still be prepared to provide the full set of data at any time, so add all blocking
// and identification phone numbers if the request is not incremental.
if (@available(iOS 11.0, *)) {
if (context.isIncremental) {
[self addOrRemoveIncrementalBlockingPhoneNumbersToContext:context];
[self addOrRemoveIncrementalIdentificationPhoneNumbersToContext:context];
} else {
[self addAllBlockingPhoneNumbersToContext:context];
[self addAllIdentificationPhoneNumbersToContext:context];
}
} else {
// Fallback on earlier versions
}
[context completeRequestWithCompletionHandler:nil];
}
身份识别主要用到的方法是[self addAllIdentificationPhoneNumbersToContext:context];
我在开发中遇到的问题就是当我在主工程中调用reloadExtensionWithIdentifier:而在这边的if (context.isIncremental) 判断为是并没有走else的分支.所以导致我的数据并没有写入进去.
被这个问题困扰了很久.在网上搜索也没有找到关于context.isIncremental的说法.无奈只能修改为if (!context.isIncremental)..如果有大神知道原因可以补充一下这里.
另附:
如何调试Call Directory Extension
的方法
在测试的时候请将读取本地文件和调用reloadExtensionWithIdentifier:
的代码写到一个按钮里面..即让他变成可控的.点一下就调用一次..后面会讲为何这样做..
1.首先将你的主工程运行在
真机上 机上 上~~~~~
10.jpg
2.14.jpeg
去设置->电话->来电身份识别开启权限
然后点击一次之前设置的调用写入方法的按钮
3之后在点击这个11.jpg
设置完之后再点击一次之前的按钮就可以开始调试了
12.jpg
欢迎随时交流,如果有写的不对的地方希望大家帮忙改正.
注各位一切顺利!!