treasure
treasure

Reputation: 423

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?

Upvotes: 27

Views: 29149

Answers (7)

Vincent Joy
Vincent Joy

Reputation: 2678

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)

Upvotes: 0

Carlos Parada
Carlos Parada

Reputation: 106

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;

Upvotes: 0

Rajesh_Bangalore
Rajesh_Bangalore

Reputation: 609

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

Upvotes: 21

Peter
Peter

Reputation: 1

If you already setup properly the UIWebViewDelegate, simply doing

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

should work

Upvotes: 0

Karthik
Karthik

Reputation: 747

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

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52565

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.

Upvotes: 23

M. Ryan
M. Ryan

Reputation: 7182

Yes, in your hyperlink tag add

target="blank"

And one question mark will be fine, thank you

Upvotes: -3

Related Questions