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 中打开链接
				
        
 
            
        基础教程推荐
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				