CallKit 這個開發框架,能夠讓語音或視訊電話的開發者將 UI 界面整合在 iPhone 原生的電話 App 中.將允許開發者將通訊 App 的功能內建在電話 App 的“常用聯絡資訊”,以及“通話記錄”,方便用戶透過原生電話 App,就能直接取用這些第三方功能;允許用戶在通知中心就能直接浏覽並回覆來電,來電的畫面也將整合在 iOS 原生的 UI 裡,總體來說,等於讓 iOS 原本單純用來打電信電話的“電話”功能,能夠結合眾多第三方語音通訊軟件,具備更完整的數碼電話潛力。CallKit 也拓展了在 iOS 8 就出現的 App Extensions 功能,可以讓用戶在接收來電時,在原生電話 App 中就透過第三方 App 辨識騷擾電話(例如詐騙).
出現下圖結構說明創建成功:
注意:
cccccccccalldemo.xcdatamodeld必須創建;
通過extension模板,創建CallDirectoryExtension
用到的方法:
//開始請求的方法,在打開設置-電話-來電阻止與身份識別開關時,系統自動調用 - (void)beginRequestWithExtensionContext:(CXCallDirectoryExtensionContext *)context; //添加黑名單:根據生產的模板,只需要修改CXCallDirectoryPhoneNumber數組,數組內號碼要按升序排列 - (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context; // 添加信息標識:需要修改CXCallDirectoryPhoneNumber數組和對應的標識數組;CXCallDirectoryPhoneNumber數組存放的號碼和標識數組存放的標識要一一對應,CXCallDirectoryPhoneNumber數組內的號碼要按升序排列 - (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context;設置
在打開設置裡的開關後,系統會調用beginRequest方法,在這個方法內部會調用添加黑名單和添加信息標識的方法,添加成功後,再調用completeRequestWithCompletionHandler方法通知系統;
sms:或者是sms://:發送短信;
tel: 或者是tel://:打電話
telprompt:或者是 telprompt://: 打電話;
mailto:發送郵件;
http:或者是 http://: 浏覽網址;
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.noTextField.text]; [self.callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]]; if (!self.callWebview.subviews) { [self.view addSubview:_callWebview]; }
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance]; // 獲取權限狀態 [manager getEnabledStatusForExtensionWithIdentifier:@"com.tq.cccccccccalldemo.CallDirectoryExtension" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) { if (!error) { NSString *title = nil; if (enabledStatus == CXCallDirectoryEnabledStatusDisabled) { /* CXCallDirectoryEnabledStatusUnknown = 0, CXCallDirectoryEnabledStatusDisabled = 1, CXCallDirectoryEnabledStatusEnabled = 2, */ title = @"未授權,請在設置->電話授權相關權限"; }else if (enabledStatus == CXCallDirectoryEnabledStatusEnabled) { title = @"授權"; }else if (enabledStatus == CXCallDirectoryEnabledStatusUnknown) { title = @"不知道"; } UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; }else{ UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"有錯誤" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } }];
注意:
電話號碼前要加區號:+86;
/** 添加黑名單:無法接通 */- (BOOL)addBlockingPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context/** 添加信息標識 */- (BOOL)addIdentificationPhoneNumbersToContext:(CXCallDirectoryExtensionContext *)context
說明:
將項目運行到真機之後,還需要在“設置->電話”設置應用的權限;
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance]; // 獲取權限狀態 [manager getEnabledStatusForExtensionWithIdentifier:@"com.tq.cccccccccalldemo.CallDirectoryExtension" completionHandler:^(CXCallDirectoryEnabledStatus enabledStatus, NSError * _Nullable error) { if (!error) { NSString *title = nil; if (enabledStatus == CXCallDirectoryEnabledStatusDisabled) { /* CXCallDirectoryEnabledStatusUnknown = 0, CXCallDirectoryEnabledStatusDisabled = 1, CXCallDirectoryEnabledStatusEnabled = 2, */ title = @"未授權,請在設置->電話授權相關權限"; }else if (enabledStatus == CXCallDirectoryEnabledStatusEnabled) { title = @"授權"; }else if (enabledStatus == CXCallDirectoryEnabledStatusUnknown) { title = @"不知道"; } UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; }else{ UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"有錯誤" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } }];
項目基本可以達到需求了!黑名單的號碼打進來是在通話中,標記的號碼顯示標記的名字;
標記的號碼這時你會發現你只有第一次運行項目的號碼設置才起作用,或者是去設置裡面重新授權;顯然這是不行的;我們需要實時更新號碼:
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance]; [manager reloadExtensionWithIdentifier:@"com.tq.cccccccccalldemo.CallDirectoryExtension" completionHandler:^(NSError * _Nullable error) { if (error == nil) { UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"更新成功" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; }else{ UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"更新失敗" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; } }];