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. cocos2dxHellowoed 发现 2.2.3

    cocos2d 笔记 文件夹介绍 cocosdx ----->cocos2d主要代码 CocosDenshion---->cocos2d的声音的 Document------>文档 ...

  2. HBase性能调优(转)

    原文链接:http://www.blogjava.net/ivanwan/archive/2011/06/15/352350.html 因官方Book Performance Tuning部分章节没有 ...

  3. HttpClient 学习整理

    HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的实现,发现这个开源项目之后就有点眉目了,令人头痛的cookie问题还是有办法解决滴.在网上整理了一些东西,写得很好,寄放在这里 ...

  4. jquery选择器用法笔记(第一部分)

    由于我在项目中用jquery比较多,而且觉得jquery真的很不错,尤其是其灵活高效的选择器更是令人无法忘怀.那么,今天就来写一篇非常基础的关于jquery选择器的文章,路过的朋友可以收藏以作参考. ...

  5. Maven Dependencies没有了的解决办法

    头疼的问题,maven Dependencies突然没有了,别的项目都有,个别的却怎么也出不来.  以下是某大神的解决方法,特此转发,以防丢失: 网上搜索了一番,大多都是下面这种做法: 右击 Mave ...

  6. 分解大量switch-case分支的两种方法

    项目经过长期多人的维护,所谓人多手杂,出现不少过多过长的switch-case分支,或者多重switch-case嵌套.每每添加功能,我都会紧皱眉头,然后带着罪恶感向已经成百上千行的函数里再添上一个c ...

  7. Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  8. Android -- onWindowFocusChanged

    Android中获取手机屏幕的高度和宽度,我们知道在onCreate方法中获取到的值都是为0的,有人说可以在onClick方法中获取值,这个也是个方法 ,但在onWindowFocusChanged方 ...

  9. 多个桌面Deskspace如何使用

    1 给Deskspace设置背景.在DeskSpace选项中设置显示背景为天空箱体图像(软件自带的图像效果,也可以使用静态图像,即自己的图片) 2 给六个桌面各设置一个背景(也可以使用同一个背景)右击 ...

  10. ios Mac 地址获取

    //mac address #include <sys/socket.h> // Per msqr #include <sys/sysctl.h> #include <n ...