【问题标题】:Delete Cache with UIWebview in an iOS application在 iOS 应用程序中使用 UIWebview 删除缓存
【发布时间】:2013-06-22 12:36:38
【问题描述】:

我遇到了 UIWebview 的问题。我在我的 web 视图中调用了几次打印图片的 url,我计算了我的图片显示了多少次。

当我调用图片的url时,计数与打印数量匹配。

NSURL *URL = [NSURL URLWithString:@"http://url_pictures.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[adView loadRequest:request];

当我用 loadhtmlstring 调用 url 时,我的计数器为 1。使用 loadhtmlstring 对我来说非常重要,因为我的 API 会返回一个带有 javascript 的 html 代码。

NSString *html = @"<html><head></head><body>document.write(\"<a target=\"_blank\"ref=\"http://url_redirect.com\"><img border=\"0\" src=\"http://url_pictures.com\"></a>\");</body></html>";
NSString *baseURL = [mbAdUtil getBaseURL:html];
NSString *script = [[NSString alloc] initWithFormat:@"%@", html];
[adView loadHTMLString:script baseURL:[NSURL URLWithString:baseURL]];

我测试了很多我的计数器与打印相同的东西。

这是我测试的一些例子:

[webView loadHTMLString:@"" baseURL:nil];
[webView stopLoading]; 
[webView setDelegate:nil];
[webView removeFromSuperview];

另一个:

// remove all cached responses
[[NSURLCache sharedURLCache] removeAllCachedResponses];

// set an empty cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

// remove the cache for a particular request
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];

另外,我通过销毁我的项目实例并创建另一个来进行测试。 我破坏了我的 webview,我所有的变量......还有很多我不记得的东西。 我的计数器总是 1。

我更好的解决方案是:

[webView loadHTMLString:@"" baseURL:nil];
[webView stopLoading]; 
[webView setDelegate:nil];
[webView removeFromSuperview];

有了这个,我计算了 2 到 3 次打印,但视觉结果不是我所期望的。

【问题讨论】:

  • 你的计数变量是如何实现的?
  • 当我调用url时我的计数变量在数据库中,数据库是递增的

标签: html ios caching uiwebview request


【解决方案1】:

在 NSURLRequest 上添加缓存策略:

NSURL *URL = [NSURL URLWithString:@"http://url_pictures.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
[adView loadRequest:request]; 

以下三项政策最有可能帮助您解决问题:

  • NSURLRequestReloadIgnoringLocalCacheData,
  • NSURLRequestReloadIgnoringLocalAndRemoteCacheData,
  • NSURLRequestReloadIgnoringCacheData

【讨论】:

  • 我想使用 loadhtmlstring,而不是 loadRequest。使用 loadRequest 它可以工作
【解决方案2】:

我解决了我的问题! 我删除了所有测试,并添加了所有级别的时间戳:

NSString *html = @"<html><head></head><body>document.write(\"<a target=\"_blank\"ref=\"http://url_redirect.com?time = 123456789\"><img border=\"0\" src=\"http://url_pictures.com?time = 123456789\"></a>\");</body></html>";

NSString *baseURL = [mbAdUtil getBaseURL:html];

就好像有多级缓存一样。这很奇怪,但它有效......

我的解决方案是: 如果http://url_redirect.comhttp://url_pictures.com 调用另一个url,我们必须为我添加时间戳。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2013-08-08
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多