SuperString
SuperString

Reputation: 22519

confirm user wants to call a phone number after click

I want to show a pop up that asks if the user wants to call the number when the user clicks on a number.

How do I do that? Currently, when I click on the number, it calls it automatically.

Upvotes: 4

Views: 5898

Answers (5)

SAHIL AMRUT AGASHE
SAHIL AMRUT AGASHE

Reputation: 339

You can follow this code. There will be system confirmation prompt for calling provided mobile number.

    @objc func pressMeAction() {
    let str = "telprompt://1234567890"
    let url = URL(string: str)!
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url)
    }
}

Upvotes: 0

Ricky Helgesson
Ricky Helgesson

Reputation: 3586

You can also do:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt:0123456789"]];

to get the prompt and return to your app afterwards.

Upvotes: 10

Bdebeez
Bdebeez

Reputation: 3552

I think what you're looking for is something like what UIWebView does automatically to phone numbers. It will pop a UIAlertView to ask if the user would like to call the number before dialing out. To do that, make your class a UIAlertViewDelegate and do the following:

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: nil
                            message: phonenum
                            delegate: self
                            cancelButtonTitle:@"Cancel"
                            otherButtonTitles:@"Call",nil];
[alert show];
[alert release];

In addition, add the following method to the same class:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"tel://%@", phoneNum]]];
    }
}

The alert view will call back to this method when the user interacts with it. The buttonIndex == 0 ensures that you only call the number when the user hits the Call button.

Upvotes: 3

Legolas
Legolas

Reputation: 12325

You should use the UIAlertView method. The documentation is provided in this link . I would suggest that you have a look at that documentation and everything is self explanatory.

But you may require something like this.

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Really call?" message:@"Do you want to call this number?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
// optional - add more buttons:
[alert addButtonWithTitle:@"Yes"];
[alert show];

Upvotes: 0

max_
max_

Reputation: 24481

Create a UIAlertView with the delegate set to self, and if the selectedButtonIndex is the buttonIndex of the yes button in the alert, call the number, if not, don't call the number.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Do you want to call..." delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert setTag:02];
[alert show];
[alert release];

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (alertView.tag == 02 && buttonIndex != alertView.cancelButtonIndex) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://phonenumber"]];
    }
} 

Upvotes: 3

Related Questions