Reputation: 65
I am a beginner in making iOS applications. I have made a simple webview showing my web page. The problem is that every link that is pressed in my web page opens in the webview. I want some links to open in safari. I would like links starting with "..something" to be opened inside the webview and every other link to be opened in safari. I also have a button for email and dial which i want to open in the dial app and email app on the phone. Is this a possibility? please explain simple.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize webView;
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:@"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
I made the same application for android using java with this code below
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try{
System.out.println("url called:::" + url);
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
} else if (url.startsWith("http:")
|| url.startsWith("https:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} else if (url.startsWith("mailto:")) {
MailTo mt=MailTo.parse(url);
send_email(mt.getTo());
}
else {
return false;
}
}catch(Exception e){
e.printStackTrace();
}
return true;
}
}
Upvotes: 1
Views: 1642
Reputation: 17307
You'll need to make your controller a UIWebViewDelegate
and implement the webView: shouldStartLoadWithRequest:navigationType:
method.
@interface ViewController () <UIWebViewDelegate>
viewDidLoad
should look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:@"http://MyWebPage"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
webView.delegate = self;
[webView loadRequest:requestURL];
}
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked && [self shouldOpenInSafari:[inRequest URL]]) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
- (BOOL)shouldOpenInSafari:(NSURL*)url
{
if ([url.scheme isEqualToString:@"mailto"]) {
return YES;
}
else if ([url.scheme isEqualToString:@"tel"]) {
return YES;
}
else if (([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"]) && [url.host isEqualToString:@"example.com"]) {
return YES;
}
return NO;
}
Then you'll need to implement the shouldOpenInSafari:
method. The openURL:
method can also handle tel:
and mailto:
links.
Some code from here.
Upvotes: 1
Reputation: 654
For the urls which you want to open in web view, use the same code which you have.
For opening in safari, use this
NSString* launchUrl = @"URL to open in safari";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: launchUrl]];
Upvotes: 1