Mat*_*ine 29 javascript ios cordova
我有一个Phonegap(cordova)应用程序,我想在phonegap WebView中加载一些外部网页,我还有其他外部网页,我想在用户激活它时在Safari中加载.
通常,大多数人都有他们想要在WebView中打开外部链接的问题.将OpenAllWhitelistURLsInWebView设置为YES(在Cordova.plist/Phongap.plist中)可以解决该问题.
但我不想打开WebView的所有链接,只是一些.
我希望我可以打电话window.open('http://someexternalsite')在Safari window.parent.location.href = 'http://mysite'中打开并在WebView中打开它.
知道怎么做吗?
Tit*_*eul 19
如果要在safari中打开的链接都包含公共字符串,则可以使用下一段代码.
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:@"SCHEME"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
Run Code Online (Sandbox Code Playgroud)
放在该代码中的代码AppDelegate.m将打开在Safari中使用指定SCHEME的所有URL.
我担心这就是我想到的一切.
希望这可以帮助
更新:
代码应该放在MainViewControler中,至少对于cordova 2.2.0.
该方法最初被评论.我不得不用它来重定向Google地图链接:
NSRange isGoogleMaps = [[url absoluteString] rangeOfString:@"maps.google.com" options:NSCaseInsensitiveSearch];
NSRange isGoogleTerms = [[url absoluteString] rangeOfString:@"terms_maps.html" options:NSCaseInsensitiveSearch];
if(isGoogleMaps.location != NSNotFound || isGoogleTerms.location != NSNotFound ) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
Run Code Online (Sandbox Code Playgroud)
Rya*_*yan 11
只需捕获您的javascript中的所有链接target="_blank",并使用'_system'参数将它们传递给window.open.这适用于iOS和Android.
$(document).on('click', 'a[target="_blank"]', function(ev) {
var url;
ev.preventDefault();
url = $(this).attr('href');
window.open(url, '_system');
});
Run Code Online (Sandbox Code Playgroud)
根据@TDeBailleul的回答,这对我有用.基本上,任何具有PDF后缀的链接,或者如果它是我想在Safari中打开的特定页面,或者如果它不是www.example.com/*(外部链接)中的页面,它将在新窗户:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Open PDF files, Specific Pages, and External Links (not beginning with http://www.example.com) in Safari.app
if (
(navigationType == UIWebViewNavigationTypeLinkClicked) &&
([[[request URL] absoluteString] hasSuffix:@"pdf"] || [[[request URL] absoluteString] hasPrefix:@"http://www.example.com/specific-page.php"] || ![[[request URL] absoluteString] hasPrefix:@"http://www.example.com"])
) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于他人!
你可以(从phonegap 1.5.0开始)使用:
<a href="some://external/url" target="_blank">Click Me</a>
Run Code Online (Sandbox Code Playgroud)
这应该会导致phonegap启动本机浏览器.
我认为user868766指的是上述工作需要外部网址在白名单上.我一直在努力的应用程序在本地浏览器中打开了新闻故事的来源,因此我们在白名单中使用*以确保我们不排除任何来源.
希望有所帮助.
正确的方法是使用inAppBrowser插件
使用cordova CLI安装它:
cordova plugin add org.apache.cordova.inappbrowser
Run Code Online (Sandbox Code Playgroud)
然后,打开safari上的链接只需使用:
window.open('http://apache.org', '_system');
Run Code Online (Sandbox Code Playgroud)
在npm上有一个更新版本的插件
从cordova CLI安装它:
cordova plugin add cordova-plugin-inappbrowser
Run Code Online (Sandbox Code Playgroud)
要在safari上打开网站,您可以使用
cordova.InAppBrowser.open('http://apache.org', '_system');
Run Code Online (Sandbox Code Playgroud)
或者,如果您想继续使用window.open,就像旧版本一样,您可以在设备就绪事件上执行此操作:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.open = cordova.InAppBrowser.open;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41703 次 |
| 最近记录: |