2017-02-16 72 views
2

我試圖根據通話狀態更改按鈕狀態。 我用這裏的代碼來檢測呼叫狀態: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 
     } 
    } 

回答

2

最後,我解決了這個問題!我使用的代碼從這樣的回答:Find if user is in a call or not?

我從AppDelegate刪除一切,都是工作在ViewController完成:

override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     NotificationCenter.default.addObserver(
      self, 
      selector: #selector(cameBackFromSleep), 
      name: NSNotification.Name.UIApplicationDidBecomeActive, 
      object: nil 
     ) 

... 

    } 

    private func isOnPhoneCall() -> Bool 
    { 
     let callCntr = CTCallCenter() 

     if let calls = callCntr.currentCalls 
     { 
      for call in calls 
      { 
       if call.callState == CTCallStateConnected || call.callState == CTCallStateDialing || call.callState == CTCallStateIncoming 
       { 
        print("In call") 
        return true 
       } 
      } 
     } 

     print("No calls") 
     return false 
    } 

    func cameBackFromSleep() 
    { 

     self.viewWillAppear(true) 
    } 

    override func viewWillAppear(_ animated: Bool) 
    { 
     print("is on call", isOnPhoneCall()) 
     switch isOnPhoneCall() 
     { 
     case true: 
      print("startBtnAnimation") 
      startBtnAnimation() 
      recordBtnIsPressed = true 
     case false: 
      print("stopBtnAnimation") 
      stopBtnAnimation() 
      recordBtnIsPressed = false 
     default: break 
     } 
    } 

現在它工作正常。不知道爲什麼CTCallCenterAppDelegate工作如此奇怪。