我想处理ios中的调用状态

时间:2014-06-20 08:44:28

标签: ios iphone phone-call

我想要拨打电话,连接电话或断开电话......

我尝试了自己,但我无法得到这个状态。

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:@"9723539389"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

CTCallCenter *callCenter = [[CTCallCenter alloc] init];
callCenter.callEventHandler=^(CTCall* call)
{
    if(CTCallStateDialing)
    {
        NSLog(@"Dialing");
    }
    if(CTCallStateConnected)
    {
        NSLog(@"Connected");
    }
    if(CTCallStateDisconnected)
    {
        NSLog(@"Disconnected");
    }
};

但问题是CTCallCenter块从未调用过......我目前在iOS 7中工作

2 个答案:

答案 0 :(得分:5)

你应该检查CTCall的callState属性以便捕获它

Use a cellular call’s CTCall object to obtain an identifier for the call and to determine the call’s state.

 extern NSString const *CTCallStateDialing;
 extern NSString const *CTCallStateIncoming;
 extern NSString const *CTCallStateConnected;
 extern NSString const *CTCallStateDisconnected;

是字符串常量。你的循环没有意义。

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:@"9723539389"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

self.callCenter = [[CTCallCenter alloc] init];

[callCenter setCallEventHandler:^(CTCall* call)
{
    if ([call.callState isEqualToString: CTCallStateConnected])
    {
        NSLog(@"Connected");
    }
    else if ([call.callState isEqualToString: CTCallStateDialing])
    {
        NSLog(@"Dialing");
    }
    else if ([call.callState isEqualToString: CTCallStateDisconnected])
    {
        NSLog(@"Disconnected");

    } else if ([call.callState isEqualToString: CTCallStateIncoming])
    {
        NSLog(@"Incoming");
    }

}];

注意:

  @property(nonatomic, strong) CTCallCenter *callCenter;
应该在Appdelegate中声明

并保留它。否则它将被视为局部变量&一出现循环就解除分配

更新

回答“呼叫中心是在appdelegate中声明的,我如何将它与自己一起使用?和其他东西当[]添加到该块时,它会显示错误等于”expected“]'”“

用这些行替换self.callCenter

   YourApplicationDelegateClass *appDel =(YourApplicationDelegateClass*)[UIApplication sharedApplication].delegate;

   appDel.callCenter =  [[CTCallCenter alloc] init];

答案 1 :(得分:1)

我是这样做的。如上所述,我在AppDelegate中使用它,但整个块也在appDelegate中,我不监视状态,但检查调用是否为nil。

在AppDelegate.h文件中

#import <UIKit/UIKit.h>
#import <CoreTelephony/CTCall.h>
#import <CoreTelephony/CTCallCenter.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) CTCallCenter *center;
@property (nonatomic, assign) BOOL callWasMade;

@end

在AppDelegate.m文件中

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    self.center = [[CTCallCenter alloc]init];
    typeof(self) __weak weakSelf = self;
    self.center.callEventHandler = ^(CTCall *call) {
        if(call!=nil) {
            NSLog(@"Call details is not nil, so user must have pressed call button somewhere in the alert view");
            weakSelf.callWasMade = YES;
        }
    };
}

使用callWasMade布尔属性,您可以继续执行任务。通过获取AppDelegate的句柄,可以在任何ViewController中引用此属性。我希望你发现这个片段很有用。