App电话状态监听
一般这种获取系统状态变化的代码都是写在AppDelegate里面,在didFinishLaunchingWithOptions方法里面调用getCallState 方法就行了
第一步:导入系统头文件
//导入系统的头文件
import <CoreTelephony/CTCallCenter.h>
import <CoreTelephony/CTCall.h>
第二步:申明全局变量callCenter
@property (nonatomic, strong) CTCallCenter *callCenter;
第三步:调用方法
- (void)getCallState {
self.callCenter = [[CTCallCenter alloc] init];
self.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");
}
};
}
注:CTCallCente必须用全局
以上就是监听电话状态方法~
慢慢来,一步一个巴掌印。。。。。
网友评论