如何以编程方式检测呼入 [英] How to detect call incoming programmatically
本文介绍了如何以编程方式检测呼入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要我的应用程序在有电话时发送通知(来电,已连接,通话结束)
我通过通知注册了我的viewController。
I need my app to send a notification when there is a call (incoming call, connected, call ended) I registered my viewController with notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callReceived:) name:CTCallStateIncoming object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callEnded:) name:CTCallStateDisconnected object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callConnected:) name:CTCallStateConnected object:nil];
我还提出了一种检查通话状态的方法
I also made a method to check call state
-(IBAction)checkForCall:(id)sender{
NSLog(@"call state %@ id %@",call.callState,call.callID);
CTCallCenter *callCenter = [[CTCallCenter alloc] init];
callCenter.callEventHandler = ^(CTCall* call){
if (call.callState == CTCallStateDisconnected)
{
NSLog(@"Call has been disconnected");
}
else if (call.callState == CTCallStateConnected)
{
NSLog(@"Call has just been connected");
}
else if(call.callState == CTCallStateIncoming)
{
NSLog(@"Call is incoming");
}
else
{
NSLog(@"None of the conditions");
}
};
}
但这都不起作用。请帮助我。
But none of this works. Please help me.
哪里出错?是否有任何代码告诉如何使用核心电话?
Where is the mistake? Is there any code which tells how to use core telephony?
推荐答案
使用此
Appdelegate.h
Appdelegate.h
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
...
@property (nonatomic, strong) CTCallCenter* callCenter;
AppDelegate.m
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....
self.callCenter = [[CTCallCenter alloc] init];
[self handleCall];
....
}
-(void)handleCall
{
self.callCenter.callEventHandler = ^(CTCall *call){
if ([call.callState isEqualToString: CTCallStateConnected])
{
//NSLog(@"call stopped");
}
else if ([call.callState isEqualToString: CTCallStateDialing])
{
}
else if ([call.callState isEqualToString: CTCallStateDisconnected])
{
//NSLog(@"call played");
}
else if ([call.callState isEqualToString: CTCallStateIncoming])
{
//NSLog(@"call stopped");
}
};
}
这篇关于如何以编程方式检测呼入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文