Bring Users Back to your Application after they End the Call

Often times, developers want to allow their users to place calls from inside their applications. Although, Apple has provided a pretty neat function openURL: for doing that, but it has one disadvantage…rather than bringing the users back to the application, it takes the users to the default Phone application after they end the call. It can be frustrating to both the developers and the users; developers want to give uninterrupted use of their applications whereas users hate to have to close the Phone app and go back to what they were doing. If you are one of the afore mentioned developers, you are reading just the post. In this small pots, Im showing a way around this…as always, totally acceptable by Apple, no private APIs or hacks that may get your app rejected.

No magic here, just a little observation; if you have ever made a call from Safari, you would have noticed that ending the call takes you back to Safari. Like me, you must have believed that Apple has added this custom behavior to Safari which is not accessible to us, bla bla bla…..actually thats not the case. This is UIWebView magic and if you use UIWebView to make calls instead of openURL:, you would get the same behavior. Lets see how to do that.

Using UIWebView to Place Calls

Simply, open your tel URL in a UIWebView. Like this

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

See, you dont even have to add the webview to your UI. Isn't this awesome?? Whats more awesome is, it displays an alert for confirmation before actually placing the call ;)

Feel free to leave a comment and wander around on my blog to find out more interesting stuff. You can also follow me here or on twitter (@xs2bush) to stay updated. Happy Coding :)

Update: The second line of code was missing a semi colon, thanks Ken for pointing that out. Also the UIWebView object was accesses using wrong variable name. All is correct now! I apologize for any inconvenience that it has might caused anyone :) I have tested it on my iPhone and it works perfectly.

About these ads

