CTCallCenter - Call Event Handler - in background state

后端 未结 3 1633
谎友^
谎友^ 2020-12-03 20:10

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<

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 20:44

    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);  
    }
    

提交回复
热议问题