打电话回来后,从notificationCenter收到通知 [英] get a notification from notificationCenter after returning from a phone call

查看:100
本文介绍了打电话回来后,从notificationCenter收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击通话按钮并拨打电话然后返回到应用程序之后执行一项操作.

I want to do an action after the user tapped on the call button and made a call then returned to the app.

这是我拨打电话的功能:

This is my function for making a phone call :

let phoneURL = URL(string: String(format: "tel://%@", phoneNumber.englishNumbers))
UIApplication.shared.open(phoneURL!)

并且我在viewDidLoad()CallView上设置了一个观察者,如下所示:

and I have set an observer on CallView in viewDidLoad() like this:

NotificationCenter.default.addObserver(self, selector: #selector (showFeedBack), name: UIApplication.didEnterBackgroundNotification, object: nil)

拨打电话并按下结束"按钮(结束通话的红色按钮)后. CallView将会出现,但通知不会被调用.

After I made the call and pressed on the End button (the red button which ends the call). the CallView would appear but the notification won't get called.

我使用正确的通知吗?还是这是检测用户何时通过您的应用拨打电话并返回的正确方法?

Am I using the right notification? or is this the correct approach for detecting when a user made a phone call through your app and came back?

P.S.我已经使用了willResignActiveNotification通知.但它会在拨打电话之前(在出现警报且用户尚未选择任何内容之前)发送通知.

P.S. I have used willResignActiveNotification notification. but it send the notification before even making the call (when the alert is appeared and the user has not select anything yet)

推荐答案

您可以使用CXCallObserver监听如下的呼叫事件,

You can use CXCallObserver to listen the call events as below,

import CallKit

class ViewController: UIViewController, CXCallObserverDelegate {

    let co = CXCallObserver()

    override func viewDidLoad() {
        super.viewDidLoad()

        co.setDelegate(self, queue: DispatchQueue.main)
    }

    func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
        if call.hasEnded {
            print("Call is ended")
        }
        if call.hasEnded == false && call.hasConnected {
            print("Call is connected")
        }
        if call.isOutgoing {
            print("This is an outgoing call")
        } else if call.hasEnded == false && call.hasConnected == false {
            print("This is an incoming call")
        }
    }
}

这篇关于打电话回来后,从notificationCenter收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