Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UIWebView app opens link in Safari

I have an iOS app which has only one view and that is UIWebView (It opens the main content of the app). I would like when I make a click somewhere(e.g on a table row) the app to opens specific link in Safari browser not inside the UIWebView.

Is that doable without writing any iOS code and instead of that make the JavaScript opens that link in Safari itself ?

I have thatcode,but it doesn't help at all:

HTML Code

<tr class='link-to-pdf' data-href='www.example.com'>

JS Code:

 $(".link-to-pdf").click(function () {

            var a = document.createElement('a');
            a.setAttribute("href", this.getAttribute("data-href"));
            a.setAttribute("target", "_blank");

            var dispatch = document.createEvent("HTMLEvents");
            dispatch.initEvent("click", true, true);
            a.dispatchEvent(dispatch);

        });
like image 395
radioaktiv Avatar asked Jul 17 '15 13:07

radioaktiv


People also ask

Is UIWebView deprecated?

Since then, we've recommended that you adopt WKWebView instead of UIWebView and WebView — both of which were formally deprecated.

What is UIWebView Safari?

What is UIWebView? UIWebView is a deprecated iOS user interface control in Apple's UIKit framework. It loads HTML files and web content into an app view, rendering them as they would appear in a browser window. See developer.apple.com/documentation/uikit/uiwebview.

Does WKWebView use Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

When was UIWebView deprecated?

As of iOS 12.0, 'UIWebView' has been explicitly marked as deprecated. Apple is encouraging developers to adopt WKWebView instead, which was introduced in iOS 8.0. Since the iOS VCO SDK targets iOS 10.0 anyways, this shouldn't require deprecating support for any existing versions.


1 Answers

In some cases, you can tell all links in your javascript that have target="_blank", and pass them to window.open with the '_system' param. This will work on both iOS and Android.

$(document).on('click', 'a[target="_blank"]', function(ev) {
  var url;

  ev.preventDefault();
  url = $(this).attr('href');
  window.open(url, '_system');
});

Or in your case, simply replace a.setAttribute("target", "_blank"); with a.setAttribute("target", "_system");

This worked for me in an ancient projekt, but im unsure if it still works (on iOS and Android)

like image 91
Nils Munch Avatar answered Oct 17 '22 00:10

Nils Munch