Tuesday, October 30, 2012

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 ofopenURL:, 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.

No comments: