创建一个call项目
Call Directory Extension创建成功后,会发现多了下面几个文件:
CallDirectoryHandler.m文件中提供了如下三个方法:
1、开始请求的方法,在打开设置-电话-来电阻止与身份识别开关时,系统自动调用
- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context
2、添加黑名单:根据生产的模板,只需要修改CXCallDirectoryPhoneNumber数组,注意,电话号码前要加区号:+86,数组内号码要按升序排列。
- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context
3、添加信息标识:需要修改CXCallDirectoryPhoneNumber数组和对应的标识数组;CXCallDirectoryPhoneNumber数组存放的号码和标识数组存放的标识要一一对应,CXCallDirectoryPhoneNumber数组内的号码要按升序排列
- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context
开启权限:
检查权限是否开启
-(void)checkPermissions
{
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
// 获取权限状态
[manager getEnabledStatusForExtensionWithIdentifier:@"com.womow.wxpush.call" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) {
if (!error) {
NSString *title = nil;
if (enabledStatus == CXCallDirectoryEnabledStatusDisabled) {
/*
CXCallDirectoryEnabledStatusUnknown = 0,
CXCallDirectoryEnabledStatusDisabled = 1,
CXCallDirectoryEnabledStatusEnabled = 2,
*/
title = @"未授权,请在设置->电话授权相关权限";
}else if (enabledStatus == CXCallDirectoryEnabledStatusEnabled) {
title = @"授权";
}else if (enabledStatus == CXCallDirectoryEnabledStatusUnknown) {
title = @"不知道";
}
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
message:title
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}else{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"有错误"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}
若提示未授权,则手动开启权限:
运行项目到真机中,效果图如下:
更新号码
-(void)updateData
{
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
[manager reloadExtensionWithIdentifier:@"com.womow.wxpush.call" completionHandler:^(NSError * _Nullable error) {
if (error == nil) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"更新成功"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}else{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"更新失败"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}