Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open link in Safari from an HTML hosted in a web browser control?

We have an iphone app that hosts a web browser control which points to a website whose sole purpose is to display information for the application. I would like to know if it is possible to have an anchor/button open Safari using HTML/JavaScript. If it is possible, how so?

Thanks...

like image 970
Jonas Stawski Avatar asked Dec 01 '25 14:12

Jonas Stawski


2 Answers

You can setup a delegate for the UIWebview you are using. In this delegate, write something like this:

-(bool) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //You might need to set up an "interceptLinks"-Bool since you don't want to intercept the initial loading of the content
    if (self.interceptLinks) {
        NSURL *url = request.URL;
        //This launches Safari with your URL
        [[UIApplication sharedApplication] openURL:url];   
        return NO;
    }
    //No need to intercept the initial request to fill the WebView
    else {
        self.interceptLinks = TRUE;
        return YES;
    }
}

If you only want to intercept some links, you can parse the url and only open Safari if necessary.

like image 192
Philipp Schlösser Avatar answered Dec 03 '25 05:12

Philipp Schlösser


Set the delegate property of your UIWebView object (what you call the 'web browser control') to an object of your own that implements the method "webView:shouldStartLoadWithRequest:navigationType:" of the UIWebViewDelegate protocol:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intf/UIWebViewDelegate

You can then detect if you desired URL is being requested and in that case open it on an external Safari instance using UIApplication's openURL method.

like image 40
Julio Gorgé Avatar answered Dec 03 '25 05:12

Julio Gorgé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!