Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone, call another phone number in response to the first not answering?

I am attempting to create an application that will initiate a call to a priority 1 contact on a call-center-like list.

Then, if that contact does not answer (let's forget the whole problem of answering machines here), I'd like to call the priority 2 contact, and so on, until one of them answers or I exhaust my list.

Is this possible?

I've tried the following:

  1. Hook into the CTCallCenter.CallEventHandler event, and checking the call state for CTCallStateConnected and CTCallStateDisconnected, and I get it to respond to the fact that the call disconnected, without ever connecting, and then attempt to initiate another call like I did the first, but this second attempt just sits dead in the water.
  2. Override the DidEnterBackground method, and periodically check the CTCall.CallState property, basically again trying to respond to a disconnect that was never connected, but this does not appear to work either

I also tried adding a short delay (1 second, 2.5 seconds and 10 seconds) after detecting the disconnected state before attempting the next dial, to allow for the phone application to "settle down" after aborting the call, this did not change anything.

like image 729
Lasse V. Karlsen Avatar asked Jan 03 '12 19:01

Lasse V. Karlsen


1 Answers

I'm of the opinion that this is better solved at the destination of the phone call. I would either have the phone company configure a "follow me" service, use Twilio or some other 3rd party service (as already suggested), or configure my own PBX using something like Asterisk (Asterisk includes the ability to configure "follow me" type behavior). It provides you much more flexibility and control, even if you did find a way to do this natively in iOS.

Having said that, I did get this to work in iOS assuming the following:

  1. Your app initiates the call.
  2. The phone app is opened, dials the number, and disconnects.
  3. The user explicitly returns to your app. If you managed to get the events while your app was backgrounded, I want to know more :-).
  4. On return of control to your app, the phone events are sent and a new call is initiated.

I have the following snippet of code in my UIApplicationDelegate didFinishLaunchingWithOptions method:

// 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];    
    }     
};

This will make the new call. I'm using the iOS 5 SDK; tested on an iPhone 4s.

EDIT:

Using Return to app behavior after phone call different in native code than UIWebView as a starting point, I've managed to get this to work. Note that I have punted on memory management for clarity. Assuming you use the web view technique for getting back to your app after the call is complete, try something like this in the call completed block:

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.  
    });
}

However, this may not quite give you what you want either. The user will get an alert on each call request, providing an opportunity to cancel the call.

like image 180
MikeG Avatar answered Nov 19 '22 17:11

MikeG