How to get incoming/outgoing call event in background state

有些话、适合烂在心里 提交于 2019-12-01 20:55:17

Have you tried to create call center and assign handler block in AppDelegate class? The following has to work.

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let callCenter: CTCallCenter = CTCallCenter()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()

        callCenter.callEventHandler = {

            (call: CTCall!) in

                switch call.callState {

                    case CTCallStateConnected:

                        print("CTCallStateConnected")

                    case CTCallStateDisconnected:

                        print("CTCallStateDisconnected")

                    case CTCallStateIncoming:

                        print("CTCallStateIncoming")

                    default:

                        print("default")

                }

        }

        return true

    }

}

Do not forget to switch on Background Modes for this. And perform something in the background as well, smth like receiving location.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!