B
B
belenerd2017-02-20 08:02:15
iOS
belenerd, 2017-02-20 08:02:15

Swift iOS How to track call status in background?

I am trying to change the state of a button in the application depending on the state of the current call (if there are no calls in the connected state, the button animation is not active, if there is, it is active). To track the state of the call, I used the code from here: How to get a call event using CTCallCenter:setCall... . Everything works fine while the application is in the foreground. But after it goes into the background, call tracking stops.
The documentation for CTCallCenter.callEventHandlerit says:

When your application resumes the active state, it receives a single call event for each call that changed state—no matter how many state changes the call experienced while your application was suspended. The single call event sent to your handler, upon your application returning to the active state, describes the call's state at that time.

But when I return to the active state, I do not receive any call events, the last state of the call is saved when the application was still in the foreground. What could be the problem, how to track the status of the call in the background?
My code:
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
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
belenerd, 2017-03-28
@belenerd

Maybe it will be useful for someone:
stackoverflow.com/questions/42267239/ios-how-to-de...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question