CallKit 这个开发框架,能够让语音或视讯电话的开发者将 UI 界面整合在 iPhone 原生的电话 App 中.将允许开发者将通讯 App 的功能内建在电话 App 的“常用联络资讯”,以及“通话记录”,方便用户透过原生电话 App,就能直接取用这些第三方功能;允许用户在通知中心就能直接浏览并回覆来电,来电的画面也将整合在 iOS 原生的 UI 里,总体来说,等于让 iOS 原本单纯用来打电信电话的“电话”功能,能够结合众多第三方语音通讯软件,具备更完整的数码电话潜力。CallKit 也拓展了在 iOS 8 就出现的 App Extensions 功能,可以让用户在接收来电时,在原生电话 App 中就透过第三方 App 辨识骚扰电话(例如诈骗).

如何创建一个call项目?

出现下图结构说明创建成功:

注意:cccccccccalldemo.xcdatamodeld必须创建;

来电提醒功能

通过extension模板,创建CallDirectoryExtension
用到的方法:

//开始请求的方法,在打开设置-电话-来电阻止与身份识别开关时,系统自动调用
- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context;
//添加黑名单:根据生产的模板,只需要修改CXCallDirectoryPhoneNumber数组,数组内号码要按升序排列
- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context;
// 添加信息标识:需要修改CXCallDirectoryPhoneNumber数组和对应的标识数组;CXCallDirectoryPhoneNumber数组存放的号码和标识数组存放的标识要一一对应,CXCallDirectoryPhoneNumber数组内的号码要按升序排列
- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context;

设置

实现流程

在打开设置里的开关后,系统会调用beginRequest方法,在这个方法内部会调用添加黑名单和添加信息标识的方法,添加成功后,再调用completeRequestWithCompletionHandler方法通知系统;

代码的实现:

  • sms:或者是sms://:发送短信;

  • tel: 或者是tel://:打电话

  • telprompt:或者是 telprompt://: 打电话;

  • mailto:发送邮件;

  • http:或者是 http://: 浏览网址;

打电话的按钮
 NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.noTextField.text];
    [self.callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    if (!self.callWebview.subviews) {
        [self.view addSubview:_callWebview];
    }
检查授权:
  CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    // 获取权限状态
    [manager getEnabledStatusForExtensionWithIdentifier:@"com.tq.cccccccccalldemo.CallDirectoryExtension" 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];
        }
    }];
CallDirectoryHandler文件的实现方法:

注意:电话号码前要加区号:+86;

/**
 添加黑名单:无法接通
 */- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context/**
 添加信息标识
 */- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context

说明:将项目运行到真机之后,还需要在“设置->电话”设置应用的权限;

判断是否有权限:
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    // 获取权限状态
    [manager getEnabledStatusForExtensionWithIdentifier:@"com.tq.cccccccccalldemo.CallDirectoryExtension" 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];
        }
    }];

项目基本可以达到需求了!黑名单的号码打进来是在通话中,标记的号码显示标记的名字;

标记的号码

这时你会发现你只有第一次运行项目的号码设置才起作用,或者是去设置里面重新授权;显然这是不行的;我们需要实时更新号码:

 CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
    [manager reloadExtensionWithIdentifier:@"com.tq.cccccccccalldemo.CallDirectoryExtension" 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];
        }
    }];

文章转自 芝麻绿豆的简书

最新文章

  1. samba服务器安装,共享nginx目录
  2. iOS 页面显示在键盘之上
  3. beautifulsoup小节
  4. ACM对时间掌控力和日积月累的习惯的意义
  5. 动画的特效Interpolator
  6. oracle的sql函数
  7. css filter详解
  8. (转)高性能I/O模型
  9. Javascript 学习 笔记一
  10. Android应用开发学习笔记之播放音频
  11. 荐 android 如何打包自定义控件(转)
  12. Android 新一代多渠道打包神器
  13. 读《Deep Learning Tutorial》(台湾大学 李宏毅 深度学习教学ppt)后杂记
  14. Apache Beam是什么?
  15. webstorm 安装与基本使用
  16. Centos7下使用yum安装lnmp zabbix3.2
  17. js实现避免浏览器拦截弹出新页面的方法
  18. 剑指offer五十三之表示数值的字符串
  19. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
  20. 剑指offer 29 多于一半的数

热门文章

  1. 金蝶KIS下载地址
  2. PHP array与Json学习
  3. iOS开发-plist文件增删改查
  4. SQL基础(四):SQL命令
  5. Declaration Merging with TypeScript
  6. IN 查询时出现ORA-01795:列表中的最大表达式数为1000解决方法
  7. 使用Yii 1.1框架搭建第一个web应用程序
  8. curl库的使用,32-64
  9. Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
  10. uni-app 如何开启sass\less处理