4

I want to make a phone call and I use below code for that.

- (void)callPhoneNumber:(NSString *)phoneNumber
{
    UIWebView * webView2 = [[UIWebView alloc] init];

    // Remove non-digits from phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Make a call
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
    [webView2 loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView2];
}

Before making a call I ask user to choose an option using UIActionSheet. When user selects an option, based on that I place a call.

Issue is that above code works fine if I don't display UIActionSheet and directly place a call. It prompts an alert asking user's confirmation to make a call. But after displaying UIActionSheet if I call above method then it does not display confirmation alert.

Weird thing is that after this if I stop app from Xcode, then app is put ion background as it should. But also I see that confirmation alert on device's home screen. How it is displayed on device's home screen and not in app?

Has anyone faced it before? What can be the reason?

EDIT : I also tried below code. But it has same issue. Whenever above webview code works below code also works. Same for when it does not work.

NSString *phoneNumberURL = [NSString  stringWithFormat:@"telprompt:%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURL]];

Edit : Adding code for action sheet and delegate.

I first display an alert. On pressing alert button I display action sheet. And choosing option from action sheet I place a call.

# pragma mark - ALERT VIEW DELEGATE

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == MY_TAG)
    {
        // Ask to select option
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Option" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Option1", @"Option2", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
        [actionSheet showInView:self.view];   
    }
}


#pragma mark - UIActionSheet Methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // If not pressed cancel button.i.e. selected one of the options
    if (buttonIndex != actionSheet.cancelButtonIndex)
    {
        // Place a call
        [self callPhoneNumber:@"1234567890"];
    }
}
Geek
  • 8,280
  • 17
  • 73
  • 137

6 Answers6

1

If there are any whitespace in your number it will not work so this needs to be removed, also when using tel: and telprompt: it needs to be tel:// or telprompt:// note the extra //.

I have commented my code with explanation of each step if you need anything else just comment and I will provide more.

- (void)callPhoneNumber:(NSString *)phoneNumber
{
    // If statement to check we actually have something
    if(phoneNumber) {
        // Remove any unwanted whitespace, if there is any whitespace it will not work
        phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
        // Create an NSURL instance of your number
        // Note the extra `//` in the string
        // Also note you can also use: `telprompt://` instead of `tel://`
        NSURL *urlPhoneNumber = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phoneNumber]];
        // Check to make sure we can open this because iPad and iPod will not respond to phone calls
        if([[UIApplication sharedApplication] canOpenURL:urlPhoneNumber]) {
            // If we can (So if iPhone really) make the call.
            [[UIApplication sharedApplication] openURL:urlPhoneNumber];
        }
    }
}

Edit Extra Notes

Have a read of the Apple documentation around this here

I will note that the Apple Documentation says that tel: and telprompt: should work and no need for the extra // but I have found that it still works with the extra //. This does confuse me a little as technically tel: and telprompt: are URL Schemes and the extra // are required in a `URL Schemes. Here is a good tutorial on them by iPhone Development 101

Here is a bit of additional information

Popeye
  • 11,839
  • 9
  • 58
  • 91
0

Use OpenURL to make call using URL.

- (void)callPhoneNumber:(NSString *)phoneNumber
{
    // Remove non-digits from phone number
    phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

    // Make a call
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumber]];
    [[UIApplication sharedApplication] openURL:url];
}
Geek
  • 8,280
  • 17
  • 73
  • 137
Savitha
  • 561
  • 6
  • 19
0

Instead of using UIWebView, try the following:

NSURL *phoneUrl = [NSURL URLWithString:[NSString  stringWithFormat:@"telprompt:%@",phoneNumber]];

if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
    [[UIApplication sharedApplication] openURL:phoneUrl];
}

Edited: Check whether the phone calling function is called or not by placing the breakpoint. If that phone calling function is not called, replace the action sheet's button index checking function as follows:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
         //Code for cancel button
    }
    if (buttonIndex == 1)
    {
       //Code for calling phone call
       [self callPhoneNumber:@"1234567890"];
    }
}

Edited 2:

Now put it this way:

  - (void)checkCallAlert :(NSInteger)index
 {
    UIAlertView *checkCallAlert = [[UIAlertView alloc] initWithTitle:@"Check?" message:@"Are you sure you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil];
    [checkCallAlert setTag:index];
    [checkCallAlert show];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
         //Code for cancel button
    }
    if (buttonIndex == 1)
    {

       [self checkCallAlert : buttonIndex];


    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if(alertView.tag == 1){
   //Destination 1 clicked
    //Code for calling phone call
       [self callPhoneNumber:@"1234567890"];
}
}

This would be my final solution for this.

Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29
  • @Geek , if that's not the case then there must be something wrong with the UIActionSheet, can u please show how you have implemented the UIActionSheet view and it's delegate method. – Bikram Thapa May 09 '14 at 10:53
  • @Geek, See my edited 2: answer.I guess this will definately solve this. – Bikram Thapa May 12 '14 at 10:14
  • @Geek Sorry for the glitch, now it's the final one.this implementation of alertviewdelegate should work. – Bikram Thapa May 12 '14 at 10:33
0

Try using following instead of [self callPhoneNumber ... ] to invoke callPhoneNumber after completion current update loop:

[self performSelector:@selector(callPhoneNumber:) withObject:@"1234567890" afterDelay:0];

Also the UIActionSheet reference document states:

In iOS 4.0 and later, action sheets are not dismissed automatically when an application moves to the background

So you need to check in applicationWillResignActive if actionSheet is being displayed and dismiss it programmatically.

rioki
  • 5,988
  • 5
  • 32
  • 55
jamihash
  • 1,900
  • 1
  • 12
  • 15
0
NSString * telephoneNumber;

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telephoneNumber]];

[telephoneNumber release];
Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
0

Try calling in didDismissWithButtonIndex:, so that the UIActionSheet is already dismissed, maybe it is blocking something.

iCaramba
  • 2,589
  • 16
  • 31