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

后端 未结 5 1394
生来不讨喜
生来不讨喜 2021-02-02 11:51

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 forg

5条回答
  •  有刺的猬
    2021-02-02 12:07

    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.

提交回复
热议问题