Search code examples
iosobjective-cxcodecallkit

How To use CallKit in Objective C


I am Go through with the WWDC CallKit session and I am comfortable with the Concept of it, But don't know how to start.

Also, I have Following the Sample Code for the CallKit by Apple Developers i.e SpeakerBox. But this one is in Swift.

Need suggestion!!

Thanks in Advance


Solution

  • Here you go. There is a full example in Objective c.

    For reference: https://github.com/naandonov-mm/iOS-10-Sampler/tree/master/CallKit

    The CallKit framework provides programmatic access to VoIP functionality, as well as call blocking and identification. Note: This sample requires a device to be built on.

    To Start first add the following code in App delegate.

     #import <Intents/Intents.h>
     #import <PushKit/PushKit.h>
     @interface AppDelegate () <PKPushRegistryDelegate>
    
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
         // Override point for customization after application launch.
         PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
         pushRegistry.delegate = self;
         pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
    
        return YES;
    }
    

    Then add following methods in app delegate as well.

     - (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler {
        if ([userActivity.interaction.intent isKindOfClass:[INStartAudioCallIntent class]]) {
            INPerson *person = [[(INStartAudioCallIntent*)userActivity.interaction.intent contacts] firstObject];
            NSString *phoneNumber = person.personHandle.value;
            CallViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"CallViewController"];
            viewController.phoneNumber = phoneNumber;
            UIViewController *mainViewController = self.window.rootViewController;
            [mainViewController presentViewController:viewController animated:YES completion:nil];
        }
        return YES;
    }
    
    #pragma mark - PKPushRegistryDelegate
    
     - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
        if([credentials.token length] == 0) {
            NSLog(@"voip token NULL");
            return;
        }
    }
    
    - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
        NSString *uuidString = payload.dictionaryPayload[@"UUID"];
        NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidString];
        NSString *phoneNumber = payload.dictionaryPayload[@"PhoneNumber"];
        CallViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"CallViewController"];
        viewController.phoneNumber = phoneNumber;
        viewController.isIncoming = YES;
        viewController.uuid = uuid;
        UIViewController *mainViewController = 
        self.window.rootViewController;
        [mainViewController presentViewController:viewController animated:YES completion:nil];
    }
    

    enter image description here