2011-07-04 9 views
0

저는 아이폰 개발에 익숙하며 웹 뷰를 디바이스에 캐시하려고하므로 애플리케이션이 닫히고 애플리케이션이 다시 시작될 때 데이터를 다시로드 할 때도 거기에서 찌르 리게됩니다. 또한 2 주마다 캐시에 데이터를 다시로드하는 것이 좋습니다.UIWebView 캐싱하기

많은 감사, 주변 검색 및 친구 요청을 많이 통해 토마스

+0

당신은 아마보기를 캐시하고 싶지는 않지만, 데이터 (모델) – Felix

+0

잘 페이지의 하나가 웹보기이고 캐시 정보를로드 할 필요가 있기 때문에 캐시를해야합니다. 오프라인에서 사용할 수 있고로드 할 때마다 데이터를 사용하지 않고 앱 스토어에 업데이트를 푸시하지 않고도 데이터를 업데이트 할 수 있습니다. –

+0

마지막으로 댓글을 작성해 주셔서 진심으로 사과드립니다. 그것은 내가 웹 페이지 페이지가 아닌 캐시에 필요한 알아요. –

답변

2

내가 코드를 찾기 위해 관리하고 있지만 나는 모두

목표 - C와 공유 할 것

- (void) cacheFile 
{ 
    //Create the file/directory pointer for the storage of the cache. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    self.dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"cache.html"]; 

    //Check to see if a file exists a the location 
    if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { 
     //Code for customising when the cache reloads would go here. 
    } 
    else 
    { 
     //If no file exists write the html cache to it 
     //Download and write to file 
     NSURL *cacheUrl = [NSURL URLWithString:@"INSERT WEB URL HERE"]; 
     NSData *cacheUrlData = [NSData dataWithContentsOfURL:cacheUrl]; 
     [cacheUrlData writeToFile:dataPath atomically:YES]; 
    } 
//Run the load web view function. 
[self loadWebView]; 
} 


- (void) loadWebView 
{ 
//Load up the web view from the cache. 
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:dataPath]]]; 

} 

스위프트 3

func cacheFile() { 
    //Create the file/directory pointer for the storage of the cache. 
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
    guard let dataPath = paths.first?.appending("cache.html") else { 
     return 
    } 

    //Check to see if a file exists a the location 
    if FileManager.default.fileExists(atPath: dataPath) { 
     //Code for customising when the cache reloads would go here. 
    } else if let cacheUrl = URL(string: "INSERT WEB URL HERE") { 
     //If no file exists write the html cache to it 
     //Download and write to file 
     do { 
      let cacheUrlData = try Data(contentsOf: cacheUrl) 
      try cacheUrlData.write(to: URL(fileURLWithPath: dataPath), options: Data.WritingOptions.atomic) 
     } catch { 
      print("Problem with cacheUrlData") 
     } 
    } 

    //Run the load web view function. 
    loadWebView(dataPath: dataPath) 
} 

func loadWebView(dataPath: String) { 
    webView.loadRequest(URLRequest(url: URL(fileURLWithPath: dataPath))) 
}