从 phone 呼叫登录 iOS 进行 voip 呼叫

make voip call from phone call log in iOS

在我的 Voip 应用程序中,我使用 callkit 来接收来电。 通过使用以下方法:

-(void)reportIncomingCall:(NSUUID*)UDID handle:(NSString*)handle{

我可以在我的 iPhone 本机呼叫应用程序的呼叫历史记录中看到此来电的日志。

我想从 iPhone 本机呼叫应用程序拨出电话。我为 WhatsApp、hangout 等应用程序工作。但是,当我尝试从来电记录中呼叫用户时,无法唤醒我的应用程序。

- (NSUUID *)reportOutgoingCallContactIdentifier:(NSString *)identifier destination:(NSString *)name telNumber:(NSString *)telnum 

基本上,您需要为您的目标创建 Intents Extension,以便处理本机通话记录中的音频通话。

在Xcode中:

File -> New -> Target

Select 意图扩展

这是扩展的主要部分 class,它应该是这样的

class IntentHandler: INExtension, INStartAudioCallIntentHandling {

func handle(intent: INStartAudioCallIntent, completion: @escaping (INStartAudioCallIntentResponse) -> Void) {
    let response: INStartAudioCallIntentResponse
    defer {
        completion(response)
    }

    // Ensure there is a person handle
    guard intent.contacts?.first?.personHandle != nil else {
        response = INStartAudioCallIntentResponse(code: .failure, userActivity: nil)
        return
    }

    let userActivity = NSUserActivity(activityType: String(describing: INStartAudioCallIntent.self))

    response = INStartAudioCallIntentResponse(code: .continueInApp, userActivity: userActivity)
    } 
}

然后,您需要为您的应用程序提供 URLScheme,以便在应用程序委托收到打开时启动 VoIP 呼叫URL

这是 NSUserActivity 的扩展,可帮助您检测开始呼叫意图

import Intents

@available(iOS 10.0, *)
protocol SupportedStartCallIntent {
    var contacts: [INPerson]? { get }
}

@available(iOS 10.0, *)
extension INStartAudioCallIntent: SupportedStartCallIntent {}

@available(iOS 10.0, *)
extension NSUserActivity {

    var startCallHandle: String? {
        guard let startCallIntent = interaction?.intent as? SupportedStartCallIntent else {
            return nil
        }
        return startCallIntent.contacts?.first?.personHandle?.value
    }

}

您还需要在目标设置中注册您的 URL 方案:

Xcode目标设置,select信息卡,底部URL类型,select+ 添加新类型并为其命名,例如 VoIPCall

AppDelegate重写下面的函数

func application(_ application: UIApplication,
               continue userActivity: NSUserActivity,
               restorationHandler: @escaping ([Any]?) -> Void) -> Bool {

    if userActivity.startCallHandle {
        // START YOUR VOIP CALL HERE ----
    }
    return true
}

for swift 5 你应该将 continueuseractivity 函数定义为 SceneDelegate.swift

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {

    let interaction = userActivity.interaction
    if let startAudioCallIntent = interaction?.intent as? INStartAudioCallIntent{

        let contact = startAudioCallIntent.contacts?.first
    
        let contactHandle = contact?.personHandle

            if let phoneNumber = contactHandle?.value {
               print(phoneNumber)
            //Your call logic
            }
 
    }
    return
}

请参考这个