iOS之来电阻止与身份识别实现 原创
-
LVXIANGAN
码龄13年
关注
为了屏蔽各种中介的电话骚扰,最近安装了腾讯手机管家,安装完成后,设置“骚扰拦截”功能时,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用途,来编写不同功能的代码了。