打開 “項目->Capabilities”蛛蒙,找到App Groups的數(shù)據(jù)欄1、創(chuàng)建一個名為ckphone的項目如下:
2 、
2.1、“File->new->target->Call Directory Extension”創(chuàng)建名為testCallKit的Extension target,輸入名稱,然后點擊激活兰迫。
2.2信殊、然后生成的項目結(jié)構(gòu)如下
3、在容器App中創(chuàng)建共享文件去APP groups
3.1汁果、打開 “項目->Capabilities”涡拘,找到App Groups的數(shù)據(jù)欄打開共享區(qū)
3.2、新增共享區(qū)“group.ckphone.app”
3.3据德、檢查是否具備訪問系統(tǒng)的權(quán)限
//檢查是否獲得電話? 來電阻止和身份識別的系統(tǒng)權(quán)限
- (void)checkpermission {
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
// 獲取權(quán)限狀態(tài)
[manager getEnabledStatusForExtensionWithIdentifier:@"ETelExchangeForiphone.zongjifuwuCallKitNew" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) {
NSString *title = nil;
if (!error) {
if (enabledStatus == CXCallDirectoryEnabledStatusDisabled) {
/*
CXCallDirectoryEnabledStatusUnknown = 0,
CXCallDirectoryEnabledStatusDisabled = 1,
CXCallDirectoryEnabledStatusEnabled = 2,
*/
title = @"來電彈屏功能未授權(quán)鳄乏,請在'設(shè)置'->'電話'->'來電阻止與身份識別'中授權(quán)";
}else if (enabledStatus == CXCallDirectoryEnabledStatusEnabled) {
title = @"來電彈屏功能已授權(quán)";
}else if (enabledStatus == CXCallDirectoryEnabledStatusUnknown) {
title = @"未知錯誤";
}
}else{
title = @"有錯誤";
}
[CVAlertTool showTip:title onView:self.navigationController.view];
}];
}
3.4、打開權(quán)限
3.5棘利、將數(shù)據(jù)更新到共享區(qū)橱野,并且觸發(fā)到CallKit擴(kuò)展讀取數(shù)據(jù)
- (BOOL)saveCallKitData {
NSString *name = _nameLabel.text;
NSString *phone = [NSString stringWithFormat:@"86%@", _phoneLabel.text ];
if([name isEqualToString:@""]){
return NO;
}
if([phone isEqualToString:@"86"]){
return NO;
}
NSDictionary *data = @{
@"name" : name,
@"phone" : phone
};
NSError *err = nil;
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.ckphone.app"];
containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/callKitData"];
[containerURL removeAllCachedResourceValues];
BOOL result = [data writeToURL:containerURL atomically:YES];
if (!result) {
[CVAlertTool showTip:@"數(shù)據(jù)保存失敗善玫!" onView:self.navigationController.view];
} else {
[CVAlertTool showTip:@"數(shù)據(jù)保存成功水援!" onView:self.navigationController.view];
[self updateCallKitData];
}
return result;
}
- (void)updateCallKitData{
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
[manager reloadExtensionWithIdentifier:@"com.demo.ckphone.PhoneCallKit" completionHandler:^(NSError * _Nullable error) {
NSString *message;
if (error == nil) {
message = @"彈屏數(shù)據(jù)更新成功";
}else{
message = @"彈屏數(shù)據(jù)更新失敗";
}
[CVAlertTool showTip:message onView:self.navigationController.view];
}];
}
3.6、CallKit中編寫讀取代碼
- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context {
//file讀取
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.ckphone.app"];
containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/callKitData"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfURL:containerURL];
NSString *name = [dict objectForKey:@"name"];
NSString *phone = [dict objectForKey:@"phone"];
CXCallDirectoryPhoneNumber phoneNum = [phone longLongValue];
[context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNum label:name];
return YES;
}
4茅郎、運行過程以及結(jié)果
4.1蜗元、輸入身份識別信息和電話號碼,點擊“來電提示”系冗。
4.2奕扣、如果沒有權(quán)限則到電話設(shè)置中賦權(quán)
4.3、回到應(yīng)用界面再次點擊“來電提示”掌敬,然后撥打電話惯豆,隨后進(jìn)入系統(tǒng)的通話記錄界面,來電提示效果如下:
??注意:應(yīng)用中幾個需要注意的地方
1奔害、電話號碼寫入共享區(qū)的格式遵循以下規(guī)則
1.1循帐、固話號碼:86+區(qū)號(去除第一個0)+電話號碼,例如 01012345678 寫入應(yīng)該是 861012345678
1.2舀武、手機(jī)號碼:86+手機(jī)號碼拄养,例如18100000000寫入應(yīng)該是8618100000000
2、如果寫入大量的電話號碼的時候,號碼數(shù)組必須遵循加上86后的升序排列