CallKit“来电阻止和身份识别”数据共享
CallKit作为一个扩展应用(Extension target)
容器应用(Containing App)是我们自己的应用
载体应用(Host App)扩展应用的触发应用,CallKit的载体应用(Host App)是“设置->电话->来电阻止和身份识别->开关按钮”
CallKit对于容器应用来讲是一个独立的应用,它有自己的进程、运行空间和运行资源,并且和容器应用不能进行直接的数据交互,只能通过共享区的方式,Apple的事项方式是APP Groups。具体步骤如下:
1、打开 “项目->Capabilities”,找到App Groups的数据栏
2、点击添加按钮进行添加ID
添加完以后如下图(如下错误点击fix):
3、容器应用(Containning APP)中编写添代码:
3.1、检查权限
[manager getEnabledStatusForExtensionWithIdentifier:@"com.test.testCallKit" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) {
NSString *title = nil;
if (!error) {
if (enabledStatus == CXCallDirectoryEnabledStatusDisabled) {
/*
CXCallDirectoryEnabledStatusUnknown = 0,
CXCallDirectoryEnabledStatusDisabled = 1,
CXCallDirectoryEnabledStatusEnabled = 2,
*/
title = @"来电弹屏功能未授权,请在'设置'->'电话'->'来电阻止与身份识别'中授权";
}else if (enabledStatus == CXCallDirectoryEnabledStatusEnabled) {
title = @"来电弹屏功能已授权";
}else if (enabledStatus == CXCallDirectoryEnabledStatusUnknown) {
title = @"未知错误";
}
}else{
title = @"有错误";
}
}];
3.2、向共享区中写入数据(实例是将一个plist文件中的数据写入):
//plist数据
NSString *plistPath = [[CallKitDataManager sharedCallKitDataManager]plistPath];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSError *err = nil;
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.shares.test"];
containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/callKitData"];
[containerURL removeAllCachedResourceValues];
BOOL result = [data writeToURL:containerURL atomically:YES];
if (!result) {
NSLog(@"%@",err);
} else {
NSLog(@"save success.");
}
return result;
3.3、CallKit应用(Extension target)中获取数据并且写入系统
NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"com.shares.test"]; containerURL = [containerURL URLByAppendingPathComponent:@"Library/Caches/callKitData"]; NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfURL:containerURL]; NSArray*PhoneNames = dict[@"phoneNameArr"]; NSArray*phoneNumbers = dict[@"phoneNumArr"];
for (NSUInteger i = 0; i < phoneNumbers.count; i += 1) {
NSNumber *number = phoneNumbers[i];
CXCallDirectoryPhoneNumber phoneNumber;
#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
phoneNumber = number.unsignedLongValue;
#else
phoneNumber = number.unsignedLongLongValue;
#endif
NSString *label = PhoneNames[i];
NSLog(@"来点提示:%@ :%lld",label,phoneNumber);
[context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:label];
到此,全部过程完成。
网友评论