Open links in Safari instead of UIWebVIew? - ios

I have an app with a UIWebView inside a UIViewController. I load HTML from a web service as a string like this:
self.webView loadHTMLString:_string baseURL:nil
Is it possible for the HTML links in this string to be opened in the browser and not in the UIWebView in my app? How can I do this?
I have tried this in the UIViewController that "hosts" the UIWebVIew:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
It doesn't seem to be working....
Any ideas?

Have you set the delegate of the UIWebView to your UIViewController? There's nothing obviously wrong with your code as far as I can see, so it's likely to be something small like that.

Add this in class..
#interface yourViewController : UIViewController
<UIWebViewDelegate>
Add this in View did load
- (void)viewDidLoad
{
[description loadHTMLString:string baseURL:nil];
description.delegate = self;
}
Add this in your .m file
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Note:
UIWebView *description;
#synthesize description;
Then It will work perfectly the way you deserve..!! :)

Set the delegate of the UIWebView to your UIViewController after that use this UIWebview method and check the condition, for example now webview current url is google.com, if suppose you clicked on gmail the url contains the string with gmail. Use the below method it opened a safari browser and automatically loaded that url.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
NSURL *url = [inRequest URL];
if ([[url absoluteString] rangeOfString:#"gmail"].location == NSNotFound) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
return YES;
}

If you already setup properly the UIWebViewDelegate, simply doing
self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;
should work

Add this line (
self.webview.delegate = self;
)
For Example in viewController.m
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlString baseURL:nil];
self.webview.delegate = self;

Apple introduced a Safari View Controller on iOS 9. Safari View Controller brings all of the features user expect from Safari over to your app without ever leaving it.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)

Yes, in your hyperlink tag add
target="blank"
And one question mark will be fine, thank you

Related

iOS webview load another url with Safari

