我认为这个问题最好在电话目的地解决。我要么让电话公司配置“跟随我”服务,使用 Twilio 或其他一些第三方服务(如已经建议的那样),要么使用类似的东西配置我自己的 PBXAsterisk http://www.asterisk.org/(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 上进行测试。
EDIT:
Using 通话后返回应用程序行为与 UIWebView 的本机代码不同 https://stackoverflow.com/questions/5317783/return-to-app-behavior-after-phone-call-different-in-native-code-than-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.
});
}
然而,这可能也不能完全满足您的需求。用户将收到有关每个呼叫请求的警报,从而提供取消呼叫的机会。