iPhone的PhoneGap:加载外部URL的问题 [英] PhoneGap for iPhone: problem loading external URL

查看:164
本文介绍了iPhone的PhoneGap:加载外部URL的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhoneGap编写iPad应用程序,并且我想加载外部URL而不触发Safari或使用内部Web浏览器,如ChildBrowser。



m使用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;
//只是调试打印
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容器中的链接,很好。重点是我要加载以Python编写的动态网页

  loadHTML('http: //www.mysite.com/cgi-bin/index.py'10000)

在此点Safari不被调用,但在PhoneGap容器中的黑页显示!
我想指出的是,如果我在Safari中输入它,我的链接是完美的工作(我不能报告它的隐私问题)。



可能是一个问题与某种所需的权限相关



我发现了一个类似于相对于PhoneGap的黑莓,建议的解决方案是修改一个config.xml文件与



 < access subdomains =trueuri =http://www.mysite.com//> 

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

有什么类似的iPhone吗?



非常感谢



在PhoneGap应用程序委托.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容器!!!



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


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.

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'); 

but this line opens the link using a new Safari window.From that point is not possible to come back in PhoneGap

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();
}

Now, calling from inside onBodyLoad():

loadHTML('http://www.wordreference.com',10000); 

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)

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???

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/" />

I tried to add this tag directly in my index.html but it doesn't work.

Is there any similar approach for iPhone??

Thanks a lot

解决方案

I think I've found the solution,

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];
}

with

- (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 ];
}
}

This will open all the external links within the PhoneGap container!!!

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 is open for external links by default.

这篇关于iPhone的PhoneGap:加载外部URL的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