Open links in Safari instead of UIWebVIew?

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?

Answers


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


Need Your Help

CA2202, how to solve this case

c# .net code-analysis fxcop

Can anybody tell me how to remove all CA2202 warnings from the following code?