如何为iPhone刷新UIWebview?

时间:2013-04-06 15:50:28

标签: iphone ios uiwebview

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

1 个答案:

答案 0 :(得分: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.
   }
 }