一旦包含在Phonegap中,iframe将在新窗口中打开

时间:2012-04-10 20:42:36

标签: jquery-mobile cordova

我有一个jquery移动应用程序,我正在包装在Phonegap for Iphone / Android商店。 我有一个页面使用iframe,没有Phonegap,就像你期望的那样工作。  但是,一旦被包装,Iframe实际上会导致应用程序打开一个新窗口/浏览器,并离开应用程序。 有谁知道这是否有解决方案? 谢谢!

2 个答案:

答案 0 :(得分:1)

http://denrobapps.com/2010/12/phonegap-and-iframes/

首先,打开PhoneGapDelegate.m并找到此代码块:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];

/*
 * Get Command and Options From URL
 * We are looking for URLS that match gap://<Class>.<command>/[<arguments>][?<dictionary>]
 * We have to strip off the leading slash for the options.
 */
 if ([[url scheme] isEqualToString:@"gap"]) {

    InvokedUrlCommand* iuc = [[InvokedUrlCommand newFromUrl:url] autorelease];

    // Tell the JS code that we've gotten this command, and we're ready for another
    [theWebView stringByEvaluatingJavaScriptFromString:@"PhoneGap.queue.ready = true;"];

    // Check to see if we are provided a class:method style command.
    [self execute:iuc];

     return NO;
}

/*
 * If a URL is being loaded that's a local file URL, just load it internally
 */
else if ([url isFileURL])
{
    //NSLog(@"File URL %@", [url description]);
    return YES;
}

/*
 * We don't have a PhoneGap or local file request, load it in the main Safari browser.
 */
else
{
    //NSLog(@"Unknown URL %@", [url description]);
    //[[UIApplication sharedApplication] openURL:url];
    return NO;
}

return YES;
}

将此else插入 - 如果正好在该块的第一个if语句下:

else if ([[url scheme] isEqualToString:@"http"])
{
    return YES;
}

还要确保在最后一个else语句中取消注释[[UIApplication sharedApplication] openURL:url](否则单击iFrame中的链接将无效):

else
{
    //NSLog(@"Unknown URL %@", [url description]);
    [[UIApplication sharedApplication] openURL:url];
    return NO;
}

答案 1 :(得分:0)

这是因为默认情况下,phonegap禁用外部内容,并引用内置浏览器。

您可以在文件/res/xml/cordova.xml

中修复此问题

右键单击 - &gt;用文本编辑器打开 然后寻找那些线:

<access origin="http://127.0.0.1*"/> <!-- allow local pages -->
<!-- <access origin="https://example.com" /> allow any secure requests to example.com -->
<!-- <access origin="https://example.com" subdomains="true" /> such as above, but including subdomains, such as www -->
<!-- <access origin=".*"/> Allow all domains, suggested development use only -->

我猜那里的文档不言而喻。我不知道允许多个访问源(我在同一台服务器上托管所有代码)并指向我的初始文件。所以我不再使用/ assets / www了。