年前各種項目驗收,總結,所以沒時間更新博客。年初,剛剛靜下來,這次文章,讓我們討論一下,iOS10新特性,來電識別和來電阻止
iOS 10中引入了許多新特性,其中 CallKit是一個非常重要的 API,The CallKit framework PRovides programmatic access to Voip functionality, as well as call blocking and identification. 這意味著現在可以通過 Call Directory Extension 來實現電話識別和黑名單功能了。本文簡單闡述了如果實現簡單的來電識別和黑名單功能。 Extension一直很輕量,單一的,這次的 Call Directory Extension 也不出例外,出奇的簡單。下面我們逐步完成今天的任務
Step1、 創建Call Directory Extension
在對應項目中的file->new->target,選擇application Extension中的Call Directory Extension,如圖:          輸入Extension項目名稱后,創建成功后會彈出這樣的提示框:
         輸入Extension項目名稱后,創建成功后會彈出這樣的提示框: 點擊激活,直接進入到Extension項目。
點擊激活,直接進入到Extension項目。
Step2、 來電識別和來電阻止
1、項目創建成功后,在項目目錄文件中,生成了Info.plist、CallDirectoryHandler.h、CallDirectoryHandler.m三個文件。我們需要關注的是CallDirectoryHandler.m。 2、打開CallDirectoryHandler.m,里面自動生成了四個方法
- (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context - (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context- (void)requestFailedForExtensionContext:(CXCallDirectoryExtensionContext *)extensionContext withError:(NSError *)error其中簡單的來電識別和來電阻止我們只要補全addIdentificationPhoneNumbersToContext和addBlockingPhoneNumbersToContext方法,在里面添加來電識別數據和黑名單數據就可以了。 3、來電識別
- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context { - CXCallDirectoryPhoneNumber phoneNumbers[] = {+8613533533110,+8613726735411,+86158140043377}; NSArray<NSString *> *labels = @[ @"Dear",@"陳老濕",@"乾坤"]; NSUInteger count = (sizeof(phoneNumbers) / sizeof(CXCallDirectoryPhoneNumber)); for (NSUInteger i = 0; i < count; i += 1) { CXCallDirectoryPhoneNumber phoneNumber = phoneNumbers[i]; NSString *label = labels[i]; [context addIdentificationEntryWithNextSequentialPhoneNumber:phoneNumber label:label]; } return YES;}注意點:1、電話號碼前要加區號:+86;2、電話號碼需要升序排列
4、來電阻止
- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context { CXCallDirectoryPhoneNumber phoneNumbers[] = {13726735412,18005555555 }; NSUInteger count = (sizeof(phoneNumbers) / sizeof(CXCallDirectoryPhoneNumber)); for (NSUInteger index = 0; index < count; index += 1) { CXCallDirectoryPhoneNumber phoneNumber = phoneNumbers[index]; [context addBlockingEntryWithNextSequentialPhoneNumber:phoneNumber]; } return YES;}注意點:1、電話號碼需要升序排列
上面簡單介紹來電識別和來電阻止,現在我們安裝app到手機,必須是64位的設備。然后進入設備的設置 —> 電話 —> 來電阻止和身份識別,開啟我們的 App即可。
到此為止,我們簡單介紹如何簡單實現來電阻止和身份識別,利用Call Directory Extension很簡單實現該功能,重點是如何編輯這個黑名單列表。列表數據項可能很多,并且數據可能是實時更新添加的,那應該怎么做才更好呢?這個需要大家的思考~~
利用業余時間看了官方文檔,簡單介紹一下,如有理解錯誤,望大家指出
新聞熱點
疑難解答