iOS CallKit技术在APP中的应用方案

CallKit和VoIP在APP中的应用

CallKit framework)是苹果在2016年推出iOS10系统时的新功能,可以调起系统的接听页进行音视频通话。目前市面上使用该方案的的APP不是很多,所有中国区在App Store上架的App都不能支持CallKit功能,官方文档和网上资料对相关功能的细节介绍都很有限。

为什么CallKit被禁用?因为苹果动了不该动的蛋糕!

讲CallKit之前不得不先说一下VoIP,VoIP是什么?

VoIP()是一种新的push通知类型,是苹果在iOS8中新引入的PushKit框架。

VoIP push和普通APNS push有什么区别?

最明显的区别就是:APP收到VoIP push消息时会直接将已经杀掉的APP激活!

这两个库配合使用,即可形成了一套APP在杀死的情况下,收到音视频邀请时持续响铃的完整解决方案。

PushKit

PushKit的使用有3步操作,首先遵守协议:

1.注册, 在APP启动代码里,通过PKPushRegistry注册VoIP服务
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    PKPushRegistry *pushRegistry = [[PKPushRegistry alloc]  initWithQueue:dispatch_get_main_queue()];
    pushRegistry.delegate = self;
    pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
    return YES;
}
2. 获取token,协议方法里面获取token并上传给对应的服务,通过Token来进行个推的
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
   // 一般做法
   // NSString *str = [NSString stringWithFormat:@"%@",credentials.token];
   // NSString *tokenStr = [[[str stringByReplacingOccurrencesOfString:@"<" withString:@""]
                           stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
}
3. 接收VoIP消息,收到VoIP消息,调用CallKit,做剩余的通讯逻辑处理
// iOS11之前收到VoIP推送后执行的回调 
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
    [self handleVoipPushMessageWithPayload:payload forType:type];
}

// iOS11之后收到VoIP推送后执行的回调 
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion {
    [self handleVoipPushMessageWithPayload:payload forType:type];
    if (completion) {
        completion();
    }
}
其他...
// token失效时回调,一般只做打印处理
- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(PKPushType)type {
    NSLog(@"之前提供的token失效,则调用此方法");
}

- (void)handleVoipPushMessageWithPayload:(PKPushPayload *)payload forType:(PKPushType)type {
    //开启后台任务(实践发现如果不开启后台任务,调不起系统的CallKit接听界面)
    [self startBgTask];
    NSDictionary *dic = payload.dictionaryPayload;
   // 这里调用CallKit
}

// 开启后台延时
- (void)startBgTask {
    UIApplication *application = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier bgTask;
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:bgTask];
    }];
}

CallKit

锁屏状态下样式
未锁屏状态下样式

CallKit主要有:CXProvider、CXCallController 2个核心类
CXProvider类用于被叫流程,主要负责系统-->APP的信息、状态传递
CXCallController类用于主叫流程,主要负责APP-->系统的信息、状态传递

因为APP业务只用到被叫功能,所以下面只讲被叫流程。

CXProvider

主要作用有三个:

  • 调起系统的电话接听页面
  • 销毁系统的电话接听页面
  • 代理方法中接收用户的操作事件,如接听、挂断..

APP内是如何调起系统的接听页面的?

    self.callUpdate.remoteHandle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:self.channelId];;
    self.callUpdate.hasVideo = NO;
    self.callUpdate.localizedCallerName = self.netCallDict[SFIMVoip_FromName];
    __weak __typeof(self) wself = self;
    [self.provider reportNewIncomingCallWithUUID:[NSUUID UUID]
                                          update:self.callUpdate
                                      completion:^(NSError *_Nullable error) {
        if (error) {
            NSLog(@"通话创建失败  current error %@", error.userInfo);
            //通话创建失败
            [wself resetVariableData];
        }
    }];

调起CallKit接听页面过程中,用到了CXHandle、CXCallUpdate、CXProvider、CXProviderConfiguration四个类

CXProvider的初始化及代理设置

        self.provider = [[CXProvider alloc] initWithConfiguration:self.configuration];
        [self.provider setDelegate:self queue:nil];

