Simple application of iOS10–CallKit

The CallKit framework allows voice or video phone developers will UI interface integration in the native iPhone App. The phone will allow developers to the communication function of App built-in phone App “common contact information”, and “call records”, convenient for the user through the original phone App, with the third party function you can allow the user to directly take; the notification center can directly browse and answer the call, call the picture will also be integrated in iOS native UI, in general, equal to iOS originally used to play simple telecommunications telephone “telephone” function, can be combined with many third party voice communication software, with more complete digital telephone potential. CallKit has also expanded the App 8 Extensions features appear on the iOS, allowing users to receive calls, in the native phone App in the identification of harassing phone calls through the third party App (such as fraud)

How to create a call project?

Simple application of iOS10--CallKit
Simple application of iOS10--CallKit
Simple application of iOS10--CallKit
Simple application of iOS10--CallKit

The following diagram shows the success of the creation:

Simple application of iOS10--CallKit

Call reminder function

Through the extension template, the method used to create CallDirectoryExtension

Start / / method request, in the open set - Call - call block and identification switch, the system automatically calls - (void) beginRequestWithExtensionContext: (CXCallDirectoryExtensionContext * context); / / add blacklist: according to the template, only need to modify the CXCallDirectoryPhoneNumber array, an array of numbers in ascending order according to the (BOOL) - addBlockingPhoneNumbersToContext: (CXCallDirectoryExtensionContext * context); / / add information identification: the need to identify the array modified CXCallDirectoryPhoneNumber array and the corresponding number; CXCallDirectoryPhoneNumber array to store and label array logo to correspond to the number in the CXCallDirectoryPhoneNumber array in ascending order - (BOOL) addIdentificationPhoneNumbersToContext: (CXCallDirectoryExtensionContext * context);
Simple application of iOS10--CallKit

Implementation process

In the open switch settings, the system will call the beginRequest method, the method call add black list and add information identification in this method, after successfully adding, and then call the completeRequestWithCompletionHandler method to notify the system;

Code implementation:

  • SMS: or sms://: send text messages;
  • Tel: or tel://: call
  • Telprompt: or telprompt://: call;
  • Mailto: send mail;
  • Http: or http://: browse url;
Call button
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]};
Check authorization:
CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance]; / / get the permission state [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 = "@ * / unauthorized please, in the setting of -> the telephone authorization authority";}else if (e NabledStatus = = CXCallDirectoryEnabledStatusEnabled) {title = @ "authorization" (enabledStatus;}else if = = CXCallDirectoryEnabledStatusUnknown) {title = @ "do not know";} UIAlertController* alert = [UIAlertController alertControllerWithTitle:@ message:title preferredStyle:UIAlertControllerStyleAlert] UIAlertAction* "tip"; defaultAction = "OK" [UIAlertAction actionWithTitle:@ style:UIAlertActionStyleDefault handler:^ (UIAlertAction * action) {}]; [al ERT addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil];}else{UIAlertController* alert = [UIAlertController alertControllerWithTitle:@ "prompt" message:@ "errors" preferredStyle: UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@ "OK" style:UIAlertActionStyleDefault handler:^ (UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES com Pletion:nil];}]};
Implementation method of CallDirectoryHandler file:

Note: before the phone number to add the area code: +86; phone numbers need to be arranged in ascending order (Numbers must be in numerically ascending order.)

Add black list: / * * is * / - (BOOL) addBlockingPhoneNumbersToContext: (CXCallDirectoryExtensionContext * context) / * * add information ID * / - (BOOL) addIdentificationPhoneNumbersToContext: (CXCallDirectoryExtensionContext * context)

Note: after the project will run to a real machine, also need to set the -&gt phone application permissions set; “;

The project can basically meet the demand! The number of the black list is in the call, the marked number shows the marked name;

Simple application of iOS10--CallKit
marked number

At this point you will find that you have only the first run of the project number set to work, or to set up the re authorization; obviously this is not enough; we need to update the number in real time:

CXCallDirectoryManager *manager = [CXCallDirectoryManager sharedInstance]; [manager reloadExtensionWithIdentifier:@ "com.tq.cccccccccalldemo.CallDirectoryExtension" completionHandler:^ (NSError * _Nullable error) {if (error = = Nil) {UIAlertController* alert = [UIAlertController alertControllerWithTitle:@ "prompt" message:@ "updated" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@ style: UIAlertActionStyleDefault handler:^ ("OK" UIAlertAct Ion * action) {}] [alert addAction:defaultAction]; [self presentViewController:alert; animated:YES completion:nil];}else{UIAlertController* alert = [UIAlertController alertControllerWithTitle:@ "prompt" message:@ "failed to update" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@ "OK" style:UIAlertActionStyleDefault handler:^ (UIAlertAction * action) {}]; [alert addAction:defaultAction]; [self presen TViewController:alert animated:YES completion:nil];}}];
Simple application of iOS10--CallKit