Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out whether the user pressed the Call or the Cancel button when making a call from my app?

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?

like image 474
DixieFlatline Avatar asked Dec 06 '12 12:12

DixieFlatline


People also ask

What happens when you decline a call?

If voice mail is enabled, when you click Decline, the call is immediately redirected to voice mail. If you don't answer but don't click Decline, the call is redirected to voice mail when it stops ringing, or, if you don't have voice mail, it is disconnected.

How do you cancel a call on iPhone?

Decline a call and send it directly to voicemailPress the side button twice quickly.

How do I reject calls automatically on iPhone?

Go to Settings/Phone. There's a switch for Silence Unknown Callers. If it is on any calls from numbers not in your contacts will go directly to voicemail.

How do you hang up a call on iPhone?

Launch the Settings app on your device. Tap Accessibility. Scroll down to the bottom and under "General," tap Siri. Scroll down to the bottom and tap Call Hang-up.


1 Answers

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")
    }
}
like image 171
Tamás Sengel Avatar answered Sep 30 '22 05:09

Tamás Sengel