41 Comments on “Bring Users Back to your Application after they End the Call”

  1. Awesome!! Thanks a lot mate!

    This is really helpful.

    Cheers!

  2. Ken says:

    You mentioned we don’t have to add the webview to our UI, but when I add your code to my working dial method, I get an undeclared identifier error for “webview”. I’m in the process of teaching myself Objective-C, so this may be silly issue I’m raising. Your thoughts?

  3. Ken says:

    Bushra,

    Could you please tell me why my code below keeps generating the following error: “Receiver type `UIWebView` for instance message does not declare a method with selector `loadrequest:`? As you can see, I’m trying to implement your UIWebView idea when I press the dial button. I’m sure I’m missing something simple and would really appreciate your help. Thanks in advance. BTW, I’m using Storyboarding to create and implement my UI. Lastly, was your second and third line of code above supposed to be a single line or were you simply missing the “;”?

    -(IBAction)dialButtonPressed:(UIButton *)numberButton{
    UIWebView *callWebView = [[UIWebView alloc] init];
    NSString *phoneLinkString = [NSString stringWithFormat:@"tel:%@", self.phoneNumberString];
    NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString];
    [callWebView loadrequest:[NSURLRequest requestWithURL:phoneLinkURL]];

    }

  4. Ken says:

    Bushra,

    Thank you very much for quickly identifying my frustrating and classic case oversight–at least it wasn’t my logic. Also, you mentioned that no semicolon was missing on line 2 of your code above. I’m assuming that lines 2 and 3 are really a single line with a wrap–right? However, I’ve tried combining them into a single line with the existing configuration of brackets, but cannot do so without Xcode continuously reporting error such as, “Expected [“. The only way I’ve been able to integrate your sample code is by placing a semicolon at the end of each line. What could I be doing wrong here? Thanks again.

  5. Ken says:

    Bushra,

    I could really use some help with integrating your code suggestion. Currently, as I mentioned, above, I have added your code as three separate lines (haven’t been able to get it to work as a single line). When I execute my phone app via your recommended method, my app crashes. When I go back to using Apple’s Phone app to dial, my app doesn’t crash. Can you point me to some additional resources? I’d sure like some clear direction on lines 2 and 3–is this one line of code or supposed to be 2? Thanks in advance.

  6. Ken says:

    Bushra,

    I meant to write in my mast comment that I haven’t been able to get your code to work as two lines of code–I erroneously wrote “as a single line of code.”

    • xs2bush says:

      I apologize for my earlier oversight. The semi colon was indeed missing and your code that you posted in the comment should work fine with the loadRequest thing corrected. Hope you get this to work :)

  7. Ken says:

    Thank you for letting me know. I did get past the semicolon thing and a few other bugs by reviewing some other examples. At this point my method runs without errors and, unfortunately, without initiating the dialing via UIWebView. I have created a telephone interface within a storyboard-based view controller–it was part of a tutorial I went through to help learn IOS development. You type in your number and press the dial button. My method worked fine by calling the Phone app, but I haven’t yet gotten it to dial via UIWebView. As I mentioned, no bugs, but also no action. I’ve verified my local variables contain the necessary string/URL, but nothing happens with I press the dial button. I noticed in your profile that you review/test apps for other developers. Would you have time to review my small app? If so, please let me know the best method for sending you a copy. And, if not, please recommend someone. Thanks very much for your time.

  8. Mir says:

    Infact this is nice but it is asking user for call and cancel with UIAlertView.

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[number stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

    (OR)

    UIWebView *callWebview = [[UIWebView alloc] init];
    NSURL *telURL = [NSURL URLWithString:];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

    Notice the difference.

  9. Nathan says:

    I tried setting the callWebView up as autorelease in my app, this however seems to cause it to not display the dialer. Is this memory released automatically or does it end up being a leak?

  10. saravanan says:

    after the call ends, i want to resume my app without showing the alert? is it possible? if anyone say not thats not the true answer. because “google voice” doing the same dial function without showing the alert i.e “cancel” or “call”.

  11. Nice post!
    Question though: Does this work with IOS5? It doesn’t seem to do anything in my tests. I was using the standard method of dialing from my app using this, [[UIApplication sharedApplication] openURL:url]. However, completed calls would not return to my app. Found your post, and tried to implement the call with the UIWebView, but nothing happens when I attempt the call now. The phone app doesn’t launch.
    Are you experiencing anything similar?

  12. David says:

    What a golden needle in the haystack! Thank you for sharing this.

    Has anyone discovered how to do this with Mail or Messages?

  13. Arthur says:

    Thank you for an interesting article.
    I am a complete newb, and have created a simple web app using iui to dial numbers from a directory. After using Phonegap and Xcode to create an iPhone app I submitted it to the Apple app store. The rejection said that the app didn’t work on an iPhone 4 running OS 5.1.

    After a relatively brief period of disbelief based on the fact that the pre-submission build worked just fine on my iPhone 4s / OS5.1, I re-read the iui documentation and looked at iui.js and concluded that the iui.js intercepts native actions and does nothing with them. And since the provisional testing app is different (saysApple) from the final uploaded binary, my href=tel:000-000-0000 didn’t work.

    (Thank you for still reading . . . .) So after reading your article in my search for an answer, I thought you might have experiences that would put me on track to enable native functions/behaviors. Any ideas? Thank you!

  14. jjxtra says:

    Does not appear to work in iOS 5.1

  15. Bocephus says:

    As you stated an alert view pops up automatically showing the Tel # and asking user to cancel or call. How can I set the title IN THE ALERT VIEW above the Tel #?

  16. estevez says:

    Great post !.. any way to even get rid of the prompt to call ?

  17. Riddick says:

    Awesome discovery! Thanks mate!!

  18. Joel Parker says:

    After returning from the call how can I be notified in my code, so that I can execute some more logic ? I seem to see the following events in my application delegate when making the call applicationWillResignActive, applicationDidBecomeActive, applicationDidEnterBackground but never seem to get a applicationWillEnterForeground or applicationDidBecomeActive after the call completes.

  19. kalyani says:

    hi, Is there any way to remove conformation alert while using webview?

  20. venkat says:

    Hi, is there any way to remove alert before doing call while using webview?

  21. Prashanth says:

    Thanks :) It helped me a lot :)

  22. Terry In San Diego says:

    THANK YOU! This is the ONLY way to programmaticaly dial the asterisk and hash!

  23. Govind says:

    Thank you.

    I’d like to know if there’s a way to skip the alert and directly make the phone call.
    If not, is there anyway where the application can automatically select “call” when prompted?

    Thanks

  24. Samvel says:

    It is really good, to auto return to app after making call. But as you know when trying to make call from safari, a confirmation dialog appears. How can we skip that?

  25. SNEHA says:

    But if I have set UIApplicationExitsOnSuspend to true then this solution won’t work.Is there any alternative.Please reply

  26. Zoeb S says:

    Is there any way to disconnect the call programatically?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 49 other followers