CallKit 这个开发框架,能够让语音或视讯电话的开发者将 UI 界面整合在 iPhone 原生的电话 App 中.将允许开发者将通讯 App 的功能内建在电话 App 的“常用联络资讯”,以及“通话记录”,方便用户透过原生电话 App,就能直接取用这些第三方功能;允许用户在通知中心就能直接浏览并回覆来电,来电的画面也将整合在 iOS 原生的 UI 里,总体来说,等于让 iOS 原本单纯用来打电信电话的“电话”功能,能够结合众多第三方语音通讯软件,具备更完整的数码电话潜力。CallKit 也拓展了在 iOS 8 就出现的 App Extensions 功能,可以让用户在接收来电时,在原生电话 App 中就透过第三方 App 辨识骚扰电话(例如诈骗).
如何创建一个call项目?
出现下图结构说明创建成功:
注意:
cccccccccalldemo.xcdatamodeld必须创建;
来电提醒功能
通过extension模板,创建CallDirectoryExtension
用到的方法:
1
2
3
4
5
6
|
//开始请求的方法,在打开设置-电话-来电阻止与身份识别开关时,系统自动调用
- (
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://: 浏览网址;
打电话的按钮
1
2
3
4
5
|
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];
}
|
检查授权:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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;
1
2
3
4
5
|
/**
添加黑名单:无法接通
*/
- (
BOOL
)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context
/**
添加信息标识
*/
- (
BOOL
)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context
|
说明:
将项目运行到真机之后,还需要在“设置->电话”设置应用的权限;
判断是否有权限:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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];
}
}];
|
项目基本可以达到需求了!黑名单的号码打进来是在通话中,标记的号码显示标记的名字;
这时你会发现你只有第一次运行项目的号码设置才起作用,或者是去设置里面重新授权;显然这是不行的;我们需要实时更新号码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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];
}
}];
|