ios 拨打电话系统回调函数

时间:2022-11-10 07:25:50


当我们使用系统的拨打电话函数拨打电话时,想获取现在电话的状态,可以使用下面的函数获得状态。

使用方法:

1.引入CoreTelephony.frameWork 

2. 在APPDelegate中做如下定义与说明

#import <CoreTelephony/CTCall.h>

#import <CoreTelephony/CTCallCenter.h>

@interface AppDelegate () 
{
CTCallCenter *callCenter;
}
@end

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self callCallBack];
}

- (void)callCallBack
{
callCenter = [[CTCallCenter alloc] init];
callCenter.callEventHandler = ^(CTCall* call) {
if ([call.callState isEqualToString:CTCallStateDisconnected])
{
NSLog(@"Call has been disconnected");
}
else if ([call.callState isEqualToString:CTCallStateConnected])
{
NSLog(@"Call has just been connected");
}
else if([call.callState isEqualToString:CTCallStateIncoming])
{
NSLog(@"Call is incoming");
}
else if ([call.callState isEqualToString:CTCallStateDialing])
{
NSLog(@"call is dialing");
}
else
{
NSLog(@"Nothing is done");
}
};
}
3.  在ViewController中调用系统的拨打电话函数,然后就会回调刚刚上面的callcallBack中的ios系统提供的方法。

[[UIApplication sharedApplicationopenURL:[NSURL URLWithString:str]]