iOS How to check if currently on phone call

女生的网名这么多〃 提交于 2019-11-29 23:31:49

The CTCallCenter object has a currentCalls property which is an NSSet of the current calls. If there is a call then the currentCalls property should be != nil.

If you want to know if any of the calls is actually connected, then you'll have to iterate through the current calls and check the callState to determine if it is CTCallStateConnected or not.

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

-(bool)isOnPhoneCall {
    /*

     Returns TRUE/YES if the user is currently on a phone call

     */

    CTCallCenter *callCenter = [[[CTCallCenter alloc] init] autorelease];
    for (CTCall *call in callCenter.currentCalls)  {
        if (call.callState == CTCallStateConnected) {
            return YES;
        }
    }
    return NO;
}

Thanks for the answer ThomasW. I thought I would also post the code.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    CTCallCenter *ctCallCenter = [[CTCallCenter alloc] init];
    if (ctCallCenter.currentCalls != nil) 
    {
        NSArray* currentCalls = [ctCallCenter.currentCalls allObjects];
        for (CTCall *call in currentCalls)
        {   
            if(call.callState == CTCallStateConnected)
            {
                // connected
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!