12

如果我从我的应用程序拨打电话,我无法检测到通话结束(状态 CallStateDisconnected)。但是,如果我在我的应用程序处于活动状态时接到电话,我可以检测到该状态。当从我的应用程序开始呼叫时,我还会收到两次或 3 次状态 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在调用之后和进入后台之前调用吗?

4

1 回答 1

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:

“断开连接”消息刚刚消失。(这不是答案,而是观察。)

这是我使用的代码。II 在 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
于 2012-12-03T06:31:22.087 回答