CXProvider的详细用法

//初始化方法 使用CXProviderConfiguration来进行配置
- (instancetype)initWithConfiguration:(CXProviderConfiguration *)configuration;

//设置代理与代理函数所工作的线程
- (void)setDelegate:(nullable id)delegate queue:(nullable dispatch_queue_t)queue;

//向系统发起一个新的通话请求
/*
UUID为此通话请求的标识 可以使用它来关闭通话
update设置界面的更新参数
*/
- (void)reportNewIncomingCallWithUUID:(NSUUID *)UUID update:(CXCallUpdate *)update completion:(void (^)(NSError *_Nullable error))completion;

//结束某个通话 使用上面的UUID作为标识
- (void)reportCallWithUUID:(NSUUID *)UUID endedAtDate:(nullable NSDate *)dateEnded reason:(CXCallEndedReason)endedReason;

CXProviderConfiguration

Provider的配置,例如设置通讯服务名称,铃声,图标

//设置服务名称
@property (nonatomic, readonly, copy) NSString *localizedName;
//设置铃声  资源必须在 app的 bundle里
@property (nonatomic, strong, nullable) NSString *ringtoneSound;
//设置应用图标
@property (nonatomic, copy, nullable) NSData *iconTemplateImageData;
//设置最大支持的组数 默认为2
@property (nonatomic) NSUInteger maximumCallGroups;
//设置最大的每组人数 默认为5
@property (nonatomic) NSUInteger maximumCallsPerCallGroup;
//设置是否将通话记录保存进最近通话列表
@property (nonatomic) BOOL includesCallsInRecents;
//设置是否支持视频通话
@property (nonatomic) BOOL supportsVideo;
//设置支持的操作类型
@property (nonatomic, copy) NSSet *supportedHandleTypes;

CXHandle中来定义操作的类型,通话对方的信息

//类型
/*
typedef NS_ENUM(NSInteger, CXHandleType) {
    CXHandleTypeGeneric = 1,//通用
    CXHandleTypePhoneNumber = 2,//电话
    CXHandleTypeEmailAddress = 3,//邮箱地址
} API_AVAILABLE(ios(10.0));
*/
@property (nonatomic, readonly) CXHandleType type;
//值
@property (nonatomic, readonly, copy) NSString *value;

- (instancetype)initWithType:(CXHandleType)type value:(NSString *)value 

CXCallUpdate类

有点类似CXProviderConfiguration,也是一些配置信息

//远程操作对象 如果是接收方 则此为呼叫方
@property (nonatomic, copy, nullable) CXHandle *remoteHandle;
//名称
@property (nonatomic, copy, nullable) NSString *localizedCallerName;
//是否支持暂时挂起
@property (nonatomic) BOOL supportsHolding;
//是否支持组
@property (nonatomic) BOOL supportsGrouping;
//是否支持非组通话
@property (nonatomic) BOOL supportsUngrouping;
//是否支持DTMF
@property (nonatomic) BOOL supportsDTMF;
//是否包含视频
@property (nonatomic) BOOL hasVideo;

可以动态更改provider的配置信息

- (void)reportCallWithUUID:(NSUUID *)UUID updated:(CXCallUpdate *)update;

CXProviderDelegate

按钮的回调方法(每个回调结束的时候要执行[action fulfill],如果逻辑处理异常则调用failtimeout函数提示通话失败)

// 点击接听按钮的回调
- (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action;
// 点击结束按钮的回调(结束或拒绝通话)
- (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action;

结束通话,销毁CallKit

[self.provider reportCallWithUUID:self.currentUUID endedAtDate:nil reason:CXCallEndedReasonRemoteEnded];

CallKit和VoIP的技术并不难,只是开发过程中调试比较麻烦、浪费时间。因为涉及到推送没法在模拟器上断点调试,只能打包真机安装验证,所以建议开发过程中一定要多加打印日志,因为后期只能通过日志观察、解决问题。

欢迎留言交流....

参考资料:
iOS10适配之 CallKit

你可能感兴趣的:(iOS CallKit技术在APP中的应用方案)