iOS UiWebView “Frame load interrupted”

旧城冷巷雨未停 提交于 2019-12-01 12:11:19

问题


I have a UiWebView that is pointing to an external site that has a session expiration of 30 minutes of inactivity. In my app, I have a custom login page embedded in the application because I can't use one from the remote site. This login page is:

file://index.html

When the user puts the app into the background, I want to automatically reload my login page if the application stays in the background for more than 20 minutes (I know this isn't ideal, but forced to do this because of the business requirements).

My code to do this is fairly simple:

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

    }
}

However when I do this I receive the error:

Failed to load webpage with error: Frame load interrupted

I understand this might be because something is automatically going from one URL scheme to another without user interaction. Is there a way to do this?


回答1:


I think you will need to handle the file:// protocol in a UIWebView delegate method. eg.

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

In your case, tell the delegate method what to do with your url scheme. eg.

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

Not sure if this will work for you, but hopefully it will provide a path to the solution.



来源:https://stackoverflow.com/questions/23473780/ios-uiwebview-frame-load-interrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!