iOS UIWebView app opens link in 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 中打开链接


基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01