先导入这两个头文件,库文件不用导可以
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
//申明电话类属性,必须申明成成品属性,不然没效果;
@property(nonatomic,strong)CTCallCenter *callCenter;
//如下两个方法中监听:
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler=^(CTCall* call)
{
if (call.callState == CTCallStateDisconnected)
{
NSLog(@"-applicationWillResignActive----挂断"); //挂断
// self.viewController.signalStatus=YES;
}
else if (call.callState == CTCallStateConnected)
{
NSLog(@"--applicationWillResignActive---连通了"); //联通了
}
else if(call.callState == CTCallStateIncoming)
{
NSLog(@"applicationWillResignActive Call is incoming");
//self.viewController.signalStatus=NO;
}
else if (call.callState ==CTCallStateDialing)
{
NSLog(@"--applicationWillResignActive----拨号"); //拨号
}
else
{
NSLog(@"applicationWillResignActive Nothing is done?");
}
};
NSLog(@"applicationWillResignActive:%@",application);
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler=^(CTCall* call)
{
if (call.callState == CTCallStateDisconnected)
{
NSLog(@"-applicationDidBecomeActive----挂断"); //挂断
// self.viewController.signalStatus=YES;
}
else if (call.callState == CTCallStateConnected)
{
NSLog(@"--applicationDidBecomeActive---连通了"); //联通了
}
else if(call.callState == CTCallStateIncoming)
{
NSLog(@"applicationDidBecomeActive Call is incoming");
//self.viewController.signalStatus=NO;
}
else if (call.callState ==CTCallStateDialing)
{
NSLog(@"--applicationDidBecomeActive----拨号"); //拨号
}
else
{
NSLog(@"applicationDidBecomeActive Nothing is done?");
}
};
NSLog(@"applicationDidBecomeActive:%@",application);
}
//结合上面的打印顺序,app方法的调用顺序如下:
//来电时> applicationDidBecomeActive Call is incoming > applicationWillResignActive:
//挂断> applicationWillResignActive----挂断 > applicationDidBecomeActive:<UIApplication: 0x137609010>
//机主接听> --applicationWillResignActive---连通了
//通话结束> -applicationWillResignActive----挂断 > applicationDidBecomeActive:<UIApplication: 0x137609010>