我正在尝试使用我的应用程序使用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)
}