27

According to Apple's documentation, in order to make phone call from my app, I need to implement the following protocols:

HTML link:

<a href="tel:1-408-555-5555">1-408-555-5555</a>

Native application URL string:

tel:1-408-555-5555

However, upon completion of a phone call initiated from an HTML link inside a UIWebView, I am redirected right back to my application. But upon completion of a phone call made from a native application URL string, my iphone stays in the iphone's regular phone application, and if I want to return to my application I have to do so manually.

As far as I can tell from reading what others have said, there is no way to change this behavior.

Here is my question:

  1. Is it true that it's impossible to return to an application after making a phone call from a native application URL string?
  2. Would there be any downside to implementing a UIWebView instead of a UILabel in situations where I really wanted the user to be redirected back to my application after completing a phone call?
Philip Walton
  • 29,693
  • 16
  • 60
  • 84

4 Answers4

17

The simplest way seems to be:

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

You will get a prompt and your app will regain focus after the call is finished.

Ricky Helgesson
  • 3,596
  • 2
  • 21
  • 23
  • 1
    Did you even test this? You're passing a string when a NSURL object is expected. This is not valid code and even throws a warning at compile time; needless to say it bombs at run-time. – wpearse May 29 '12 at 03:04
16
  1. Behavior does differ between calling -[UIApplication openURL:] with a tel: URL, and clicking a link to the same URL in a UIWebView.

  2. Using a UIWebView instead of a UILabel might have some downsides, but you don't have to actually display the UIWebView to get its tel URL handling behavior. Instead, just load a tel URL request in an instance of UIWebView without adding it to your view hierarchy.

For example:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PhoneCaller : NSObject
{
  @private
    UIWebView *webview;
}
- (void)callTelURL:(NSURL *)url;
@end

@implementation
- (id)init
{
    self = [super init];
    if (self)
    {
        webview = [[UIWebView alloc] init];
    }
    return self;
}
- (void)callTelURL:(NSURL *)url
{
    [webview loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)dealloc
{
    [webview release];
    [super dealloc];
}
@end
lemnar
  • 4,063
  • 1
  • 32
  • 44
  • 1
    this is awesome, thanks! It would have never occurred to me to use the UIWebView just to make the phone call. Another added benefit is it creates an alert, which saves me a step. – Philip Walton May 31 '11 at 17:37
  • 1
    it Worked for iOS 4.x devices .. have you guys tried to do this on iOS 5, apparently it stop working.. Thanks! – Omer Nov 18 '11 at 18:02
  • hey I am loading a web view which has phone number. Webview invokes the call alert on tapping the phone number link. But when user taps on cancel button on the calling screen, my app does not resume back. It displays black screen This happens in IOS5 devices. It was working properly in previous version devices. Please help.. – CKT Dec 12 '11 at 12:26
  • You can also do [[UIApplication sharedApplication] openURL:@"telprompt:0123456789"]; to get the prompt and return to your app afterwards. – Ricky Helgesson May 21 '12 at 10:20
  • User need to click "call" to continue. If there is a way to call automatically? – Bagusflyer Dec 01 '14 at 03:08
10

Allow me to simplify a bit. All you need is this little snippet:

UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:@"tel:number-to-call"];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

which I got here.

*Recently tested successfully on iOS 5.0.

Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
  • when do you release the UIWebView? after the phone call ends and returns to your app? Wouldn't this leak as you've written it? or are you keeping the webview around for lifetime of app and using it for all calls? – Speckpgh Apr 20 '12 at 16:28
  • @leolobato maybe You typed in the wrong number. (Must be without spaces, without " + " etc... Works on all versions on my end. – Guntis Treulands Dec 02 '13 at 12:23
  • Does this work on iOS 7? I just tried it and it didn't seem to work. – theDuncs Mar 27 '14 at 12:28
  • Adding the webview to my view works on iOS 7.1 [self.view addSubview:callWebview]; – bubastis Apr 09 '14 at 14:06
  • Did anyone of you added the webview to the main view before the request? See my answer – 3d0 Aug 29 '16 at 21:28
4

The Eric's Brotto method still works in 5.1. You have to add the webview to the main view before the loadRequest, like this:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", escapedPhoneNumber]];
UIWebView *mCallWebview = [[UIWebView alloc] init]  ;
[self.view addSubview:mCallWebview];
[mCallWebview loadRequest:[NSURLRequest requestWithURL:telURL]]; 

(I added a phone number cleaner, to delete any non-number char that blocks this method)

3d0
  • 290
  • 6
  • 15
  • It works perfectly! thanks a lot!!! Is there any way to customise the popup message displaying just before the phone call? – Claus May 08 '12 at 10:51
  • 1
    Well, not using this way. You see, this is the "legal" (The App won't be rejected at the App Store) if you use this. There is another way: creating your popup and using the old way (calling the openURL using the sharedApplication) but instead of using the "tel" scheme, you have to change it to "telprompt". Notice that this functionality is undocumented, therefore, there is a chance to get your App rejected. – 3d0 May 11 '12 at 16:15