How to get incoming/outgoing call event in background state

后端 未结 1 1477
囚心锁ツ
囚心锁ツ 2021-01-21 07:38

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

相关标签:
1条回答
  • 2021-01-21 07:58

    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.

    0 讨论(0)
提交回复
热议问题