27

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?

mins
  • 6,478
  • 12
  • 56
  • 75
treasure
  • 423
  • 1
  • 5
  • 15
  • The code you've posted should work, assuming that [request URL] is a type of URL that Safari (or some other application on the iOS device) can handle. Can you post an example of the one of the URLs that a user would tap? – Greg Dec 15 '10 at 17:14

7 Answers7

23

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.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • yes i did. but i did add this as follows: not surei f it is wrong or right but ut seems to be working: webView.delegate = self; is it wrong to add this??? – treasure Dec 15 '10 at 18:32
  • 2
    Adding `` to the class definition just means that you implement the protocol. It doesn't mean that it _is_ the delegate, only that it _can_ be. You do need to the `webView.delegate = self` (or you can do that in Interface Builder). – Stephen Darlington Dec 15 '10 at 20:53
  • @Yar The answer already says that. The comment is clarifying the difference between the `UIWebViewDelegate` protocol and the `delegate` property. – Stephen Darlington Jan 03 '12 at 07:43
21

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..!! :)

propstm
  • 3,461
  • 5
  • 28
  • 41
Rajesh_Bangalore
  • 613
  • 5
  • 21
  • Be sure to capitalize the V in webView like: -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType – dinjas Dec 07 '12 at 23:12
  • I am using UIWebView to display quotes from the web with a link at the end of each quote to the source. This worked for me to open links in Safari rather than in the UIWebView. – JScarry Feb 13 '14 at 22:52
1

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;
}
Karthik
  • 747
  • 4
  • 21
  • 48
0

If you already setup properly the UIWebViewDelegate, simply doing

self.webView loadHTMLString:_string baseURL:nil
self.webView.delegate = self;

should work

Peter
  • 1
0

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;
0

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)
Vincent Joy
  • 2,598
  • 15
  • 25
-3

Yes, in your hyperlink tag add

target="blank"

And one question mark will be fine, thank you

M. Ryan
  • 6,973
  • 11
  • 52
  • 76
  • 4
    Setting `target="blank"` or `target="_blank"` does seem to have no effect. On iOS 4.3 links still open inside the UIWebView. – Palimondo Sep 09 '11 at 22:39