2016-06-01 42 views
1

如何在UIWebView中啓用緩存?如何在UIWebView中啓用緩存?

這很令人沮喪。我正在尋找幾個小時,但在其他許多帖子中沒有提到的解決方案對我沒有幫助。

我嘗試了許多解決方案,如https://stackoverflow.com/a/26174438https://stackoverflow.com/a/1424301http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/CachePolicies.htmliOS Cache a UIWebView

我的代碼看起來像

[self setUrl:[[NSString alloc] initWithFormat:@"http://example.com"]]; 
    [[NSURLCache sharedURLCache] setDiskCapacity:0]; 
    [[NSURLCache sharedURLCache] setMemoryCapacity:0]; 
    [self setLoadUrl: [NSURL URLWithString:[self url]]]; 
    [self setRequestObj:[NSURLRequest requestWithURL:[self loadUrl]]]; 

如果caching被禁用。如果它開啓我

if([[self cache] isEqualToNumber:[[NSNumber alloc] initWithInt:1]]) 
{ 
    [[NSURLCache sharedURLCache] setDiskCapacity:4 * 1024 * 1024]; 
    [[NSURLCache sharedURLCache] setMemoryCapacity:32 * 1024 * 1024]; 
    [self setRequestObj:[NSURLRequest requestWithURL:loadUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]]; 
} 

+0

你可以試試這個網址https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI = w300並告訴我結果。 –

+0

試試這個答案並實現NSMutableURLRequest http://stackoverflow.com/a/26174438/3116236 – iSashok

+0

@iSashok我也試過這個答案,它也鏈接在我的文章。不幫助我......我錯了什麼? – Premox

回答

2

@Premox我怎麼覺得你可以選擇不同的方式。您的UIWebView加載頁面後,你可以得到迴應的HTML轉換爲NSString類,並將其保存到緩存或序列化爲你想

+0

Thx,help我。現在它正常工作=) – Premox