iOS之来电阻止与身份识别实现
来源:互联网 发布:linux shell嵌套for 编辑:程序博客网 时间:2024/06/07 06:46
为了屏蔽各种中介的电话骚扰,最近安装了腾讯手机管家,安装完成后,设置“骚扰拦截”功能时,App提示需要点击“设置--->电话--->来电阻止与身份识别--->勾选App提供的"黄页"、“电话识别”和“电话拦截”。
了解过苹果沙盒机制的朋友都知道,一般在App中要访问到其他 App/硬件(地理位置,相册,通讯录,话筒) 内容,会通过系统弹窗询问用户,必须要获取管理员许可才行,比如这样:
那么上面的“来电阻止和身份识别”功能又是什么东东呢?
CallKit.framework
苹果在iOS 10中为我们带来了callkit.framework,当我们语言通话显示系统界面时,它允许我们对来电号码进行识别、过滤、标记、阻止等操作。目前国内垃圾电话、骚扰电话、诈骗电话不胜其烦,针对iPhone全球第二大市场,苹果今次顺势而为推出开放接口,也算是人心所向。有兴趣的朋友可以通过官方文档说明了解callkit.framework。
下面我们尝试来写一个demo,让自己App出现在电话“设置--->电话--->来电阻止与身份识别 列表中,实现来电过滤功能
1、新建一个iOS工程
2、完成后,新建target:点击"+"---> 选择iOS,Call Directory Extension, 输入名称如:CallFirewall,一路next,选择“Activate”
3、完成上面操作,可以看到在项目工程中,自动生成了下面几个文件。
4、打开文件 CallDirectoryHandler.h 和 CallDirectoryHandler.m,使用注意事项,请参照代码注释:
//// CallDirectoryHandler.h// CallFirewall//// Created by lvxiangan on 26/07/2017.// Copyright © 2017 lvxiangan. All rights reserved.//#import <Foundation/Foundation.h>#import <CallKit/CallKit.h>@interface CallDirectoryHandler : CXCallDirectoryProvider@end
//// CallDirectoryHandler.m// CallFirewall//// Created by lvxiangan on 26/07/2017.// Copyright © 2017 lvxiangan. All rights reserved.//#import "CallDirectoryHandler.h"@interface CallDirectoryHandler () <CXCallDirectoryExtensionContextDelegate>@end@implementation CallDirectoryHandler// 请求前检查- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context { context.delegate = self; if (![self addBlockingPhoneNumbersToContext:context]) { NSLog(@"Unable to add blocking phone numbers"); NSError *error = [NSError errorWithDomain:@"CallDirectoryHandler" code:1 userInfo:nil]; [context cancelRequestWithError:error]; return; } if (![self addIdentificationPhoneNumbersToContext:context]) { NSLog(@"Unable to add identification phone numbers"); NSError *error = [NSError errorWithDomain:@"CallDirectoryHandler" code:2 userInfo:nil]; [context cancelRequestWithError:error]; return; } [context completeRequestWithCompletionHandler:nil];}// 来电阻止- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context { // Retrieve phone numbers to block from data store. For optimal performance and memory usage when there are many phone numbers, // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded. // // Numbers must be provided in numerically ascending order. CXCallDirectoryPhoneNumber phoneNumbers[] = { 14085555555, 18005555555 }; NSUInteger count = (sizeof(phoneNumbers) / sizeof(CXCallDirectoryPhoneNumber)); for (NSUInteger index = 0; index < count; index += 1) { CXCallDirectoryPhoneNumber phoneNumber = phoneNumbers[index]; [context addBlockingEntryWithNextSequentialPhoneNumber:phoneNumber]; } return YES;}// 来电识别- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context { // Retrieve phone numbers to identify and their identification labels from data store. For optimal performance and memory usage when there are many phone numbers, // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded. // // Numbers must be provided in numerically ascending order. CXCallDirectoryPhoneNumber phoneNumbers[] = { 18775555555, 18885555555 }; NSArray<NSString *> *labels = @[ @"Telemarketer", @"Local business" ]; NSUInteger count = (sizeof(phoneNumbers) / sizeof(CXCallDirectoryPhoneNumber)); for (NSUInteger i = 0; i < count; i += 1) { CXCallDirectoryPhoneNumber phoneNumber = phoneNumbers[i]; NSString *label = labels[i]; [context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:label]; } return YES;}#pragma mark - CXCallDirectoryExtensionContextDelegate// 添加黑名单或识别来电失败处理- (void)requestFailedForExtensionContext:(CXCallDirectoryExtensionContext *)extensionContext withError:(NSError *)error { // An error occurred while adding blocking or identification entries, check the NSError for details. // For Call Directory error codes, see the CXErrorCodeCallDirectoryManagerError enum in <CallKit/CXError.h>. // // This may be used to store the error details in a location accessible by the extension's containing app, so that the // app may be notified about errors which occured while loading data even if the request to load data was initiated by // the user in Settings instead of via the app itself.}@end
同理,参照腾讯手机管家的做法,我们建多几个target,就会在“来电阻止与身份识别”中有多个选项。
这样,我们就可以根据自定义target用途,来编写不同功能的代码了。
- iOS之来电阻止与身份识别实现
- iOS 来电阻止和身份识别
- iOS 来电阻止和身份识别二
- msf之VNC身份识别
- 通过Web Services实现统一身份识别
- 运用session实现系统身份识别功能
- zxing和opencv实现身份识别
- Kinect身份识别:技术与经验
- Kinect身份识别:技术与经验
- 利用CallKit 实现来电识别 学到的知识 类扩展
- Android开发之来电电话挂断实现
- Android之来电秀实现(一)
- Android之来电秀实现(二)
- Android 识别来电号码
- WebService安全性的几种实现方法【身份识别】
- IOS的Cookie之网页身份认证与Session的追踪
- play framework学习笔记之 身份识别,openID
- iOS实现手势识别
- 浅谈c++继承与对象模型
- Android面试(四)内存优化一
- 详解 Vue 2.4.0 带来的 4 个重大变化
- 做人资OA产品过程中经历的坑【链家篇】
- 上传base64格式图片, Illegal base64 character 20
- iOS之来电阻止与身份识别实现
- NKOJ 3253 (CQOI 2015) 标识设计(状压DP)
- web/html5调用摄像头实现二维码扫描
- SSH通过密钥登入.md
- react-router v4
- 利用定时器实现流水灯闪烁同时数码管循环显示1-f
- JavaScript-JSON详解
- SQL优化技巧 改善数据库性能
- 转:深度 | 深度学习与神经网络全局概览:核心技术的发展历程