iOS10:CallKit的简单应用
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的简单应用的更多相关文章
- 【腾讯Bugly干货分享】QQ电话适配iOS10 Callkit框架
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/58009392302e4725036142fc Dev Club 是一个交流移动 ...
- iOS10 CAAnimationDelegate 的简单适配
1.iOS10中CAAnimationDelegate的警告 原有的工程用xcode8打开编译后,莫名的增加了许多警告,其中关于动画的警告有这样一个,虽然运行后发现并没有什么影响,但还是要探究一下: ...
- iOS10新特性之CallKit开发详解:锁屏接听和来电识别
国庆节过完了,回家好好休息一天,今天好好分享一下CallKit开发.最近发现好多吃瓜问CallKit的VoIP开发适配,对iOS10的新特性开发和适配也在上个月完成,接下来就分享一下VoIP应用如何使 ...
- Swift:一个简单的货币转换器App在iOS10中的分析和完善
这本不算是一个完整的货币转换App,只不过是一个小巧的学习性质的程序.该App覆盖了如下几个知识点: 多国语言的支持 通过网络Api接口读取数据 最后我们来修复一个原来代码中的一个小错误作为完美的收尾 ...
- iOS开发 适配iOS10
2016年9月7日,苹果发布iOS 10.2016年9月14日,全新的操作系统iOS 10将正式上线. 作为开发者,如何适配iOS10呢? 1.Notification(通知) 自从Notificat ...
- 【iOS10 SpeechRecognition】语音识别 现说现译的最佳实践
首先想强调一下“语音识别”四个字字面意义上的需求:用户说话然后马上把用户说的话转成文字显示!,这才是开发者真正需要的功能. 做需求之前其实是先谷歌百度一下看有没有造好的轮子直接用,结果真的很呵呵,都是 ...
- 对iOS10新增Api的详细探究
本文主要是一些对iOS新功能的探索,之前发现博客里关于iOS新功能的分析大多是过于概括,每个功能几句话,无法了解到具体的功能.所以本次的探索是基于Api层面,着重看一些具体用法所做的笔记,本来想分别画 ...
- Xcode8+和iOS10+使用Masonry自动计算行高
说起tableView的自动计算行高,真的是不想再提了,写了不知道几百遍了.可就是这麽一个小玩意儿,把我给难的不行不行的,眼看都要没头发了. 1.设置tableView的预估行高和行高为自动计算 // ...
- iOS10推送通知适配
iOS10推送新增了UserNotifications Framework,使用起来其实很简单. 只是在iOS10以上系统上点击通知栏,回调方法不再走原来的这两个方法 - (void)applicat ...
随机推荐
- 【STL学习】堆相关算法详解与C++编程实现(Heap)
转自:https://blog.csdn.net/xiajun07061225/article/details/8553808 堆简介 堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级 ...
- 网络编程socket之listen函数
摘要:listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被 ...
- 我的SQL里哪个语句占用的CPU最多?
可以使用下面的语句来得到 SELECT SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1, ( (CASE qs.statement_end_off ...
- Median of Two Sorted Array leetcode java
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- WebChromeClient 简介 API 案例
代码位置:https://github.com/baiqiantao/WebViewTest.git 设计思想理解 在WebView的设计中,不是什么事都要WebView类干的,有相当多的杂事是分给其 ...
- Java基础(十二):包(package)
一.Java 包(package): 为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间.包的作用: 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2.如同文件夹 ...
- ubuntu添加默认路由才可以访问网络
- Thread-Local Storage for C99
线程本地存储(TLS)是一种机制,通过这样的机制进行变量分配.在每一个现存线程都有一个实例变量.这样的执行模型GCC用来实现这个,起源于IA-64处理器,可是已经被迁移到其它的处理器.它须要大量的支持 ...
- [ kvm ] 四种简单的网络模型
1. 隔离模式:虚拟机之间组建网络,该模式无法与宿主机通信,无法与其他网络通信,相当于虚拟机只是连接到一台交换机上. 2. 路由模式:相当于虚拟机连接到一台路由器上,由路由器(物理网卡),统一转 ...
- ORACLE关于锁表查询的部分SQL
http://www.cnblogs.com/quanweiru/archive/2012/08/28/2660700.html --查询表空间名称和大小 SELECT UPPER (F.TABLES ...