0

我有一个具有 UIWebView 的 iPhone 应用程序。X 小时后,用户再次启动应用程序,我发现 webview 显示相同的数据,即使页面可能有新内容。当用户在 X 小时后第二次启动应用程序时,如何自动让 UIWebView 刷新页面?

4

1 回答 1

2

用于重新加载 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.
   }
 }
于 2013-04-06T16:06:42.467 回答