How to get incoming/outgoing call event in background state

心已入冬 提交于 2019-12-01 23:12:01

问题


In one of my app it has a feature of playing sound that I achieved successfully. Even though when app is running (foreground state) and we received the incoming call, app music gets stopped and resume again when call gets disconnected.

Now real problem is here. When app enters in the background state, we are not receiving any event for incoming/outgoing call. In the background mode If music is playing inside my app and we get any incoming call, then app music is stopped automatically but not resume again when call disconnected unlike iPhone Music app.

Is it a limitation of the iOS or can we achieve that ?

Note: I'm not looking for any solution for Jailbreak devices or Enterprise apps


回答1:


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.



来源:https://stackoverflow.com/questions/38696151/how-to-get-incoming-outgoing-call-event-in-background-state

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