先導(dǎo)入 Call Directory Extension
導(dǎo)入完成后趣倾,項(xiàng)目會(huì)多了一個(gè)文件夾
文件夾里的文件是CallKit 中 來(lái)電識(shí)別功能 添加黑名單功能
的實(shí)現(xiàn)模版
發(fā)現(xiàn)問(wèn)題:
1菲饼,addBlockingPhoneNumbersToContext
谎势、addIdentificationPhoneNumbersToContext
厨剪,這兩個(gè)方法中 承載電話號(hào)的 數(shù)據(jù)類型是CXCallDirectoryPhoneNumber
, 這個(gè)類型是int64_t
類型,也是long long
類型兽间,所以大家在做類型轉(zhuǎn)換的時(shí)候需要注意卖氨。
2朝刊,如何動(dòng)態(tài)修改CXCallDirectoryPhoneNumber數(shù)組和對(duì)應(yīng)的標(biāo)識(shí)數(shù)組酬屉?
因?yàn)樗拗鞒绦?和 擴(kuò)展程序的數(shù)據(jù) 是隔離的半等,所以我們需要在宿主程序和 擴(kuò)展程序都 開(kāi)啟 APP Groups
宿主程序和 擴(kuò)展程序都設(shè)置完 APP Groups 就可以共享數(shù)據(jù)了揍愁。
在宿主程序中把從服務(wù)端請(qǐng)求回來(lái)的最新數(shù)據(jù),寫入到NSUserDefaults
中杀饵,我用來(lái)電識(shí)別功能舉個(gè)?? 莽囤,如下:
NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.testCallKit"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
[dict setObject:@"歐弟122" forKey:@"8615657138888"];
[userDefault setObject:dict forKey:@"PhoneNumbers"];
[userDefault synchronize];
上面代碼 需要注意的是 :
1, SuiteName
的名字 是剛才設(shè)置 APP Groups 時(shí)創(chuàng)建的切距。
2朽缎,電話號(hào)碼前面要加 區(qū)號(hào) ,中國(guó)大陸的區(qū)號(hào)就是 86 或者 +86谜悟。
回到 CallDirectoryHandler.m
更改如下:
- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context {
NSDictionary *dict = [[[NSUserDefaults alloc] initWithSuiteName:@"group.testCallKit"] objectForKey:@"PhoneNumbers"];
NSMutableDictionary *incomingTelegramInfo = @{}.mutableCopy;
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
incomingTelegramInfo[@([key longLongValue])] = obj;
}];
for (NSNumber *phoneNumber in [incomingTelegramInfo.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
NSString *label = incomingTelegramInfo[phoneNumber];
[context addIdentificationEntryWithNextSequentialPhoneNumber:(CXCallDirectoryPhoneNumber)[phoneNumber unsignedLongLongValue] label:label];
}
return YES;
}
那么如何維護(hù)號(hào)碼庫(kù)呢话肖?
經(jīng)調(diào)研發(fā)現(xiàn),目前非越獄手機(jī)沒(méi)有 獲取來(lái)電號(hào)碼 的權(quán)限赌躺,所以就不能用 單個(gè)來(lái)電號(hào)碼 去云端數(shù)據(jù)庫(kù)比對(duì)處理狼牺,只能通過(guò)第三方應(yīng)用 把號(hào)碼數(shù)據(jù)庫(kù)寫入到手機(jī)本地羡儿,當(dāng)來(lái)電時(shí) 系統(tǒng)自動(dòng)去和本地號(hào)碼庫(kù)比對(duì) 并做出身份顯示礼患。
機(jī)制如下:
我也咨詢了做安卓的童鞋,如何在來(lái)電時(shí)觸發(fā)第三方應(yīng)用掠归,他提供了 泵宓活進(jìn)程 的思路。我這里就不過(guò)多闡述了虏冻》袅唬可以參考Android來(lái)電號(hào)碼獲取代碼