UIWebview without Cache

前端 未结 4 830
旧巷少年郎
旧巷少年郎 2020-12-05 05:04

I want to load a webview without cache?

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj =         


        
相关标签:
4条回答
  • 2020-12-05 05:45

    Objective-C:

    // Remove and disable all URL Cache, but doesn't seem to affect the memory
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    

    You can find more information in http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/

    Swift:

    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0
    
    0 讨论(0)
  • 2020-12-05 05:46

    You can change your cache policy, using the code:

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url 
                                            cachePolicy:NSURLRequestReloadIgnoringCacheData
                                        timeoutInterval: 10.0]; 
    
    0 讨论(0)
  • 2020-12-05 05:47

    I try to answer a similar question here. I hope it was useful for you! The last solution works for HTML but not for images, css and js linked in html.

    How to clear UIWebView cache?

    0 讨论(0)
  • 2020-12-05 06:00

    I've tried all of these solutions but none of them work every single time. My main test was an html page with an image set as a background in the html page. This html page and image are downloaded locally on my iPad through a "sync" method i have written. The only way to keep the cache fully reset is the constantly change the url of the image in the html page so it thinks it's a unique image each time. For example in my html:

    <body background="image.png?v123">
    

    So each page change i make i have to update the version number:

    <body background="image.png?v1234">
    

    etc, etc. You can change the url in anyway you want, it just has to be different.

    Well i'm tired of doing this. Now When i download content locally, I am instead dynamically changing the containing folder's name. Since this folder is a parent folder and all of my links are relative it means i don't have to edit my html files each time. So far it seems to be working ok. I wish these other methods were consistent as it would be so much easier to simply reset the cache.

    So in my requests its the baseURL which is causing my cache to reset, not the query string to my url or background image, or css file, etc, etc.

    NSString *myURL = @"index.html";
    NSURL *baseURL = [NSURL fileURLWithPath:@"FULL_PATH_TO_DOCS/MYFOLDERV123"];
    [myTargetWebView loadHTMLString:myURL baseURL:baseURL];
    
    0 讨论(0)
提交回复
热议问题