load - iPhone的PhoneGap : problem loading external URL

标签 load cordova window.location external-links

我正在使用 PhoneGap 为 iPad 编写应用程序,我想在不触发 Safari 或使用 ChildBrowser 等内部 Web 浏览器的情况下加载外部 URL。

我正在使用 PhoneGap iPad/iPhone 示例项目,并尝试了不同的方法。在我添加的 onBodyLoad() 函数中:

window.location.href('http://www.wordreference.com'); 

但这行使用新的 Safari 窗口打开链接。从那时起,无法返回 PhoneGap

之后,我尝试使用 AJAX 请求替换页面内容,使用 document.write
function loadHTML(url, timeout) {
if (timeout == undefined)
    timeout = 10000;
var req = new XMLHttpRequest();
var timer = setTimeout(function() {
    try {
        req.abort();
    } catch(e) {}
    navigator.notification.loadingStop();
},timeout);
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if (req.status < 300) {
            clearTimeout(timer);

            var html = req.responseText;
            //just a debug print   
    alert(html);
    document.write(html);

        }
        navigator.notification.loadingStop();
        delete req;
    }       
};          
req.open('GET', url, true);
req.send();
}

现在,从 onBodyLoad() 内部调用:
loadHTML('http://www.wordreference.com',10000); 

在 PhoneGap Container 中打开链接,这很好。关键是我要加载 动态页面用 Python 编写
loadHTML('http://www.mysite.com/cgi-bin/index.py',10000)

此时没有调用 Safari,但在 PhoneGap 容器中显示了一个黑页!!
我想指出,如果我在 Safari 中键入该链接,它就可以正常工作(我不能报告它的隐私问题)。

可能是与某种所需许可有关的问题吗???

我发现了与 BlackBerry 的 PhoneGap 类似的东西,建议的解决方案是修改 config.xml 文件
<access subdomains="true" uri="http://www.mysite.com/" />

我试图直接在我的 index.html 中添加这个标签,但它不起作用。

iPhone有类似的方法吗?

非常感谢

最佳答案

我想我找到了解决方案

在 PhoneGap 应用程序委托(delegate) .m 文件中 {YourProject}AppDelegate.m ,修改方法:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}


- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
 NSURL *url = [request URL];
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
    return YES;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}

这将打开 PhoneGap 容器中的所有外部链接!!!

附言。在你周围你会发现对这个 link 的引用但我认为它不适用于使用 0.9.5 版本编写的应用程序,因为默认情况下 Safari 会为外部链接打开。

关于load - iPhone的PhoneGap : problem loading external URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5911255/

相关文章:

android - :active not working on phonegap

mysql - phonegap/msql(i) 无法建立与数据库的连接

JQuery.address如何从url中删除#(javascript window.location删除#)

jquery - Window.location.hash 需要语法帮助

performance - 如何开始性能测试

javascript - 通过 javascript 在同一窗口中加载页面

javascript - 如何在页面加载时执行绑定(bind)到元素的 javascript/jquery 函数?

javascript - 可以在 iPhone 中向下拖动页面 - Framework7 Cordova

当所有图像加载时jquery div

javascript - 使用 window.location.replace 但页面仍在浏览器内存中