hzxu
hzxu

Reputation: 5823

Make phone call on iPhone and take user back to app? (UIWebView does it)

I used this code to make phone call:

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

and when the call ends, it does not take user back to the app. However, if I show a website in UIWebView and there is a phone number in the website(ie UIWebView recognises it), and if I tap that phone number link to make phone call, I will be taken back to the app when the call finishes.

My preliminary thinking is that the UIWebView does something internally like a deep link to the Phone app then another deep link inside the deep link to take the user back to the app. But I'm not sure. Is there a way to implement this feature?

Thanks!

Upvotes: 2

Views: 6954

Answers (2)

eric.mitchell
eric.mitchell

Reputation: 8845

You need to use the telprompt URL, not tel.

So:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

This will also give the user a confirmation box before calling the number.

Edit:

This question covers the same issue.

Edit 2:

For those wondering if this URL will result in rejection from the App Store, the answer is generally no. The greater risk is that Apple will suddenly stop supporting the telprompt scheme. As explained by this article, there is a slightly 'safer' way of implementing telprompt with UIWebView (which uses telprompt internally, even if you call it with tel). The relevant code from the article shows how using the documented tel scheme can still give you the effect of telprompt:

+ (void)callWithString:(NSString *)phoneString {
    [self callWithURL:[NSURL URLWithString:[NSString     
        stringWithFormat:@"tel:%@",phoneString]]];
}

+ (void)callWithURL:(NSURL *)url {
    static UIWebView *webView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{  
        webView = [UIWebView new];
    });
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

(Code taken from the article reference in my second edit)

Upvotes: 12

dwsolberg
dwsolberg

Reputation: 969

The website: http://www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an-ios-app/ has a discussion of whether to use telprompt (undocumented, Apple could potentially change the API without notice), and instead using a category that sends the number to a web view which opens it using telprompt. This has the advantage of not breaking if Apple does something odd.

Upvotes: 0

Related Questions