xcode OpenAllWhitelistURLsInWebView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10838598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenAllWhitelistURLsInWebView
提问by nate8684
In order to allow for Vimeo video embeds I have "OpenAllWhitelistURLsInWebView" = "yes". But since I did that it only opens whitelisted items and opens them up in webview obviously. I need all non whitelisted items to open up in the safari browser, not webview. Any thoughts on how to accomplish this?
为了允许 Vimeo 视频嵌入,我有“OpenAllWhitelistURLsInWebView”=“yes”。但既然我这样做了,它只会打开白名单项目,并且显然在 webview 中打开它们。我需要在 safari 浏览器中打开所有未列入白名单的项目,而不是 webview。关于如何实现这一点的任何想法?
Cordova 1.7 | XCode 4.3.2 | Jquery 1.7.1 | JqueryMobile 1.1.0 | ios 5.1
科尔多瓦 1.7 | XCode 4.3.2 | jQuery 1.7.1 | JqueryMobile 1.1.0 | IOS 5.1
回答by Titouan de Bailleul
I don't know exactly the difference with Cordovoa but I'm working with PG 1.4.1 and I have this settings in my PhoneGap.plist
我不知道与 Cordovoa 的确切区别,但我正在使用 PG 1.4.1,并且我的PhoneGap.plist 中有此设置
And this in my AppDelegate.m
这在我的AppDelegate.m 中
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if([[url absoluteString] rangeOfString:@"vimeo.com"].length > 0 || [[url scheme] isEqualToString:@"file"]){
return [self.viewController webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
[[UIApplication sharedApplication] openURL:url];
return NO;
}
This is my pretty simple index.htmlopened by PG
这是我用 PG 打开的非常简单的index.html
<body>
<a href="http://www.vimeo.com">Vimeo</a>
<a href="http://www.google.com">Google</a>
</body>
The vimeo link is opened within the webview and the google link is opened in Safari.
vimeo 链接在 webview 中打开,google 链接在 Safari 中打开。
UPDATECordova 1.7
更新科尔多瓦 1.7
Apparently, the shouldSTartLoadWithRequest function is not called in latests versions of PhoneGap/Cordova (from 1.6.1 I think). So, now if you want to open a link within Safari, you need to set the target
attribute a the a tag to _blank
. Since you don't always have access to the code, here's a script to help.
显然,在最新版本的 PhoneGap/Cordova(我认为是 1.6.1)中没有调用 shouldStartLoadWithRequest 函数。所以,现在如果你想在 Safari 中打开一个链接,你需要target
将 a 标签的属性设置为_blank
。由于您并不总是可以访问代码,因此这里有一个脚本可以提供帮助。
<head>
<script type="text/javascript" src="cordova-1.7.0.js"></script>
<script>
document.onclick = checkLink;
function checkLink(e) {
var url = e.target.href;
if(url.indexOf('vimeo.com') == -1){
window.open(url,'_blank');
}
}
</script>
</head>
<body>
<a href="http://www.vimeo.com">Vimeo</a>
<a href="http://www.google.com" target="_blank">Google</a>
</body>
回答by Kristian
So I'm using Cordova 1.9. After debugging a little, I see that the function shouldStartLoadWithRequest should now be implemented in the MainViewController.m file not in the AppDelegate.m file. This is why it is never triggered.
所以我使用的是 Cordova 1.9。稍微调试后,我看到函数 shouldStartLoadWithRequest 现在应该在 MainViewController.m 文件中实现,而不是在 AppDelegate.m 文件中。这就是它永远不会被触发的原因。
Discovered this after updating from PhoneGap 1.4.1 to Cordova 1.9.
从 PhoneGap 1.4.1 更新到 Cordova 1.9 后发现了这一点。
回答by codemonkey
It should already do that but since you've posted here I guess it doesn't.
它应该已经这样做了,但既然你已经在这里发布了,我想它没有。
What you can do is override the shouldStartLoadWithRequest
method in your AppDelegate.m file. You can add a condition you would like to check for (such as the URL containing vimeo) and return true, else return false.
您可以做的是覆盖shouldStartLoadWithRequest
AppDelegate.m 文件中的方法。您可以添加要检查的条件(例如包含 vimeo 的 URL)并返回 true,否则返回 false。
if ( [request.URL.absoluteString rangeOfString:@"vimeo.com"].location != NSNotFound) {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
//open in Safari
[[UIApplication sharedApplication]openURL:request.URL];
return NO;