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

文章转自 芝麻绿豆的简书

iOS10:CallKit的简单应用的更多相关文章

  1. 【腾讯Bugly干货分享】QQ电话适配iOS10 Callkit框架

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/58009392302e4725036142fc Dev Club 是一个交流移动 ...

  2. iOS10 CAAnimationDelegate 的简单适配

    1.iOS10中CAAnimationDelegate的警告 原有的工程用xcode8打开编译后,莫名的增加了许多警告,其中关于动画的警告有这样一个,虽然运行后发现并没有什么影响,但还是要探究一下: ...

  3. iOS10新特性之CallKit开发详解:锁屏接听和来电识别

    国庆节过完了,回家好好休息一天,今天好好分享一下CallKit开发.最近发现好多吃瓜问CallKit的VoIP开发适配,对iOS10的新特性开发和适配也在上个月完成,接下来就分享一下VoIP应用如何使 ...

  4. Swift:一个简单的货币转换器App在iOS10中的分析和完善

    这本不算是一个完整的货币转换App,只不过是一个小巧的学习性质的程序.该App覆盖了如下几个知识点: 多国语言的支持 通过网络Api接口读取数据 最后我们来修复一个原来代码中的一个小错误作为完美的收尾 ...

  5. iOS开发 适配iOS10

    2016年9月7日,苹果发布iOS 10.2016年9月14日,全新的操作系统iOS 10将正式上线. 作为开发者,如何适配iOS10呢? 1.Notification(通知) 自从Notificat ...

  6. 【iOS10 SpeechRecognition】语音识别 现说现译的最佳实践

    首先想强调一下“语音识别”四个字字面意义上的需求:用户说话然后马上把用户说的话转成文字显示!,这才是开发者真正需要的功能. 做需求之前其实是先谷歌百度一下看有没有造好的轮子直接用,结果真的很呵呵,都是 ...

  7. 对iOS10新增Api的详细探究

    本文主要是一些对iOS新功能的探索,之前发现博客里关于iOS新功能的分析大多是过于概括,每个功能几句话,无法了解到具体的功能.所以本次的探索是基于Api层面,着重看一些具体用法所做的笔记,本来想分别画 ...

  8. Xcode8+和iOS10+使用Masonry自动计算行高

    说起tableView的自动计算行高,真的是不想再提了,写了不知道几百遍了.可就是这麽一个小玩意儿,把我给难的不行不行的,眼看都要没头发了. 1.设置tableView的预估行高和行高为自动计算 // ...

  9. iOS10推送通知适配

    iOS10推送新增了UserNotifications Framework,使用起来其实很简单. 只是在iOS10以上系统上点击通知栏,回调方法不再走原来的这两个方法 - (void)applicat ...

随机推荐

  1. iOS:多个单元格的删除(方法一)

    采用存取indexPath的方式,来对多个选中的单元格进行删除 删除前: 删除后: 分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们 ...

  2. 3D屏保:排色榜

    3D屏保:排色榜 排色榜,是一个针对图形学中的色彩进行排序的DEMO,这里的色是色彩的意思,看成别的点进来的同学请自觉面壁.该DEMO可以按RGB,GBR,BRG,灰度值四种方式进行排序.排序算法为冒 ...

  3. 介绍两个非常好用的Javascript内存泄漏检测工具

    内存泄漏对开发者来说一般很难检测因为它们是由一些大量代码中的意外的错误引起的,但它在系统内存不足前并不影响程序的功能.这就是为什么会有人在很长时间的测试期中收集应用程序性能指标来测试性能. 最简单的检 ...

  4. GoLang中面向对象的三大特性

    有过 JAVA 语言学习经历的朋友都知道,面向对象主要包括了三个基本特征:封装.继承和多态.封装,就是指运行的数据和函数绑定在一起,JAVA 中主要是通过 super 指针来完成的:继承,就是指 cl ...

  5. Error Lookup工具

    GetLastError()获取到的错误代码,可以通过VS2013自带的Error Lookup工具来查询错误的描述.

  6. (算法)Trapping Rain Water I

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  7. ArcGIS Pro体验04——菜单栏

    对菜单栏进行熟悉一下: 1.地图菜单 剪切板(Clipboard):剪切(Cut).复制(Copy).粘贴(Paste),这些不用说了,在ArcMap中是放在"编辑"菜单下面的.当 ...

  8. Python面向对象编程 - 一个记事本程序范例(一)

    notebook.py import datetime last_id = 0 class Note: '''Represent a note in the notebook. Match again ...

  9. dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta

    dyld: Library not loaded: @rpath/XCTest.framework/XCTest   Referenced from: /private/var/mobile/Cont ...

  10. 〖Linux〗Qt+gsoap开发客户端程序,服务端地址设定的字符串转换处理

    之所以写出来,是由于经常因为这个问题屡屡丢面子.. 一般情况下,QString转换成(char*),我们一般直接使用: char *str = qstr->text().toLatin1().d ...