我有一个带有 UIWebView 的 iPhone 应用程序。 X 小时后,用户再次启动应用程序,我发现 webview 显示相同的数据,即使页面可能有新内容。当用户在 X 小时后第二次启动应用程序时,如何自动让 UIWebView 刷新页面?
最佳答案
重新加载 webView 使用
[webView reload];
计算时差:
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
要计算 X 小时,请执行以下操作:
添加这个是viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredForeground) name:UIApplicationDidEnterForegroundNotification object:nil];
编写通知处理函数
-(void)applicationEnteredBackground
{
//storing timestamp when user goes background
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSDate date] forKey:@"timeStamp"];
[userDefaults synchronize];
}
-(void)applicationEnteredForeground
{
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
NSString *lastTimeOpened= [userDefaults objectForKey:@"timeStamp"];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
//set formatter style as long
if([[NSDate date] timeIntervalSinceDate:[formatter dateFromString:lastTimeOpened]] > X hours)
{
//reload your webview here.
}
}
关于iphone - 如何刷新 iPhone 的 UIWebview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852855/