我正在使用 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。