当应用程序处于后台时,使用CallKit检测来电

时间:2018-04-04 11:08:14

标签: ios swift4 callkit

我正在尝试使用我的应用程序使用CallKit检测来电,但当应用程序进入后台时它无法正常工作。当应用程序变为后台时,有没有办法检测来电。也尝试使用notificationcenter,但它无法正常工作。

这是我提到的代码。

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(cameFromBackground),
            name: NSNotification.Name.UIApplicationWillResignActive,
            object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(_ animated: Bool) {
        print("Call Status - ", phoneCallStatus())
        switch phoneCallStatus()
        {
        case true:
            print("Incomming")
        case false:
            print("OnGoing")
            default: break
        }
    }

    @objc private func phoneCallStatus() -> Bool
    {
        let callCntr = CXCallObserver()
        let calls = callCntr.calls

        for call in calls{
            if call.isOutgoing == false  {
                print("Incomming")
                return true
            }
        }
        return false
    }

    @objc func cameFromBackground()
    {
        self.viewWillAppear(true)
    }

0 个答案:

没有答案