Objective-C iOS - UIWebView:如何忽略缓存数据?

时间:2018-03-09 11:30:37

标签: ios objective-c caching uiwebview

我尝试了更多方法在UIWebView中加载请求而不使用缓存数据。我要从网址加载受保护的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];
                    }
                }

再次失败。

有什么想法吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用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消息发送到您的网络视图实例之前,我认为您想要将其设置为NSURLRequestReloadIgnoringLocalCacheData,但您可能需要查看NSURLRequestCachePolicy documentation以查看所有可用选项及其各自的行为。