Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear cache for my UIWebView's loadHtmlString?

I have some embedded html in my application bundle.I'm using loadHtmlString to load it. At the same time there are some image files in my documents folder.I want to replace my img in html with the one in my documents folder. For example, here is the sample html I have:

       <div id="image">
            <a href="image">
                <img src="@image" class="center">               
            </a>
        </div>

I'm using the following code to replace the @image to the file path in my documents:

    NSString* imgFile = [NSString stringWithFormat:@"file:///%@/%@",
                         [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images"],@"myimage.png"];
    text = [text stringByReplacingOccurrencesOfString:@"@image" withString:imgFile];   

    NSURL* baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
    [self.webView loadHTMLString:text baseURL:baseUrl];    

(text is the html text of my html file).

It works the first time when this code is called. But for example, if I modify myimage.png and call the code again, the image before I modified is still showed. (I have check my file in my document folder, it's already modified correctly).

I suspect it's caused by the cache. Can anybody advise?

Thanks

Note:

It is caused by the cache. I solved the problem by using the following method to force UIWebView to load a fresh copy of image:

NSString* imgFile = [NSString stringWithFormat:@"file:///%@/%@?t=%d",
                         [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images"],@"myimage.png",time(NULL)];
    text = [text stringByReplacingOccurrencesOfString:@"@image" withString:imgFile];
like image 233
Bagusflyer Avatar asked Jul 17 '12 05:07

Bagusflyer


1 Answers

I solved the problem by add a time in my url. So the image will not be cached. Please refer to my questions. The question is consider to be closed.

like image 183
Bagusflyer Avatar answered Sep 29 '22 19:09

Bagusflyer