我正在尝试检测iOS 10上的通话状态。 我在iOS 9.3上测试了它,它运行正常。 但在iOS 10上,CTCallCenter已被弃用,因此我使用了Callkit。
我无法检测到通话状态。 我找不到任何正确的答案。
我的代码是:
@property (nonatomic, strong) CXCallObserver *callObserver;
...
self.callObserver = [[CXCallObserver alloc] init];
[callObserver setDelegate:self queue:nil];
...
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
if (call.hasConnected) {
NSLog(@"connected/n");
// perform necessary actions
} else if(call.hasEnded) {
NSLog(@"disconnected/n");
}
}
答案 0 :(得分:0)
这段代码对我有用。唯一的区别是我为观察者提供了一个调度队列,而不是使用主队列。
#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>
@interface CallStatusIos10 : NSObject <CXCallObserverDelegate>
{
dispatch_queue_t dq;
}
@property (nonatomic, strong) CXCallObserver* observer;
@end
@implementation CallStatusIos10
@synthesize observer;
-(id) init
{
if(self = [super init])
{
observer = [[CXCallObserver alloc] init];
// Hang on to a reference to the queue, as the
// CXCallObserver only maintains a weak reference
dq = dispatch_queue_create("my.ios10.call.status.queue", DISPATCH_QUEUE_SERIAL);
[observer setDelegate: self queue: dq];
}
return self;
}
#pragma mark - CXCallObserverDelegate
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call
{
NSString* callId = [call.UUID UUIDString];
if(!call.hasConnected && !call.hasEnded && !call.onHold)
{
if(call.outgoing)
{
// outgoing call is being dialling;
}
else
{
// incoming call is ringing;
}
}
else if(call.onHold)
{
// call is on hold;
}
else if(call.hasEnded)
{
// call has disconnected;
}
else if(call.hasConnected)
{
// call has been answered;
}
}
@end
答案 1 :(得分:-1)
试试这个
Appdelegate.h
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
...
@property (nonatomic, strong) CTCallCenter* callCenter;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
....
self.callCenter = [[CTCallCenter alloc] init];
[self handleCall]; //Call this method when you want to use it
....
}
-(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");
}
};
}