我认为这在电话的目的地更好地解决。我要么让电话公司配置“跟我来”服务,使用 Twilio 或其他一些第 3 方服务(如前所述),或者使用 Asterisk 之类的东西配置我自己的 PBX ( Asterisk 包括配置“跟我来”类型的能力行为)。它为您提供了更多的灵活性和控制力,即使您确实找到了一种在 iOS 中本地执行此操作的方法。
话虽如此,我确实让它在 iOS 中工作,假设如下:
- 您的应用发起呼叫。
- 打开电话应用程序,拨打号码,然后断开连接。
- 用户明确返回到您的应用程序。如果您在应用程序后台运行时设法获取事件,我想知道更多:-)。
- 将控制权返回给您的应用后,将发送电话事件并启动新呼叫。
我的 UIApplicationDelegate didFinishLaunchingWithOptions 方法中有以下代码片段:
// In appdelegate header, ct is declared as @property (strong, nonatomic) CTCallCenter *ct;
self.ct = [[CTCallCenter alloc] init];
self.ct.callEventHandler = ^(CTCall *call) {
if (call.callState == CTCallStateConnected) {
// do some state management to track the call
} else if (call.callState == CTCallStateDisconnected) {
// check that this is the expected call and setup the
// new phone number
NSURL *telURL = [NSURL URLWithString:myNewNumberURL];
[application openURL:telURL];
}
};
这将拨打新电话。我正在使用 iOS 5 SDK;在 iPhone 4s 上测试。
编辑:
使用与 UIWebView 不同的本机代码中的电话后返回到应用程序行为作为起点,我已经设法让它工作。请注意,为了清楚起见,我已经强调了内存管理。假设您在调用完成后使用 Web 视图技术返回您的应用程序,请在调用完成块中尝试以下操作:
else if (call.callState == CTCallStateDisconnected) {
// check that this is the expected call and setup the
// new phone number
NSURL *telURL = [NSURL URLWithString:myNewNumberURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIWebView *callWebview = [[UIWebView alloc] init] ;
[self.window.rootViewController.view addSubview:callWebview];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
// and now callWebView sits around until the app is killed....so don't follow this to the letter.
});
}
但是,这也可能无法完全满足您的需求。用户将收到有关每个呼叫请求的警报,从而提供取消呼叫的机会。