简体   繁体   中英

How to force UIWebView to open link in browser?

We have a site. In some cases users can open in from Facebook App or VKontakte App using built-in browser. And there is a link which I want to open in Safari or another browser. But not in this WebView.

Is any HTML property or something that can tell the WebView not top open this link in itself?

UPDATE: I am not a App developer. I am web developer. And I do not have any access to Facebook App. I need to force Facebook App to open links not in its built-in browser.

You can use UIWebViewDelegate:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if(UIWebViewNavigationTypeLinkClicked == navigationType /*you can add some checking whether link should be opened in Safari */) {

        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}

Web view contains some delegate methods to find the click event from that delegate method you need to call the URL like this

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];

This is the delegate method which will fire while clicking any link inside the web view

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
{
   [[UIApplication sharedApplication] openURL:[request URL]];
}

}

just differentiate the URL with the URLRequest .

If you have set the UIWebViewDelegate you can use the method

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

Using this method we can block or allow certain links/urls to be opened in either the UIWebView or in the mobile browser.

So we can do something like

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // We only want to block links
    if(navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSString *url = [[request URL] absoluteString];
        if([url isEqualToString:@"http://www.google.co.uk"]) {
            // If the requested url is a link and is http://www.google.co.uk block it from loading in the UIWebView and load it in the mobile browser.
            if([[UIApplication sharedApplication] canOpenUrl:[request URL]]) {
                // We should be checking to make sure we can open it first, for whatever reason we might not be able to and we don't want to give the user a bad journey.
                [[UIApplication sharedApplication] openURL:[request URL]];

                // We also need to tell our UIWebView not to do anything so we need to return NO.
                return NO;
            }
        }
    }

    // If all is OK and we are happy for any other link to be included then just return YES and continue.
    return YES;
}

If you want to block all links then just remove the isEqualToString: if statement or you can just block certain links by adding else if to the if statement . This code does one of two things it will either load the clicked link site in the UIWebView or it will load the site in the mobile browser if it passes the if statements .

UPDATE

It turns out the user isn't doing the objective-c side of things they are doing the html which wasn't in the original question, but since this could help users in the future I will leave my answer here.

EDIT

Since the user is using html to try and direct the user to the mobile browser what they are looking for is called URL Schemes. Unfortunately I don't believe there is any URL Scheme as of iOS 7 that will open safari mobile browser. This question has been asked before so I am not going to sit here and saying everything again I will just direct you to the question and answer Open Mobile Safari from a Link in a WebView

//codigo para abrir no safari
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {

NSURL *requestURL =[ [ request URL ] retain ];
if ( ( [ [ requestURL scheme ] isEqualToString: @”http” ] || [ [ requestURL scheme ] isEqualToString: @”https” ] || [ [ requestURL scheme ] isEqualToString: @”mailto” ])&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) )
{ return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; } [     requestURL release ]; return YES;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM