【问题标题】:How do I refresh UIWebview for iPhone?如何刷新 iPhone 的 UIWebview?
【发布时间】:2013-04-06 15:50:28
【问题描述】:

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

【问题讨论】:

    标签: iphone ios uiwebview


    【解决方案1】:

    用于重新加载 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.
       }
     }
    

    【讨论】:

    • 嘿,可能有一个小的逻辑错误,但这是你的做法,请回复我。
    • @sharataka 有用吗?接受,如果你觉得很酷,请投票给我。
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2014-11-14
    • 2012-11-13
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多