ios电话拨打进行监听电话状态

#import "ViewController.h"

#import <CoreTelephony/CTCallCenter.h>

#import <CoreTelephony/CTCall.h>

@interface ViewController ()

@property (nonatomic, strong) CTCallCenter *center;

@property (nonatomic, strong) UIWebView * callView;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 150, 50)];

    btn.backgroundColor = [UIColor redColor];

    [btn setTitle:@"拨打电话" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(callBack) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    _center = [[CTCallCenter alloc] init];

    _center.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");

        }

    };

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)callBack{

    NSString * str=[[NSString alloc] initWithFormat:@"tel:%@",@"18310501773"];

    UIWebView * callWebview = [[UIWebView alloc] init];

    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];

    [self.view addSubview:callWebview];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

posted @   孙富有(iOS工程师)  阅读(525)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示