【发布时间】:2014-10-17 19:08:09
【问题描述】:
我正在尝试查看是否有任何正在进行的呼叫,但我无法将 CTCallCenter 的实例保留为属性。这基本上就是我现在正在调试的工作(一切都在 MyClass 中):
-(void)checkForCurrentCalls
{
CTCallCenter *newCenter = [[CTCallCenter alloc] init];
if (newCenter.currentCalls != nil)
{
NSLog(@"completely new call center says calls in progress:");
for (CTCall* call in newCenter.currentCalls) {
NSLog(call.callState);
}
}
if (self.callCenter.currentCalls != nil) {
NSLog(@"property-call center says calls in progress:");
for (CTCall* call in self.callCenter.currentCalls) {
NSLog(call.callState);
}
}
}
我的 self.callCenter 是 @property (nonatomic, strong) CTCallCenter *callCenter;。它合成了setter和getter。在 MyClass 的 init 方法中初始化:
- (id)init
{
self = [super init];
if (self) {
self.callCenter = [[CTCallCenter alloc] init];
}
return self;
}
如果我在checkForCurrentCalls 之前调用我的另一个方法,那么我的self.callCenter.currentCalls 将停止更新它应该的方式。更准确地说,我(对自己)拨打的电话不断增加,因此如果我拨打并挂断了三个电话,我会打印出三个处于“拨号”状态的 CTCall。 newCenter 按预期工作。
我要做的就是打破它:
- (void)trackCallStateChanges
{
self.callCenter.callEventHandler = ^(CTCall *call)
{
};
}
我遇到的答案说 CTCallCenter 必须在主队列上进行分配初始化。从那以后,我注意只在主队列上调用我自己的init,使用我的应用委托的 dispatch_async:
dispatch_async(dispatch_get_main_queue(), ^{
self.myClass = [[MyClass alloc] init];
[self.myClass trackCallStateChanges];
}
我的checkForCurrentCalls 稍后在applicationWillEnterForeground 中调用。
我不明白为什么只设置callEventHandler-block 会破坏它。
一切都在 iphone 5 ios 7.1.2 上测试。
【问题讨论】:
标签: ios objective-c iphone