我希望在我的应用中获得通话状态
经过一番搜索,我找到了CoreTelephony
框架。但是在iOS 10中已经弃用了。那么还有其他可用选择吗?
我还找到了CallKit
。 iOS 10中的一个新框架。但是没有像我搜索到的那样获得呼叫状态。
答案 0 :(得分:8)
import CallKit
进入AppDelegate
并添加以下代码:
// AppDelegate
var callObserver: CXCallObserver! // add property
// in applicationDidFinishLaunching...
callObserver = CXCallObserver()
callObserver.setDelegate(self, queue: nil) // nil queue means main thread
extension AppDelegate: CXCallObserverDelegate {
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if call.hasEnded == true {
print("Disconnected")
}
if call.isOutgoing == true && call.hasConnected == false {
print("Dialing")
}
if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false {
print("Incoming")
}
if call.hasConnected == true && call.hasEnded == false {
print("Connected")
}
}
}
答案 1 :(得分:7)
您可以尝试以下代码:
创建调用观察者实例
@property ( nonatomic ) CXCallObserver *callObserver;
启动实例并设置代理
_callObserver = [CXCallObserver new];
[_callObserver setDelegate:self queue:dispatch_get_main_queue()];
添加调用观察者委托
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call{
if (call == nil || call.hasEnded == YES) {
NSLog(@"CXCallState : Disconnected");
}
if (call.isOutgoing == YES && call.hasConnected == NO) {
NSLog(@"CXCallState : Dialing");
}
if (call.isOutgoing == NO && call.hasConnected == NO && call.hasEnded == NO && call != nil) {
NSLog(@"CXCallState : Incoming");
}
if (call.hasConnected == YES && call.hasEnded == NO) {
NSLog(@"CXCallState : Connected");
}
}
在Swift 4.2中:
var callObserver: CXCallObserver()
callObserver.setDelegate(self, queue: DispatchQueue.main)
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if call == nil || call.hasEnded == true {
print("CXCallState : Disconnected")
}
if call.isOutgoing == true && call.hasConnected == false {
print("CXCallState : Dialing")
}
if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false && call != nil {
print("CXCallState : Incoming")
}
if call.hasConnected == true && call.hasEnded == false {
print("CXCallState : Connected")
}
}
答案 2 :(得分:2)
要从iOS 10开始收到关于电话呼叫状态的通知,您应该使用CXCallObserver
课程并实施其CXCallObserverDelegate
协议方法,该方法在this回答中建议:
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
if (call.hasConnected) {
// perform necessary actions
}
}
答案 3 :(得分:1)
有一些委托方法可用于指示来电和去电状态。
答案 4 :(得分:-3)
Apple不允许您访问这些信息,但您可以通过使用第三方库来访问这些信息,但是在向App Store提交时您的应用会被拒绝,因为Apple不允许访问敏感用户信息。< / p>