0

我正在使用 AFNetworking 下载 pdf(每周更改一次)并使用以下代码将其保存到文档目录中:

//Get the PDF    

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.somewebsiteaddress/CurrentEdition1.pdf"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath =  [documentsDirectory stringByAppendingPathComponent:@"CurrentEdition1.pdf"];

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Successfully downloaded file to %@", filePath);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation start];

然后,我想在 UIWebView 的应用程序的另一部分中读取该保存的文件(下载后)并使用以下代码:

//Now create Request for the file that was saved in your documents folder

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"CurrentEdition1.pdf"];

NSURL *url = [NSURL fileURLWithPath:filePath];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webVieweedition setUserInteractionEnabled:YES];

[webVieweedition setDelegate:self];

[webVieweedition loadRequest:requestObj];

我使用了一些单页 pdf 文档进行测试 - 将它们加载到服务器上,然后查看它们是否已下载,然后在更改时查看它们。问题是,这似乎只有大约 50% 的时间有效。我会放一个新文件,有时正确的文件会显示在 UIWebView 中,有时它会显示前一个文件而不是新文件。在尝试转到 UIWebView 之前,我一直在等待,直到看到下载完成消息(尽管我知道客户不会这样做,但这是另一个问题)。无论如何,我是 XCode 的新手,只是一个 web html 人。这让我头晕了两天。使用故事板、ARC、XCode 4.6.2。

4

1 回答 1

1

如果我理解正确,有时您会在应用程序中看到相同的 pdf,尽管您在网络服务器上更改了它们?可能是缓存的原因,尝试以这种方式构造请求,忽略缓存

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.somewebsiteaddress/CurrentEdition1.pdf"]  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
于 2013-05-22T13:35:07.407 回答