打开 “项目->Capabilities”,找到App Groups的数据栏1、创建一个名为ckphone的项目如下:
2 、
2.1、“File->new->target->Call Directory Extension”创建名为testCallKit的Extension target,输入名称,然后点击激活。
2.2、然后生成的项目结构如下
3、在容器App中创建共享文件去APP groups
3.1、打开 “项目->Capabilities”,找到App Groups的数据栏打开共享区
3.2、新增共享区“group.ckphone.app”
3.3、检查是否具备访问系统的权限
//检查是否获得电话 来电阻止和身份识别的系统权限
- (void)checkpermission {
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
// 获取权限状态
[manager getEnabledStatusForExtensionWithIdentifier:@"ETelExchangeForiphone.zongjifuwuCallKitNew" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) {
NSString *title = nil;
if (!error) {
if (enabledStatus == CXCallDirectoryEnabledStatusDisabled) {
/*
CXCallDirectoryEnabledStatusUnknown = 0,
CXCallDirectoryEnabledStatusDisabled = 1,
CXCallDirectoryEnabledStatusEnabled = 2,
*/
title = @"来电弹屏功能未授权,请在'设置'->'电话'->'来电阻止与身份识别'中授权";
}else if (enabledStatus == CXCallDirectoryEnabledStatusEnabled) {
title = @"来电弹屏功能已授权";
}else if (enabledStatus == CXCallDirectoryEnabledStatusUnknown) {
title = @"未知错误";
}
}else{
title = @"有错误";
}
[CVAlertTool showTip:title onView:self.navigationController.view];
}];
}
3.4、打开权限
3.5、将数据更新到共享区,并且触发到CallKit扩展读取数据
- (BOOL)saveCallKitData {
NSString *name = _nameLabel.text;
NSString *phone = [NSString stringWithFormat:@"86%@", _phoneLabel.text ];
if([name isEqualToString:@""]){
return NO;
}
if([phone isEqualToString:@"86"]){
return NO;
}
NSDictionary *data = @{
@"name" : name,
@"phone" : phone
};
NSError *err = nil;
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.ckphone.app"];
containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/callKitData"];
[containerURL removeAllCachedResourceValues];
BOOL result = [data writeToURL:containerURL atomically:YES];
if (!result) {
[CVAlertTool showTip:@"数据保存失败!" onView:self.navigationController.view];
} else {
[CVAlertTool showTip:@"数据保存成功!" onView:self.navigationController.view];
[self updateCallKitData];
}
return result;
}
- (void)updateCallKitData{
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
[manager reloadExtensionWithIdentifier:@"com.demo.ckphone.PhoneCallKit" completionHandler:^(NSError * _Nullable error) {
NSString *message;
if (error == nil) {
message = @"弹屏数据更新成功";
}else{
message = @"弹屏数据更新失败";
}
[CVAlertTool showTip:message onView:self.navigationController.view];
}];
}
3.6、CallKit中编写读取代码
- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context {
//file读取
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.ckphone.app"];
containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/callKitData"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfURL:containerURL];
NSString *name = [dict objectForKey:@"name"];
NSString *phone = [dict objectForKey:@"phone"];
CXCallDirectoryPhoneNumber phoneNum = [phone longLongValue];
[context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNum label:name];
return YES;
}
4、运行过程以及结果
4.1、输入身份识别信息和电话号码,点击“来电提示”。
4.2、如果没有权限则到电话设置中赋权
4.3、回到应用界面再次点击“来电提示”,然后拨打电话,随后进入系统的通话记录界面,来电提示效果如下:
⭐️注意:应用中几个需要注意的地方
1、电话号码写入共享区的格式遵循以下规则
1.1、固话号码:86+区号(去除第一个0)+电话号码,例如 01012345678 写入应该是 861012345678
1.2、手机号码:86+手机号码,例如18100000000写入应该是8618100000000
2、如果写入大量的电话号码的时候,号码数组必须遵循加上86后的升序排列