iOS UiWebView“帧加载中断”

时间:2014-05-05 13:31:02

标签: ios objective-c uiwebview

我有一个指向外部网站的UiWebView,该网站的会话有效期为30分钟。在我的应用程序中,我在应用程序中嵌入了自定义登录页面,因为我无法使用远程站点中的一个。此登录页面为:

file://index.html

当用户将应用程序放入后台时,如果应用程序在后台停留超过20分钟,我想自动重新加载我的登录页面(我知道这不是理想的,但是因为业务需求)。

我这样做的代码非常简单:

static NSDate *lastActivity = nil;

- (void)applicationWillResignActive:(UIApplication *)application
{
    lastActivity = [NSDate date];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSinceDate:lastActivity];
    if(time > 60 * 20){
       UIWebView *view = self.viewController.webView;
       NSURL *url = [NSURL URLWithString:self.viewController.startPage];
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
       [view loadRequest:request];

    }
}

然而,当我这样做时,我收到错误:

Failed to load webpage with error: Frame load interrupted

我理解这可能是因为某些内容会在没有用户交互的情况下自动从一个URL方案转移到另一个URL方案。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

我认为您需要在UIWebView委托方法中处理file://协议。例如

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

在您的情况下,告诉委托方法如何处理您的网址方案。例如

if ([url.scheme isEqualToString:@"file"]) {
    NSLog(@"Open start page");
    [[UIApplication sharedApplication] openURL:url];
    return NO;
}

不确定这是否适合您,但希望它能提供解决方案的路径。