ios关于手机拨打电话,回调问题

时间:2022-11-10 14:06:41

需要导入下面两个头文件,导入CoreTelephony.framework,

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

@interface TelViewController ()

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

以下两种打电话的方式:

1.通常的方法
- (IBAction)phoneAction:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
}
2.获取回调的方法
- (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
{
CTCallCenter *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");
}
};
}