Open target=quot;_blankquot; links outside of UIWebView in Safari(打开目标=“_blank;Safari 中 UIWebView 之外的链接)
问题描述
在我的 iOS 应用程序中,我有一个 UIWebView.
Inside my iOS Application I have an UIWebView.
现在我希望所有具有属性 target="_blank" 的链接不要在我的 WebView 内部打开,而是在 Safari 外部打开.
Now I want all links that have the attribute target="_blank" not to open inside my WebView but externally in Safari.
我该怎么做?
推荐答案
我的答案来自我在 Android WebView 的堆栈溢出中找到的答案.但实际上,两个 webview 都有相同的问题和相同的(脏)修复:
My answer, which is a from an answer I found on stack overflow for the Android WebView. But actually, both webview have the same problem and same (dirty) fix:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString hasPrefix:@"newtab:"])
{
// JS-hacked URl is a target=_blank url - manually open the browser.
NSURL *url = [NSURL URLWithString:[request.URL.absoluteString substringFromIndex:7]];
[[UIApplication sharedApplication] openURL:url];
return true;
}
return true;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// JS Injection hack to solve the target="_blank" issue and open a real browser in such case.
NSString *JSInjection = @"javascript: var allLinks = document.getElementsByTagName('a'); if (allLinks) {var i;for (i=0; i<allLinks.length; i++) {var link = allLinks[i];var target = link.getAttribute('target'); if (target && target == '_blank') {link.setAttribute('target','_self');link.href = 'newtab:'+link.href;}}}";
[webView stringByEvaluatingJavaScriptFromString:JSInjection];
}
这解决了在 safari 中打开 target="_blank" 的问题,并在 webview 中保持打开标准链接.
This solves both the target="_blank" issue to open in safari, AND keeps opening standard links within the webview.
这篇关于打开目标=“_blank";Safari 中 UIWebView 之外的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:打开目标=“_blank";Safari 中 UIWebView 之外的链


基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01