应用程序进入前台后UIWebview白屏
UIWebview white screen after application enters foreground
我的应用程序只是一个加载给定 url 的简单 Web 视图。我的问题是,如果我在一段时间后打开我的应用程序,webview 将不再显示其内容。我的问题是如何知道 webview 是否渲染了某些东西,或者它是否显示白屏并且我必须重新加载它。
我搜索过 SO 但没有成功,所以如果我能找到解决我问题的网站的任何建议或指导,我将不胜感激。
提前致谢!
以防万一有人需要这个我正在加载这样的内容并且一切正常,除非我有一段时间不使用它并且它位于后台,进入前景后它是白色的。我猜 iOS 正在以这种方式处理内存,并清除 webview。
- (void)loadWebView
{
NSDate *date = [NSDate date];
long unixTimeStamp = (long)[date timeIntervalSince1970];
NSString *urlString = [NSString stringWithFormat:@"http://www.myurlExample.com/mobile/?timestamp=%lu&secret=%@",unixTimeStamp,[[NSUserDefaults standardUserDefaults] objectForKey:kUniqueUserSecretID]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
[request setHTTPShouldHandleCookies:YES];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
[request setAllHTTPHeaderFields:headers];
[self.webView loadRequest:request];
}
听起来问题是在您将应用置于前台时出现的?
您可以在 AppDelegate.m:
中执行类似的操作
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Depending on the architecture of your app, grab the handler to your controller that contains the webView
[webViewController.webview reload];
}
一种方法可能是使用 -stringByEvaluatingJavaScriptFromString:
检查网络视图的内容。例如,您可以这样做:
NSString *head =
[myWebView stringByEvaluatingJavaScriptFromString:
@"document.getElementsByTagName(\"head\")"];
我的应用程序只是一个加载给定 url 的简单 Web 视图。我的问题是,如果我在一段时间后打开我的应用程序,webview 将不再显示其内容。我的问题是如何知道 webview 是否渲染了某些东西,或者它是否显示白屏并且我必须重新加载它。
我搜索过 SO 但没有成功,所以如果我能找到解决我问题的网站的任何建议或指导,我将不胜感激。
提前致谢!
以防万一有人需要这个我正在加载这样的内容并且一切正常,除非我有一段时间不使用它并且它位于后台,进入前景后它是白色的。我猜 iOS 正在以这种方式处理内存,并清除 webview。
- (void)loadWebView
{
NSDate *date = [NSDate date];
long unixTimeStamp = (long)[date timeIntervalSince1970];
NSString *urlString = [NSString stringWithFormat:@"http://www.myurlExample.com/mobile/?timestamp=%lu&secret=%@",unixTimeStamp,[[NSUserDefaults standardUserDefaults] objectForKey:kUniqueUserSecretID]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
[request setHTTPShouldHandleCookies:YES];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
[request setAllHTTPHeaderFields:headers];
[self.webView loadRequest:request];
}
听起来问题是在您将应用置于前台时出现的? 您可以在 AppDelegate.m:
中执行类似的操作- (void)applicationWillEnterForeground:(UIApplication *)application {
// Depending on the architecture of your app, grab the handler to your controller that contains the webView
[webViewController.webview reload];
}
一种方法可能是使用 -stringByEvaluatingJavaScriptFromString:
检查网络视图的内容。例如,您可以这样做:
NSString *head =
[myWebView stringByEvaluatingJavaScriptFromString:
@"document.getElementsByTagName(\"head\")"];