如果我从我的应用程序拨打电话,我无法检测到呼叫结束(状态CallStateDisconnected)。但如果我的应用程序处于活动状态时接到电话,我可以检测到该状态。当我的应用程序启动呼叫时,我还会收到状态CTCallStateDialing两次或三次。 它曾经在iOS5下工作,这个问题出现在iOS6上。
我的应用程序代码;
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* call) {
// anounce that we've had a state change in our call center
NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:@"callState"]; //BREAKPOINT HERE
[[NSNotificationCenter defaultCenter] postNotificationName:@"CTCallStateDidChange" object:self userInfo:dict];
};
奇怪的是它一切正常,如果我在callEventHandler块中放置断点并在调用完成后恢复执行,那么我正确地获得CallStateDisconnected。
然后我在视图控制器中订阅通知,并在收到它时执行此代码:
- (void)ctCallStateDidChange1:(NSNotification *)notification
{
NSString *call = [[notification userInfo] objectForKey:@"callState"];
if ([call isEqualToString:CTCallStateDisconnected])
{
NSLog(@"Call has been disconnected");
}
else if([call isEqualToString:CTCallStateDialing])
{
NSLog(@"Call start");
}
else if ([call isEqualToString:CTCallStateConnected])
{
NSLog(@"Call has just been connected");
}
else if([call isEqualToString:CTCallStateIncoming])
{
NSLog(@"Call is incoming");
}
else
{
NSLog(@"None");
}
}
我可以通过我的应用程序拨打电话:
UIWebView *callWebview = [[UIWebView alloc] init];
[self.view addSubview:callWebview];
NSURL *telURL = [NSURL URLWithString:number];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
我还记录了应用状态,然后收到- (void)applicationDidBecomeActive:(UIApplication *)application
在我开始通话后,我转到- (void)applicationDidEnterBackground:(UIApplication *)application
并且在通话结束后返回- (void)applicationDidBecomeActive:(UIApplication *)application.
奇怪的是,在调用之后和进入后台之前调用了DidBecomeActive
吗?
答案 0 :(得分:6)
以下是iOS 5手机的日志:
2012-12-02 22:00:00.252 TestPhone[2297:707] didFinishLaunchingWithOptions:
2012-12-02 22:00:00.352 TestPhone[2297:707] applicationDidBecomeActive:
2012-12-02 22:00:06.708 TestPhone[2297:707] applicationWillResignActive:
2012-12-02 22:00:10.582 TestPhone[2297:1c03] Call start
2012-12-02 22:00:11.016 TestPhone[2297:707] applicationDidBecomeActive:
2012-12-02 22:00:12.536 TestPhone[2297:707] applicationWillResignActive:
2012-12-02 22:00:12.564 TestPhone[2297:707] applicationDidEnterBackground:
2012-12-02 22:00:36.543 TestPhone[2297:707] applicationWillEnterForeground:
2012-12-02 22:00:36.769 TestPhone[2297:707] applicationDidBecomeActive:
2012-12-02 22:00:36.840 TestPhone[2297:2e07] Call has been disconnected
2012-12-02 22:00:36.854 TestPhone[2297:707] applicationWillResignActive:
2012-12-02 22:00:49.885 TestPhone[2297:707] applicationDidBecomeActive:
这是来自iOS 6手机的日志:
2012-12-03 06:27:55.401 TestPhone[7734:907] didFinishLaunchingWithOptions:
2012-12-03 06:27:55.462 TestPhone[7734:907] applicationDidBecomeActive:
2012-12-03 06:28:01.396 TestPhone[7734:907] applicationWillResignActive:
2012-12-03 06:28:04.345 TestPhone[7734:907] applicationDidBecomeActive:
2012-12-03 06:28:04.401 TestPhone[7734:1803] Call start
2012-12-03 06:28:05.824 TestPhone[7734:907] applicationWillResignActive:
2012-12-03 06:28:05.826 TestPhone[7734:907] applicationDidEnterBackground:
2012-12-03 06:28:17.318 TestPhone[7734:907] applicationWillEnterForeground:
2012-12-03 06:28:17.329 TestPhone[7734:907] applicationDidBecomeActive:
“已断开连接”消息刚刚消失。 (这不是一个答案,这是一个观察。)
这是我使用的代码。我在xcode中创建了一个单视图应用程序,在xib中只有一个按钮,这就是我的整个UIViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* myCall) {
NSString *call = myCall.callState;
if ([call isEqualToString:CTCallStateDisconnected])
NSLog(@"Call has been disconnected");
else if([call isEqualToString:CTCallStateDialing])
NSLog(@"Call start");
else if ([call isEqualToString:CTCallStateConnected])
NSLog(@"Call has just been connected");
else if([call isEqualToString:CTCallStateIncoming])
NSLog(@"Call is incoming");
else
NSLog(@"None");
};
}
- (IBAction)callButtonPressed:(id)sender {
NSString *number = @"tel:01234567890";
UIWebView *callWebview = [[UIWebView alloc] init];
callWebview.frame = self.view.frame;
[self.view addSubview:callWebview];
NSURL *telURL = [NSURL URLWithString:number];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
}
@end