iphone - 如何刷新 iPhone 的 UIWebview?

标签 iphone ios uiwebview

我有一个带有 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/

相关文章:

iphone - 仪器可能会在自动释放的对象上显示泄漏?

ios - 访问整个数组的数组对象字典

ios - UIwebView 加载动画图像不响应 UITapGestureRecognizer

ios - iOS 上的 GMail 应用程序使用什么来编辑 HTML 电子邮件?

javascript - HTML、CSS 应用程序原型(prototype) - canvas、iframe 或其他

iphone - 如何让UISlider以指数方式输出漂亮的四舍五入数字?

iphone - Objective-C:用大量文本拆分类的方法

ios - UIBarButtonItem tintColor 停止工作

ios - 如何更改此 UIBezierPath 的箭头方向

ios - 如何在 iOS WebView 中启用 peek 和 pop?