一、创建一个新的target
屏幕快照 2018-05-04 上午11.13.21.png选择Call Directory Extension
屏幕快照 2018-05-04 上午11.16.26.png
选中主程序YSCallDemo这个target,找到App Group打开
勾选一个开发者team
1.jpg
选中用于号码识别的target同样找到App Group打开
勾选一个开发者team
75E093D0839AA5089656BA7C29B218CA.jpg
两个target都选中后会多了一个YSCallDemo.entitlements
屏幕快照 2018-05-08 下午11.24.06.png
有时候会出现文件找不到的情况,可以在target-->build phases里手动添加编译文件
image.png二、准备号码库,更新号码识别库就可以号码识别了
//这里是采用realm数据库来存储号码,路径选择共享路径即可
- (void)realmConfiguation {
NSURL *url = [[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.yasha.group"] URLByAppendingPathComponent:@"IDCall"] URLByAppendingPathExtension:@"realm"];
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
// APP Group 宿主程序数据库共享
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.fileURL = [[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.yasha.group"] URLByAppendingPathComponent:@"IDCall"] URLByAppendingPathExtension:@"realm"];
[RLMRealmConfiguration setDefaultConfiguration:configuration];
NSLog(@"数据库地址:%@", [RLMRealmConfiguration defaultConfiguration].fileURL);
// 数据迁移
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
}
更新号码识别库
//调用这个方法就可以将数据写入到号码识别库
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance];
[manager reloadExtensionWithIdentifier:@"yasuo.YSCallDemo.CallExtension" completionHandler:^(NSError * _Nullable error) {
}
调用更新后程序自动执行CallDirectoryHandler.m的方法
- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context {
context.delegate = self;
[self addOrRemoveIncrementalIdentificationPhoneNumbersToContext:context];
//这里直接调用号码增加和移除的方法
[context completeRequestWithCompletionHandler:nil];
}
调用这个方法数据写入
(void)addOrRemoveIncrementalIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context {
//配置数据库
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
configuration.fileURL = [[[[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:@"group.com.yasha.group"] URLByAppendingPathComponent:@"IDCall"] URLByAppendingPathExtension:@"realm"];
[RLMRealmConfiguration setDefaultConfiguration:configuration];
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
RLMResults *results = [[YSContactModel allObjects] sortedResultsUsingKeyPath:@"phone" ascending:YES];
[realm commitWriteTransaction];
if (results.count != 0) {
[context removeAllIdentificationEntries];
NSMutableArray *allPhoneNumbersArray = [NSMutableArray array];
NSMutableArray *labelsArray = [NSMutableArray array];
for (YSContactModel *model in results) {
[allPhoneNumbersArray addObject:[NSString stringWithFormat:@"%zd",model.phone ]];
[labelsArray addObject:model.name];
}
for (NSUInteger i = 0; i < allPhoneNumbersArray.count; i ++) {
CXCallDirectoryPhoneNumber phoneNumber = [allPhoneNumbersArray[i] longLongValue];
NSString *label = labelsArray[I];
[context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:label];
}
}
}
这里就可以实现号码识别了
三、常见错误
号码存入时需要加国家区号,要升序排列
比如com.apple.CallKit.error.calldirectorymanager Code=4 表示数据重复
授权没打开也会报,Domain=com.apple.CallKit.error.calldirectorymanager Code=6
官方
网上找的swift版本的报错枚举
public enum Code : Int {
public typealias _ErrorType = CXErrorCodeCallDirectoryManagerError
case unknown
case noExtensionFound
case loadingInterrupted
case entriesOutOfOrder
case duplicateEntries
case maximumEntriesExceeded
case extensionDisabled
@available(iOS 10.3, *)
case currentlyLoading
@available(iOS 11.0, *)
case unexpectedIncrementalRemoval
}
三、通过cocoapod进来的第三方和让共享target可以使用的方法
第一步:
PROJECT --info --configurations,将对应的Debug和Release 设置成pods-你的Xcode项目名.debug和pods-你的Xcode项目名.release。
这些是在下图中可以选择的,不需要手动填写。!
29BCA5E97E3FD86DE1CC6C8AFC10D768.jpg
31A227EF2AD6879836E434A72CC3F038.jpg
第二步:
网友评论