iOS UiWebView“帧加载中断”
我有一个UiWebView指向外部网站的会议过期30分钟不活动。 在我的应用程序中,我有一个embedded在应用程序中的自定义login页面,因为我无法使用远程站点中的一个。 此login页面是:
file://index.html
当用户将应用程序放入后台时,如果应用程序在后台停留超过20分钟,我想要自动重新加载login页面(我知道这并不理想,但由于业务需求而被迫这样做) 。
我的代码做到这一点很简单:
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模式转到另一个模式,而无需用户交互。 有没有办法做到这一点?
我想你将需要在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 ]; } }
在你的情况下,告诉委托方法如何处理你的urlscheme。 例如。
if ([url.scheme isEqualToString:@"file"]) { NSLog(@"Open start page"); [[UIApplication sharedApplication] openURL:url]; return NO; }
不知道这是否会为你工作,但希望它会提供一个解决scheme的path。