【问题标题】:Objective-C iOS - UIWebView: how ignoring cached data?Objective-C iOS - UIWebView:如何忽略缓存数据?
【发布时间】:2018-08-17 23:14:58
【问题描述】:

我尝试了更多方法在 UIWebView 中加载请求,而不使用缓存数据。我必须从 url 加载 受保护的 pdf。不幸的是,任何尝试都没有成功。这是我的代码:

           [mypdfView removeFromSuperview];
            mypdfView = [[UIWebView alloc] initWithFrame:CGRectMake(0,85,self.view.frame.size.width,self.view.frame.size.height-150)];
            mypdfView.backgroundColor = [UIColor clearColor];
            mypdfView .autoresizesSubviews = YES;
            mypdfView .autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

            NSString *urlstring = @"https://user:psw@mysites.com/mypdf.pdf";
            NSURL *myUrl = [NSURL URLWithString:urlstring];
            NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
           [mypdfView  loadRequest:myRequest];

第一次尝试:忽略缓存数据的新请求

NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];

没有成功。 第二次尝试:清理 myRequest 缓存数据

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:myRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];

没办法。 3尝试:清除所有缓存并删除所有cookie

                [[NSURLCache sharedURLCache]removeAllCachedResponses];
                [[NSURLCache sharedURLCache] setDiskCapacity:0];
                [[NSURLCache sharedURLCache] setMemoryCapacity:0];
 for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

                    if([[cookie domain] isEqualToString:urlstring]) {

                        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
                    }
                }

再次。没有成功。所以试图删除所有凭据:

NSURLCredentialStorage *credentialsStorage = [NSURLCredentialStorage sharedCredentialStorage];
                NSDictionary *allCredentials = [credentialsStorage allCredentials];
                for (NSURLProtectionSpace *protectionSpace in allCredentials) {
                    NSDictionary *credentials = [credentialsStorage credentialsForProtectionSpace:protectionSpace];
                    for (NSString *credentialKey in credentials) {
                        [credentialsStorage removeCredential:[credentials objectForKey:credentialKey] forProtectionSpace:protectionSpace];
                    }
                }

再次失败。

有什么想法吗?

感谢您的帮助。

【问题讨论】:

  • 你怎么知道文件正在从缓存中加载?
  • '因为我改变了它然后重新加载了 webview 而 webview 没有改变。
  • 更多我从我的服务器中删除了它并且 webview 仍然显示它。

标签: ios objective-c caching uiwebview


【解决方案1】:

你试过用 WKWebView 代替 UIWebView 吗?我建议 UIWebView 已经被弃用了一段时间。对于 WKWebView,您可以执行以下操作

 WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
 [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {

                                                                             for (WKWebsiteDataRecord *record  in records) {

                                                                                 [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
                                                                                                                           forDataRecords:@[record]
                                                                                                                        completionHandler:^{

                                                                                                                            NSLog(@"Record Cleared: %@", record.displayName);

                                                                                                                        }];

                                                                             }

                                                                         }];

在任何情况下,我都会修改您正在使用的 NSURLRequest 对象的缓存策略,然后再将其加载到 Web 视图中。我认为您可能希望在将 -loadRequest 消息发送到您的 Web 视图实例之前将其设置为 NSURLRequestReloadIgnoringLocalCacheData,但您可能希望查看 NSURLRequestCachePolicy documentation 以查看所有可用选项及其各自的行为。

【讨论】:

  • 我已经解决了每次创建一个不同名称的新pdf文件。但下次我会使用 WKWebView!谢谢(我在发送加载请求之前设置了 NSURLRequestReloadIgnoringLocalCacheData。)
猜你喜欢
  • 2015-10-28
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 2023-03-21
  • 2012-02-21
  • 2013-09-07
  • 2016-10-14
  • 1970-01-01
相关资源
最近更新 更多