How to get incoming/outgoing call event in backgro

2019-02-25 04:35发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-02-25 04:55

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.

查看更多
登录 后发表回答