xcode iOS 如何检查当前是否正在通话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10942334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS How to check if currently on phone call
提问by friend
I have look around and all I can find is checking the callState of CTCallCenter. However, this works by listening to an event - which depending on whether the application is active/suspended/resumed the event can be raised at different time.
我环顾四周,我能找到的只是检查 CTCallCenter 的 callState。但是,这是通过侦听事件来实现的——这取决于应用程序是否处于活动/挂起/恢复状态,可以在不同时间引发事件。
What I need is rather than listening to event and got told when call is connected, I want to decide myself when to ask if the call is connected.
我需要的不是监听事件并在呼叫连接时被告知,我想决定自己何时询问呼叫是否已连接。
Use case: When phone call is connected - user knows and will always click on the app icon, which will open the app. At this time I just want to run a quick function to check if currently on call or not.
用例:当电话接通时 - 用户知道并将始终单击应用程序图标,这将打开应用程序。此时我只想运行一个快速函数来检查当前是否在通话中。
Is this even possible?
这甚至可能吗?
回答by AlBeebe
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
-(bool)isOnPhoneCall {
/*
Returns TRUE/YES if the user is currently on a phone call
*/
CTCallCenter *callCenter = [[[CTCallCenter alloc] init] autorelease];
for (CTCall *call in callCenter.currentCalls) {
if (call.callState == CTCallStateConnected) {
return YES;
}
}
return NO;
}
回答by ThomasW
The CTCallCenter
object has a currentCalls
property which is an NSSet
of the current calls. If there is a call then the currentCalls
property should be != nil.
该CTCallCenter
对象有一个currentCalls
属性,它是NSSet
当前调用的一个。如果有电话,则该currentCalls
属性应为 != nil。
If you want to know if any of the calls is actually connected, then you'll have to iterate through the current calls and check the callState
to determine if it is CTCallStateConnected
or not.
如果您想知道是否有任何呼叫实际连接,那么您必须遍历当前呼叫并检查callState
以确定是否连接CTCallStateConnected
。
回答by friend
Thanks for the answer ThomasW. I thought I would also post the code.
感谢您的回答 ThomasW。我以为我也会发布代码。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
CTCallCenter *ctCallCenter = [[CTCallCenter alloc] init];
if (ctCallCenter.currentCalls != nil)
{
NSArray* currentCalls = [ctCallCenter.currentCalls allObjects];
for (CTCall *call in currentCalls)
{
if(call.callState == CTCallStateConnected)
{
// connected
}
}
}
}
回答by JPetric
I was having the same problem, but I think that the correct way to do this from iOS 10is:
我遇到了同样的问题,但我认为从iOS 10执行此操作的正确方法是:
func checkForActiveCall() -> Bool {
for call in CXCallObserver().calls {
if call.hasEnded == false {
return true
}
}
return false
}