iOS UIWebView 应用在 Safari 中打开链接

iOS UIWebView app opens link in Safari(iOS UIWebView 应用在 Safari 中打开链接)

本文介绍了iOS UIWebView 应用在 Safari 中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iOS 应用程序,它只有一个视图,即 UIWebView(它打开应用程序的主要内容).我希望当我在某处(例如在表格行上)单击应用程序以在 Safari 浏览器中打开特定链接,而不是在 UIWebView 内.

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.

这是否可行,无需编写任何 iOS 代码,而是让 JavaScript 在 Safari 本身中打开该链接?

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 代码

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

JS 代码:

 $(".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);

        });

推荐答案

在某些情况下,您可以告诉 javascript 中所有具有 target="_blank" 的链接,并将它们传递给 window.使用_system"参数打开.这适用于 iOS 和 Android.

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

或者在您的情况下,只需将 a.setAttribute("target", "_blank"); 替换为 a.setAttribute("target", "_system");

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

这在一个古老的项目中对我有用,但我不确定它是否仍然有效(在 iOS 和 Android 上)

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

这篇关于iOS UIWebView 应用在 Safari 中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS UIWebView 应用在 Safari 中打开链接

基础教程推荐