Cla*_*aus 26 load window.location external-links cordova
我正在使用PhoneGap编写iPad应用程序,我想加载外部URL而不触发Safari或使用像ChildBrowser这样的内部Web浏览器.
我正在使用PhoneGap iPad/iPhone示例项目,我尝试了不同的方法.在onBodyLoad()函数中,我添加了:
window.location.href('http://www.wordreference.com');
Run Code Online (Sandbox Code Playgroud)
但是这一行使用一个新的Safari窗口打开链接.从那时起,无法返回PhoneGap
之后,我尝试使用document.write替换页面内容的AJAX请求
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();
}
Run Code Online (Sandbox Code Playgroud)
现在,从onBodyLoad()内部调用:
loadHTML('http://www.wordreference.com',10000);
Run Code Online (Sandbox Code Playgroud)
打开PhoneGap容器中的链接,没问题.关键是我想加载用Python编写的动态页面
loadHTML('http://www.mysite.com/cgi-bin/index.py',10000)
Run Code Online (Sandbox Code Playgroud)
此时未调用Safari,但会显示PhoneGap容器中的黑色页面!我想指出,如果我在Safari中键入它,链接就完全正常工作(我无法报告隐私问题).
可能是与某种必要的许可有关的问题???
我找到了与PhoneGap for BlackBerry类似的东西,建议的解决方案是修改config.xml文件
<access subdomains="true" uri="http://www.mysite.com/" />
Run Code Online (Sandbox Code Playgroud)
我试图直接在我的index.html中添加此标记,但它不起作用.
iPhone有什么类似的方法吗?
非常感谢
Cla*_*aus 23
我想我找到了解决方案,
在PhoneGap Application Delegate .m文件 {YourProject} AppDelegate.m中,修改方法:
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
Run Code Online (Sandbox Code Playgroud)
同
- (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 ];
}
}
Run Code Online (Sandbox Code Playgroud)
这将打开PhoneGap容器中的所有外部链接!
PS.您可以在周围找到对此链接的引用,但我认为它对使用0.9.5版本编写的应用程序不起作用,因为Safari默认情况下会为外部链接打开.
nis*_*isc 10
对于在Android中遇到此问题的人:
我不知道早期版本,但在PhoneGap 1.1.0中,您可以创建一个名为res/xml/phonegap.xml的文件,并列出不应在外部浏览器中打开的域.
来自DroidGap.java:
/**
* Load PhoneGap configuration from res/xml/phonegap.xml.
* Approved list of URLs that can be loaded into DroidGap
* <access origin="http://server regexp" subdomains="true" />
* Log level: ERROR, WARN, INFO, DEBUG, VERBOSE (default=ERROR)
* <log level="DEBUG" />
*/
private void loadConfiguration() {
[...]
Run Code Online (Sandbox Code Playgroud)
示例phonegap.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phonegap>
<access origin="http://stackoverflow.com" subdomains="true" />
</phonegap>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33599 次 |
| 最近记录: |