CallKit

创建一个call项目
2017-06-22 下午3.45.29.jpg
2017-06-22 下午3.44.08.jpg
2017-06-22 下午4.07.51.jpg
Call Directory Extension创建成功后,会发现多了下面几个文件:
2017-06-22 下午4.36.29.jpg
CallDirectoryHandler.m文件中提供了如下三个方法:
2017-06-22 下午4.49.10.jpg

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];
        }
    }];
}

若提示未授权,则手动开启权限:

IMG_1990.PNG
IMG_1991.PNG

运行项目到真机中,效果图如下:

更新号码
-(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];
        }
    }];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先导入 Call Directory Extension 导入完成后,项目会多了一个文件夹文件夹里的文件是Call...
    Y_van阅读 6,185评论 2 5
  • 打开 “项目->Capabilities”,找到App Groups的数据栏1、创建一个名为ckphone的项目如...
    couriant阅读 6,169评论 0 1
  • 前言 最近需要实现一个新需求,用iOS 10出的CallKit实现将APP的通讯录的信息同步到系统中,可以不把人员...
    莫云溪阅读 14,756评论 22 23
  • 1 三个月前兴冲冲地开了公众号,打算尽量每日更新,拿原创,拿打赏。 现在三个月过去了,我的公众号关注依然寥寥无几,...
    空心人乙阅读 1,423评论 2 1
  • 事先说明此文章并不是针对《互赞好么?不好!》而写,我只是发表我自己的看法。 常年深居居与简书的书友们有时会收到其他...
    不懂wz阅读 4,028评论 28 8