如何在 Objective C 中使用 CallKit
How To use CallKit in Objective C
我正在使用 WWDC CallKit session,我对它的概念很满意,但不知道如何开始。
另外,我遵循了 Apple 开发人员的 CallKit 示例代码,即 SpeakerBox。但是这个在 Swift.
需要建议!!
提前致谢
给你。 Objective c.
中有完整示例
供参考:https://github.com/naandonov-mm/iOS-10-Sampler/tree/master/CallKit
CallKit 框架提供对 VoIP 功能的编程访问,以及呼叫阻止和识别。
注意:此示例需要构建设备。
首先在 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;
}
然后在app delegate中也添加以下方法。
- (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];
}
我正在使用 WWDC CallKit session,我对它的概念很满意,但不知道如何开始。
另外,我遵循了 Apple 开发人员的 CallKit 示例代码,即 SpeakerBox。但是这个在 Swift.
需要建议!!
提前致谢
给你。 Objective c.
中有完整示例供参考:https://github.com/naandonov-mm/iOS-10-Sampler/tree/master/CallKit
CallKit 框架提供对 VoIP 功能的编程访问,以及呼叫阻止和识别。 注意:此示例需要构建设备。
首先在 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;
}
然后在app delegate中也添加以下方法。
- (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];
}