我有一个 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 方案,而无需用户交互。有没有办法做到这一点?
我认为您需要在 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 ];
}
}
对于您的情况,请告诉委托方法如何处理您的 url 方案。例如。
if ([url.scheme isEqualToString:@"file"]) {
NSLog(@"Open start page");
[[UIApplication sharedApplication] openURL:url];
return NO;
}
不确定这是否适合您,但希望它能提供解决方案的途径。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)