CTCallCenter - Call Event Handler - in background state

帅比萌擦擦* 提交于 2019-11-26 18:30:01

问题


Regarding the Apple documentation there is no way to handle the phone state while the app is suspended: https://developer.apple.com/documentation/coretelephony/ctcallcenter

"While it is suspended, your application does not receive call events"

Is this also true for the "background" state? (As the background state is not the same with the "suspended" app state regarding the states described in the Apple documentation)

https://web.archive.org/web/20140824215114/https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

I'm handling the phone state using the following code:

CTCallCenter *callCenter = [[CTCallCenter alloc] init];

callCenter.callEventHandler=^(CTCall* call)
{

  //call state

};

I have added a local notifications into the callEventHandler block in order to check if a call events will be received while my app is in background state but is seams that the block is not executed ( my app has a background support and all received events (via TCP) are handled correctly while the app is in background )


回答1:


All tests that I've done I can't receive any using callEventHandler when the application is in background. But, when the application is in foreground, all nicely work.

The socket works, because iOS handles it for you app and deliver the packtes accordingly. But for that, you need to create a voip socket and add voip to UIBackgroundModes to your App-Info.plist.




回答2:


You will not be able to monitor phone calls in the background using the callEventHandler...

However, according to this thread in apple dev forums, you can check the currentCalls periodically in the background to determine if calls are in progress.

Here is the code you should use (to check every seconds) :

- (void)viewDidLoad {  
    [super viewDidLoad];

    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(phoneDetection) userInfo:nil repeats:YES];  
}  

- (void)phoneDetection {
    _callCenter = [[CTCallCenter alloc] init]; // Here is the important part : instanciating a call center each time you want to check !  

    [_callCenter setCallEventHandler:^(CTCall *call) {  
        NSLog(@"Call detected");  
    }];  

    NSLog(@"%@", _callCenter.currentCalls);  
}



回答3:


I know some apps run soundless audio files in the background to prevent from being closed after 10 minutes of inactivity.

Bluetooth, location, and audio can prevent the app from being completely killed.

My app is able to detect incoming vs outgoing calls while in background because the app stays alive with location updates. We have used audio/bluetooth in the past.

Without some method of "keepAlive" the OS will suspend your app until some external stimulus reactivates it (push notification, user launch, etc...)



来源:https://stackoverflow.com/questions/10102108/ctcallcenter-call-event-handler-in-background-state

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