I am making an app for iOS, use UIwebview to load my website in the app. I want all links (besides my website) open in Safari. Read many articles and tried many different suggestion still can't make it.
I am not familiar in coding, use Xcode 6.3.2
Here is my code of ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://www.mywebsite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Check if this was a click event and then some other criteria for determining if you want to launch Safari.
if (navigationType == UIWebViewNavigationTypeLinkClicked
&& [ [ request.URL scheme ] isEqualToString: #"http" ] ) {
[[UIApplication sharedApplication] openURL:request.URL];
// Return false to indicate to the UIWebView to not navigate to the linked target
return false;
}
// Return true so that the UIWebView loads the link target
return true;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
please help
Update your method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
in something like:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestURL = [[request URL] absoluteString];
if ([requestURL rangeOfString:#"mydomain.com"].location != NSNotFound) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}

How to load a link in safari from a pdf being displayed in a UIWebView ios

I am using the Page View Application to show a series of PDF's. The UIWebView is displaying the PDF's fine, however whenever there is a link that is embedded into the PDF and the user clicks it, it opens the link inside of the UIWebView. I want it to open said link in safari.
NSString *path = [[NSBundle mainBundle] pathForResource:page ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
As You can see i have tried to insert the code to launch it but the BOOL never gets run when stepped through.
Use This:-
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked || inType ==UIWebViewNavigationTypeOther) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
UIWebViewNavigationTypeLinkClicked happens if the user taps a style link, if the change is done from within Javascript (onclick event for example) UIWebViewNavigationTypeOther is used.
Furthermore UIWebViewNavigationTypeOther is used if you first load a page or you'll be redirected by a meta refresh or something

iOS UIWebViewDelegates not called

I have a UIViewController class named WebScreenViewController, which is having an UIWebView. Here is the class implementation file:
#implementation WebScreenViewController
#synthesize urlString;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
// Do any additional setup after loading the view.
[self gotoUrl:self.urlString];
//[self gotoUrl:#"http://www.google.com"];
}
#pragma mark - UIWebView Delegates
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = [request URL];
NSString* urlStr = url.absoluteString;
NSLog(#"shouldStartLoadWithRequest. URLStr = %#", urlStr);
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"webViewDidStartLoad");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* html = [webView stringByEvaluatingJavaScriptFromString:#"document.body"];
NSLog(#"webViewDidFinishLoad: HTML string = %#", html);
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"didFailLoadWithError. ERROR: %#", error);
}
#pragma mark - Member Methods
-(void)gotoUrl:(NSString*)urlStr
{
NSLog(#"Calling website %# in UIWebView...", urlStr);
NSURL* url = [NSURL URLWithString:urlStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
#end
The above code work well on one of my testing device but did not work on another. It worked on my iPod Touch with the latest iOS 8.1.2. But it didn't work on my iPad3 with iOS version 7.0. When I run it on my iPad3, it will call the gotoUrl: method and the log "Calling website %# in UIWebView..." will be displayed. And that's it. I receive no more logs from the delegates. To check the internet connections, I tried using Safari on the same device and it was working.
Check whether you've connected the web view's outlet correctly in iPad storyboard if you're using different storyboards for iPhone & iPad.
Also check that the web view is not nil.
Hope this helps..
NSString * strURL = [NSString stringWithFormat:#"http://www.google.com/"];
NSURL *loadURL=[NSURL URLWithString:strURL];
NSURLRequest *loadRequest = [NSURLRequest requestWithURL:loadURL];
[webView loadRequest:loadRequest];
webView.delegate=self;
Try this way in your view did load or if you have URL you can replace first line like this
NSString * strURL = [NSString stringWithFormat:self.urlString];

UIWebView Launch in Safari

I know that this has been brought up many a times and answered twice as many on this site, however I think I may have something a little different and need to know if it is possible.
I am trying to load a banner ad from a website into a UIWebView in the app. All of this works flawlessly. However no matter what code I have tried to implement I cannot get it so that when the ad is clicked in the app it will launch in safari.
Basically I am wanting to have my own Ad server. The ad is managed ad hosted on our site. The banner has the link embedded into it by the server.
Here is the code I am using.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *string;
string = #"http://www.samplesite.com/mobile_ads";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.adBox loadRequest:requestObj];
}
-(BOOL) adBox:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Any ideas of where I should go?
In your UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method, do something like the following (assuming your ads have part of their URL identifiable):
- (void)methodThatCreatesTheWebview {
UIWebView *webview = [[UIWebView alloc] init];
webview.delegate = self;
}
// Portion of the URL that is only present on ads and not other URLs.
static NSString * const kAd = #"adSubdominOrSubDirectory";
// pragma mark - UIWebViewDelegate Methods
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString rangeOfString:kAd] != NSNotFound) {
// Launch Safari to open ads
return [[UIApplication sharedApplication] openURL:request.URL];
} else {
// URL isn't an ad, so just load it in the webview.
return YES;
}
}

How can I link a local pdf file on html file - the html file is showing on UIWebView

I am showing a local html file on my UiWebView using following code.
NSString *path = [[NSBundle mainBundle] pathForResource:#"test.html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath];
[_webView loadHTMLString:htmlString baseURL:baseURL];
[_webView setBackgroundColor:[UIColor clearColor]];
App is showing the html correctly. In the test.html there is a link to local pdf file hello.pdf. This pdf is added to the project. But when I click on the link nothing happens. I want to load pdf on my web view when user clicks on the link.
I want to use the UIWebView delegate to send requests for internet hyperlinks (e.g. http://www.google.com) to the safari app.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
NSLog(#"%#",inRequest.URL.absoluteString);
if([inRequest.URL.absoluteString rangeOfString:#"hello.pdf"].location == NSNotFound)
{
if ( inType == UIWebViewNavigationTypeLinkClicked )
{
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
else
{
return NO;
}
}
You should have stated more clearly that you wanted internet hyperlinks to load in Safari and local hyperlinks to load in your UIWebView.
I've just tested this and it does what you want:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if (inType == UIWebViewNavigationTypeLinkClicked) {
if ([inRequest.URL.absoluteString rangeOfString:#"http://"].location == NSNotFound) {
return YES;
}
else {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
}
else {
return YES;
}
}
This works by only loading links in your UIWebView that don't contain the hyper text type protocol prefix (http://).
Tip: Make sure you've set the delegate for the UIWebView in the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate = self;
}

Resources