我試圖根據通話狀態更改按鈕狀態。 我用這裏的代碼來檢測呼叫狀態:How to get a call event using CTCallCenter:setCallEventHandler: that occurred while the app was suspended?iOS如何檢測背景中的呼叫狀態?
而且它在應用程序處於前景時工作正常。但它根本不適用於背景。在CTCallCenter.callEventHandler
文檔:
當你的應用程序恢復激活狀態時,它接收爲改變國家無論多少國家 如何改變呼叫經歷,而你的應用程序被暫停每次調用一個 呼叫事件。當您的應用程序 返回到活動狀態時,將發送到您的處理程序的單個呼叫事件描述呼叫在該時間的狀態。
但是當應用恢復激活時,我沒有收到任何通話事件。我所得到的是應用程序在前臺時的最後保存的呼叫狀態。我如何檢測後臺的通話狀態?
這裏是我的代碼:
AppDelegate.swift
let callСenter = CTCallCenter()
func block (call:CTCall!)
{
callState = String(call.callState)
print(call.callState)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
//check for call state
callСenter.callEventHandler = block
...
return true
}
ViewController.swift
override func viewDidLoad()
{
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(cameBackFromSleep),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil
)
...
}
func cameBackFromSleep()
{
self.viewWillAppear(true)
}
override func viewWillAppear(_ animated: Bool)
{
switch callState
{
case "CTCallStateConnected":
print("callState: ", callState)
self.textLabel.isHidden = true
startBtnAnimation()
case "CTCallStateDisconnected":
print("callState: ", callState)
self.textLabel.center.y += self.view.bounds.height
self.textLabel.isHidden = false
stopBtnAnimation()
default: break
}
}