去年蘋果推出了一套CallKit框架,用於第三方VOIP通話能疾速調用手機本地零碎通話界面,在體驗上可謂是一大提高.所以我已經著手的項目被要求在IOS 10以上的零碎完成對CallKit框架的運用,於是研討了大半月,總算初步搞定了一些功用的完成.
先強調一點,要想完成近程推送能拉起CallKit界面,首先必需要集成蘋果在IOS 8推出的推送框架PushKit,由於只要PushKit能在推送抵達時將APP從底層喚醒,普通推送無法完成.而只要底層喚醒了APP,才干經過APP的代碼邏輯完成CallKit的調用從而展現出原生通話界面.否則音訊抵達時只會展現普通的推送而不能像電話到來時一樣翻開原生接聽界面.
第一步,需求先集成PushKit,在app啟動時的代理辦法中注冊PushKit(沒有近程推送直接翻開原生接聽界面的需求的可以直接跳到第二步)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //以下注冊代碼及詳細證書配置可以在蘋果PushKit官方文檔中找到,在此不做贅述,詳見:https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-IOS/OptimizeVoIP.html#//apple_ref/doc/uid/TP40015243-CH30-SW1 dispatch_queue_t mainQueue = dispatch_get_main_queue(); PKPushRegistry * voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue]; voipRegistry.delegate = self; voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; }
同時,PushKit提供了代理辦法來確認token能否注冊成功
#pragma mark --PKPushRegistryDelegate - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type; { NSLog(@"credentialsToken=%@",credentials.token); }
第二步,集成CallKit.首先在Project-->Build Phases-->Link Binary With Libraries中添加CallKit.framework
#import <CallKit/CallKit.h> @interface:MessageManager() @property(nonatomic,strong)CXProvider * provider; @end
在MessageManager收到音訊時調用辦法receiveCallWithName
-(void)receiveCallWithName:(NSString *)name { CXCallUpdate *callUpdate = [[CXCallUpdate alloc] init]; NSString *remoteNickName = name; if (!remoteNickName) { remoteNickName = @"未知號碼"; } [callUpdate setLocalizedCallerName:remoteNickName]; [callUpdate setHasVideo:[self isVideoCall]]; CXHandle *calleeHandle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:remoteNickName]; [callUpdate setRemoteHandle:calleeHandle]; [_provider reportNeWincomingCallWithUUID:[NSUUID UUID] update:callUpdate completion:^(NSError *error){ completion(error); }]; }在Call完畢後,調用finishCallWithReason
-(void)finishCallWithReason:(CXCallEndedReason)reason { [_provider reportCallWithUUID:[NSUUID UUID] endedAtDate:nil reason:reason]; }
另外:CallKit也有許多代理辦法可供完成,辨別對應不同狀況下的處置,並不復雜,按需完成即可.
#pragma mark - CXProviderDelegate /// Called when the provider has been reset. Delegates must respond to this callback by cleaning up all internal call state (disconnecting communication channels, releasing.network resources, etc.). This callback can be treated as a request to end all calls without the need to respond to any actions - (void)providerDidReset:(CXProvider *)provider { NSLog(@"CK: Provider did reset"); } /// Called when the provider has been fully created and is ready to send actions and receive updates - (void)providerDidBegin:(CXProvider *)provider { NSLog(@"CK: Provider did begin"); } - (BOOL)provider:(CXProvider *)provider executeTransaction:(CXTransaction *)transaction { NSLog(@"CK: Provider execute transaction"); return NO; } // If provider:executeTransaction:error: returned NO, each perform*CallAction method is called sequentially for each action in the transaction - (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallAction *)action { NSLog(@"CK: Start Call Action"); [action fulfill]; } - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action { NSLog(@"CK: Answer Call Action"); [action fulfill]; } - (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action { NSLog(@"CK: End Call Action"); [action fulfill]; } - (void)provider:(CXProvider *)provider performSetHeldCallAction:(CXSetHeldCallAction *)action { NSLog(@"CK: Set Held Call Action"); [action fulfill]; } - (void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCallAction *)action { NSLog(@"CK: Set Muted Call Action"); [action fulfill]; } - (void)provider:(CXProvider *)provider performSetGroupCallAction:(CXSetGroupCallAction *)action { NSLog(@"CK: Set Group Call Action"); [action fulfill]; } - (void)provider:(CXProvider *)provider performPlayDTMFCallAction:(CXPlayDTMFCallAction *)action { NSLog(@"CK: Play DTMF Call Action"); [action fulfill]; } /// Called when an action was not performed in time and has been inherently failed. Depending on the action, this timeout may also force the call to end. An action that has already timed out should not be fulfilled or failed by the provider delegate - (void)provider:(CXProvider *)provider timedOutPerformingAction:(CXAction *)action { NSLog(@"CK: Provider Timed out"); } /// Called when the provider's audio session activation state changes. - (void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession *)audioSession { NSLog(@"CK: Audio session activated"); } - (void)provider:(CXProvider *)provider didDeactivateAudioSession:(AVAudioSession *)audioSession { NSLog(@"CK: Audio session deactivated"); }
【iOS 初探PushKit集成與CallKit集成】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!