How to get a call event using CTCallCenter:setCallEventHandler: that occurred while the app was suspended?

后端 未结 2 1541
别跟我提以往
别跟我提以往 2020-11-29 02:46

The documentation for CTCallCenter:setCallEventHandler: states that:

However, call events can also take place while your application is suspended.

2条回答
  •  一生所求
    2020-11-29 03:15

    I've found a solution but I have no idea why it's working. Only thing I can think of is a bug in GCD and/or CoreTelephony.

    Basically, I allocate two instances of CTCallCenter like this

    void (^block)(CTCall*) = ^(CTCall* call) { NSLog(@"%@", call.callState); };
    
    -(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    {
        callCenter1 = [[CTCallCenter alloc] init];
        callCenter1.callEventHandler = block;
    
        callCenter2 = [[CTCallCenter alloc] init];
        callCenter2.callEventHandler = block;
    
        return YES;
    }
    

    Similar Code in Swift:

    func block (call:CTCall!) {
            println(call.callState)
        }
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            //Declare callcenter in the class like 'var callcenter = CTCallCenter()'
            callcenter.callEventHandler = block
    
            return true
        }
    

    To test this I made a call, answered it and then hanged up it while app was in background. When I launched it I received 3 call events: incoming, connected, disconnected.

提交回复
热议问题