How to receive an incoming / outgoing call in the background

In one of my applications, it has a sound reproduction function, which I have achieved success. Even if the application is running (foreground state) and we have received an incoming call, the music of the application stops and resumes again when the call is disconnected.

Now the real problem is here. When the application enters the background state, we do not receive any event for an incoming / outgoing call. In the background If music is playing inside my application and we receive any incoming call, the music of the application stops automatically, but does not resume again when the call is disconnected unlike the iPhone Music application.

Is this a limitation of iOS or can we achieve this?

Note. I'm not looking for a solution for jailbroken devices or enterprise applications

+3
source share
1 answer

Have you tried creating a call center and assigning a handler block in the AppDelegate class? The following should 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

    }

}

Remember to enable Background Modes for this. And do something in the background, for example, getting a location.

+1
source

All Articles