11

I need to return to my app after making a call from my app, so I use this code:

NSURL *url = [NSURL URLWithString:@"telprompt://123-4567-890"]; 
[[UIApplication  sharedApplication] openURL:url]; 

When the user presses a button to execute this code, an alert is shown with 2 buttons, "Call" and "Cancel".

How can I find out if the user pressed the "Call" button?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
DixieFlatline
  • 7,895
  • 24
  • 95
  • 147

6 Answers6

13

Here is an iOS 10+ solution for Swift 4.2, tested with iOS 12 that detects both the Cancel and the Call button.

Don't forget to import CallKit and conform your class to CXCallObserverDelegate!

let callObserver = CXCallObserver()

var didDetectOutgoingCall = false

func showCallAlert() {
    guard let url = URL(string: "tel:+36201234567"),
        UIApplication.shared.canOpenURL(url) else {
            return
    }

    callObserver.setDelegate(self, queue: nil)

    didDetectOutgoingCall = false

    //we only want to add the observer after the alert is displayed,
    //that's why we're using asyncAfter(deadline:)
    UIApplication.shared.open(url, options: [:]) { [weak self] success in
        if success {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                self?.addNotifObserver()
            }
        }
    }
}

func addNotifObserver() {
    let selector = #selector(appDidBecomeActive)
    let notifName = UIApplication.didBecomeActiveNotification
    NotificationCenter.default.addObserver(self, selector: selector, name: notifName, object: nil)
}

@objc func appDidBecomeActive() {
    //if callObserver(_:callChanged:) doesn't get called after a certain time,
    //the call dialog was not shown - so the Cancel button was pressed
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
        if !(self?.didDetectOutgoingCall ?? true) {
            print("Cancel button pressed")
        }
    }
}

func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
    if call.isOutgoing && !didDetectOutgoingCall {
        didDetectOutgoingCall = true

        print("Call button pressed")
    }
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
9

This isn't perfect, but you could identify that "call" was pressed instead of "cancel" by listening for the UIApplicationSuspendedNotification (you'd have to add some logic to ignore this event when someone pushed the 'home' key, or was accepting an incoming call... maybe by adding/removing the observer around the logic where the phone number is being presented):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suspended:) name:@"UIApplicationSuspendedNotification" object:nil];

-(void)suspended:(NSNotification *) notification
{
    NSLog(@"Suspended");
}
J Shapiro
  • 3,861
  • 1
  • 19
  • 29
  • 2
    exactly what I needed except that is seems your notification doesnt exist anymore in iOS6 use: UIApplicationDidEnterBackgroundNotification – Nicolas Manzini Jul 19 '13 at 13:37
  • @NicolasManzini: I tried UIApplicationDidEnterBackgroundNotification, but seems not working in iOS8 – BaSha Dec 10 '14 at 10:35
3
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suspended:) name:@"UIApplicationSuspendedNotification" object:nil];

The above code by @J Saprio works absolutely fine. Before it suspends the Application NSNotificationCenter call UIApplicationSuspendedNotification. Once you click the "call" it will execute the (suspended:) method. Here the catch is, every time you call NSNotificationCenter method it will give a call to (suspended:) with the increment of one. Solution is to remove the observer for NSNotificationCenter. Following is the snippet for it. It will help you to execute the following method only once.

-(void)suspended:(NSNotification *) notification
{
    NSLog(@"Suspended");
   [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIApplicationSuspendedNotification" object:nil];

}
GameBegins
  • 580
  • 7
  • 12
2

As per iOS terms it is not possible. when completing the call can't redirect to Application.

In Jailbroken Phones only it's possible.

1

When application executes above code, it will exit current application & navigate to call application of iPhone. So, it is not possible for the application to identify whether the user has pressed call or cancel. Hope,it will help you.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

Try this,I deal with this issue as follows:

in -(void)viewDidLoad add the following :

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserverForName:UIApplicationDidBecomeActiveNotification
                    object:nil
                     queue:[NSOperationQueue mainQueue]
                usingBlock:^(NSNotification *note)
 {
     // use instance flag to know
     if (self.isBeginCallPhone)
     {
         //do some thing
         self.isBeginCallPhone = NO;
     }
 }];

Do not forget:

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
  }
cx_hi
  • 1
  • 2