iPhone 版 PhoneGap:加载外部 URL 时出现问题 [英] PhoneGap for iPhone: problem loading external URL
问题描述
我正在使用 PhoneGap 为 iPad 编写应用程序,我想在不触发 Safari 或使用像 ChildBrowser 这样的内部网络浏览器的情况下加载外部 URL.
I'm writing an application for iPad using PhoneGap and I would like to load an external URL without triggering Safari or using internal web browser like ChildBrowser.
我正在使用 PhoneGap iPad/iPhone 示例项目,并尝试了不同的方法.在 onBodyLoad() 函数中我添加了:
I'm using the PhoneGap iPad/iPhone sample project and I tried different approaches. In the onBodyLoad() function I added:
window.location.href('http://www.wordreference.com');
但此行使用新的 Safari 窗口打开链接.从那时起就无法在 PhoneGap 中返回
but this line opens the link using a new Safari window.From that point is not possible to come back in PhoneGap
之后,我尝试使用 AJAX 请求替换页面内容,使用 document.write
Afterwards, I tried with an AJAX request substituting the content of the page using 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() 内部调用:
Now, calling from inside onBodyLoad():
loadHTML('http://www.wordreference.com',10000);
在PhoneGap Container中打开链接,没问题.重点是我想加载一个用Python编写的动态页面
Opens the link in the PhoneGap Container,which is fine. The point is that I want to load a dynamic page written in Python
loadHTML('http://www.mysite.com/cgi-bin/index.py',10000)
此时并没有调用Safari,而是显示PhoneGap容器中的黑页!!我想指出,如果我在 Safari 中输入该链接,则它可以正常工作(由于隐私问题,我无法报告它).
At this point Safari is not called but a black page in the PhoneGap container is displayed!! I want to point out that the link is perfectly working if I type it in Safari( I cannot report it for privacy issues).
这可能是与某种所需权限有关的问题吗???
Could be it a problem related to some kind of needed permission???
我发现一些与用于 BlackBerry 的 PhoneGap 类似的东西,建议的解决方案是使用
I found something similar relative to PhoneGap for BlackBerry and the proposed solution was to modify a config.xml file with
<access subdomains="true" uri="http://www.mysite.com/" />
我试图直接在我的 index.html 中添加这个标签,但它不起作用.
I tried to add this tag directly in my index.html but it doesn't work.
iPhone 有没有类似的方法??
Is there any similar approach for iPhone??
非常感谢
推荐答案
我想我已经找到了解决方案,
I think I've found the solution,
在PhoneGap Application Delegate .m文件{YourProject}AppDelegate.m中,修改方法:
in the PhoneGap Application Delegate .m file {YourProject}AppDelegate.m, modify the method:
- (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 容器内的所有外部链接!!!
ps.您会在周围找到对这个 link 的引用,但我认为它不适用于使用 0.9 编写的应用程序.5 版本,因为默认情况下 Safari 会打开外部链接.
ps. Around you will find references to this link but I think it doesn't work for app written using the 0.9.5 version,since Safari gets opened for external links by default.
这篇关于iPhone 版 PhoneGap:加载外部 URL 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!