iOS监听来电状态

@hanq  2015年10月15日 16:47


1)首先需要引入CoreTelephony.framework;

2)引入头文件:

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

3)声明成员变量:

@property (nonatomic,strong) CTCallCenter *callCenter;

4)init下初始化CTCallCenter:

_callCenter = [[CTCallCenter alloc] init];

5)添加事件Handler:

_callCenter.callEventHandler = ^(CTCall *call){
    if ([call.callState isEqualToString:CTCallStateDisconnected]){
        MLog(@"Call has been disconnected");
    }else if ([call.callState isEqualToString:CTCallStateConnected]){
        MLog(@"Call has just been connected");
    }else if([call.callState isEqualToString:CTCallStateIncoming]){
        MLog(@"Call is incoming");
    }else if ([call.callState isEqualToString:CTCallStateDialing]){
        MLog(@"call is dialing");
    }else{
        MLog(@"Nothing is done");
    }
};

运行代码,呼入电话看效果吧~


添加新评论