我想处理ios中的调用状态
我希望通过拨打电话,连接电话或断开连接来获得电话状态…
我尝试了自己但我无法获得状态。
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中工作过
您应该检查CTCall的callState属性以捕获它
使用蜂窝呼叫的CTCall对象获取呼叫的标识符并确定呼叫的状态。
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中声明的,我如何将它与self一起使用?等等[]添加到该块然后它显示错误等于”expected’]’“”
用这些行替换self.callCenter
YourApplicationDelegateClass *appDel =(YourApplicationDelegateClass*)[UIApplication sharedApplication].delegate; appDel.callCenter = [[CTCallCenter alloc] init];
我是这样做的。 如上所述,我在AppDelegate中使用它,但整个块也在appDelegate中,我不监视状态,但检查调用是否为nil。
在AppDelegate.h文件中
#import #import #import @interface AppDelegate : UIResponder @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中引用此属性。 我希望你发现这个片段很有用。