iOS监听拨打电话回调函数

时间:2022-11-10 07:30:26

今天在公司写一个项目,需要监听拨打电话完的状态,客户要求在接通电话之后挂断电话的话就进行一系列操作,如果没有接通就挂断电话则不能进行操作

下面上代码

头文件 在这里需要包含两个头文件,不需要导入framwork

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

@interface DetailViewController () <UIWebViewDelegate>

@property (nonatomic, strong) CTCallCenter *callCenter;
@end

这是两种打电话的方式

- (IBAction)phoneAction:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
}

- (IBAction)phoneWebAction:(id)sender {
UIWebView *callWebview =[[UIWebView alloc] init] ;
NSURL *telURL =[NSURL URLWithString:@"tel://514091"];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]]
;
[self.view addSubview:callWebview];
}

这是电话回调函数

-(void)detectCall
{
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler=^(CTCall* call)
{
if (call.callState == CTCallStateDisconnected)
{
NSLog(@"-----------------------------挂断"); //挂断
// self.viewController.signalStatus=YES;
}
else if (call.callState == CTCallStateConnected)
{
NSLog(@"-----------------------------连通了"); //联通了
}

else if(call.callState == CTCallStateIncoming)
{
NSLog(@"Call is incoming??????????1");
//self.viewController.signalStatus=NO;
}

else if (call.callState ==CTCallStateDialing)
{
NSLog(@"-----------------------------拨号"); //拨号
}
else
{
NSLog(@"Nothing is done????????????2");
}
};
}