Return to app behavior after phone call different in native code than UIWebView - iphone

According to Apple's documentation, in order to make phone call from my app, I need to implement the following protocols:
HTML link:
1-408-555-5555
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:
Is it true that it's impossible
to return to an application after
making a phone call from a native
application URL string?
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?

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.

Behavior does differ between calling -[UIApplication openURL:] with a tel: URL, and clicking a link to the same URL in a UIWebView.
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

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.

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)

Related

Press a Button and open a URL in another ViewController

I am trying to learn Xcode by making a simple app.
But I been looking on the net for hours (days) and I cant figure it out how I make a button that open a UIWebView in another ViewController :S
first let me show you some code that I have ready:
I have a few Buttons om my main Storyboard that each are title some country codes like UK, CA and DK.
When I press one of those Buttons I have an IBAction like this:
- (IBAction)ButtonPressed:(UIButton *)sender {
// Google button pressed
NSURL* allURLS;
if([sender.titleLabel.text isEqualToString:#"DK"]) {
// Create URL obj
allURLS = [NSURL URLWithString:#"http://google.dk"];
}else if([sender.titleLabel.text isEqualToString:#"US"])
{
allURLS = [NSURL URLWithString:#"http://google.com"];
}else if([sender.titleLabel.text isEqualToString:#"CA"])
{
allURLS = [NSURL URLWithString:#"http://google.ca"];
}
NSURLRequest* req = [NSURLRequest requestWithURL:allURLS];
[myWebView loadRequest:req];
}
How do I make this open UIWebview on my other Viewcontroller named myWebView?
please help a lost man :D
Well, you've firstViewController already designed and coded, now you've to create secondViewController with a UIWebView binded in IB it self, also it have a setter NSString variable like strUrl that you need to pass at the time of pushing or presenting secondViewController, and assign it to UIWebView in viewDidLoad of secondViewController. See my answer about how to pass a NSString? Also UIWebView has its delegates method (What's delegate & datasource methods? - Apple Doc) Which you can use to handle URL request.
These are the delegate of UIWebView, if you'll going to use it, you need to give UIWebView delegate to self.
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
firstViewController.m
- (IBAction)ButtonPressed:(UIButton *)sender {
NSURL* allURLS;
//get your URL
secondViewControlelr *secondView=[[secondViewControlelr alloc]init];
second.urlToLoad=allURLS;
[self.navigationController pushViewController:secondView animated:YES];
}
secondViewControlelr.h
//declare url and set property
NSURL *urlToLoad;
secondViewControlelr.m
- (void)viewDidLoad
{
myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myWebView];
NSURLRequest *urlReq=[NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
[myWebView loadRequest:urlReq];
}

HTML, Javascript and CSS as an App

I was wondering whether one could run a HTML page from the iOS device's hard drive, using Objective-C only as a view port to the HTML page and having the HTML page be the application's actual interface. Is this possible to do?
Thanks,
Odinulf
for IOS create a file with the following:
#import "WrapperViewController.h"
#implementation WrapperViewController
- (void) viewDidLoad
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
NSString *fullpath = [NSBundle pathForResource:#"index" ofType:#"htm" inDirectory:path];
[webView loadRequest:[NSURLRequest requestWithURL: [NSURL fileURLWithPath:fullPath]]];
}
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
return YES;
}
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
{
webView.scalesPageToFit = YES;
WebView.backgroundColor = [UIColor blackColor];
}
- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void) dealloc
{
[super dealloc];
}
#end
(I got this straight out of the book "HTML5 for IOS and Android: A Beginner's Guide" [page 388-390])
And this is how you would do it for android.
Hope this helps!
Yes it is possible. You need to subview a UIWebView and then load a request pointing to the HTML file in your local directory instead of a web URL. All of your web pages need to be added to the application bundle.
UIWebView *webview;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"page_name" ofType:#"html"]isDirectory:NO]]]
Yes it is possible. But, be careful, apple sometimes refuses it. This is what we call Web application.
As Matt points out, take a look at PhoneGap or Sencha Touch
http://phonegap.com/
http://www.sencha.com/products/touch/
Sounds like http://phonegap.com is what you are looking for.
I've worked with phonegap with Android and am impressed with it. Haven't tried it with IOS but here's the link PhoneGap

Launch an application after the call ends

I am developing an application in which, I can initialize a call from the application. Is it possible to come back to the same place in the application where I was before the call, after the call is terminated? If it is possible, then how? Thank you in advance.
I believe you can not do what you are trying to do. Because once the user attends the call and terminates the call, the application currently active would be Dialer and you can not in no way make you application active.
Implement the following methods from the UIApplicationDelegate protocol:
applicationWillResignActive is called when the phone receives an incoming call
applicationWillTerminate is called when the user answers the call
applicationDidBecomeActive is called if the user choose not to answer the call
applicationWillTerminate will give a few seconds to save your apps current state.
Use NSUserDefaults to save state . When the app starts again you read your state from NSUserDefaults and restore the app to its previous state.
If you want to come back to your application after a phone call, need to change
[UIApplication sharedApplication] openURL:telURL];
to
UIWebView PhoneCallWebView = [[UIWebView alloc] init];
[PhoneCallWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
and it works.
I hope this will help you...and this will back after in application after call ended.
NSString *aPhoneNo = #"9876543210";
NSURL *url= [NSURL URLWithString:aPhoneNo];
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
if ([osVersion floatValue] >= 3.1)
{
UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
webview.hidden = YES;
// Assume we are in a view controller and have access to self.view
[self.view addSubview:webview];
[webview release];
}
else
{
// On 3.0 and below, dial as usual
[[UIApplication sharedApplication] openURL: url];
}

Cannot load alternating urls with UIWebview

I have one view holding a list of videos(buttons which can be clicked, each hold a url) which currently bring up a new view which holds the UIWebview, the only problem is I cant pass the url to the webview depending on which button was clicked.
At the moment I just get an empty UIWebview.
How can the url string be passed to the webview so it can load the correct url for each button?
Regards
NSURL *videoURL = [NSURL URLWithString:#"http://..."];
[webView loadRequest:[NSURLRequest requestWithURL:videoURL]];
UPDATE:
In response to comment #3, here's what you can do:
This goes without saying, but keep a reference to the UIWebView instance in Browser
Add an NSString or NSURL #property to Browser
Pass the URL to the Browser instance right after init
In viewDidLoad:, call loadRequest:
So your code might look like this:
- (void)buttonClicked {
Browser *browser = [[Browser alloc] initWithNibName:nil bundle:nil];
browser.URL = [NSURL URLWithString:#"http..."];
[self presentModalViewController:browser animated:YES];
[browser release];
}
and in Browser's viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
}

How to open a UITextView URL in UI Web View?

In my iPhone app an UITextView is containing an URL. I want to open this URL in an UIWebView instead of opening it into safari?
My UITextView contains some data along with an URL. In some cases the no. of URLs can be more than one.
Thanks
Sandy
You can follow these steps:
Tick on the following properties in UITextView taken from Xib or Storyboard.
OR write these for textview taken dynamically.
textview.delegate=self;
textview.selectable=YES;
textView.dataDetectorTypes = UIDataDetectorTypeLink;
Now write the below delegate method :
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(#"URL: %#", URL);
//You can do anything with the URL here (like open in other web view).
return NO;
}
I think you are searching for that.
The UITextView has the capability to detect URLs and embed hyperlinks accordingly. You can turn that option on in:
myTextView.dataDetectorTypes = UIDataDetectorTypeLink;
Then you need to configure your app to trap this URL request and let your application handle it. I published a boilerplate class on github that does this, which might be the easiest route: http://github.com/nbuggia/Browser-View-Controller--iPhone-.
The first step is to sub-class UIApplication so you can override who gets to take action on the 'openUrl' request. Here's what that class might look like:
#import <UIKit/UIKit.h>
#import "MyAppDelegate.h"
#interface MyApplication : UIApplication
-(BOOL)openURL:(NSURL *)url;
#end
#implementation MyApplication
-(BOOL)openURL:(NSURL *)url
{
BOOL couldWeOpenUrl = NO;
NSString* scheme = [url.scheme lowercaseString];
if([scheme compare:#"http"] == NSOrderedSame
|| [scheme compare:#"https"] == NSOrderedSame)
{
// TODO - Update the cast below with the name of your AppDelegate
couldWeOpenUrl = [(MyAppDelegate*)self.delegate openURL:url];
}
if(!couldWeOpenUrl)
{
return [super openURL:url];
}
else
{
return YES;
}
}
#end
Next, you need to update main.m to specify MyApplication.h as being the bonified delegate for your UIApplication class. Open main.m and change this line:
int retVal = UIApplicationMain(argc, argv, nil, nil);
to this
int retVal = UIApplicationMain(argc, argv, #"MyApplication", nil);
Finally, you need to implement the [(MyAppDelegate*) openURL:url] method to have it do what ever you would like with the URL. Like maybe open up a new view controller with a UIWebView in it, and show the URL. You could do something like this:
- (BOOL)openURL:(NSURL*)url
{
BrowserViewController *bvc = [[BrowserViewController alloc] initWithUrls:url];
[self.navigationController pushViewController:bvc animated:YES];
[bvc release];
return YES;
}
Hopefully that should work for you.
Assuming you have the following instances, that are also added to your UIView:
UITextView *textView;
UIWebView *webView;
and textView contains the URL string, you can load the contents of the URL into webView, as follows:
NSURL *url = [NSURL URLWithString:textView.text];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];