簡體   English   中英

如何為iPhone刷新UIWebview?

[英]How do I refresh UIWebview for iPhone?

我有一個擁有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.
   }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM