0

我有一个应用程序可以下载每隔几个月更新的 PDF,每次下载某个 PDF 时,我还会下载一个包含一个字符的 .txt 文件。每次我在服务器上上传更新的 PDF 时,我也会将服务器上 .txt 文件中的字符数加一。

当用户在我的应用程序中点击“更新”按钮时,我会将来自服务器的 .txt 文件中的字符串与本地 .txt 文件的字符串进行比较。

这是检查新 PDF 是否可用的一种非常轻量级的方法,但我想问是否有更好的方法或更轻松的方法来执行此操作,而无需为我拥有的每个 PDF 文件创建一个 .txt 文件服务器?

提前感谢您提供的任何帮助!

这是我用来检查新 PDF 的代码:

NSString *documentDirectory = [(AppDelegate *)[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory];

NSString *path = [documentDirectory stringByAppendingPathComponent:@"test.txt"];

NSString *server = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"foo.com/test.txt"] encoding:NSASCIIStringEncoding error:nil];

NSString *local = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];

if(![local isEqualToString:server])
{
   // I Download the new PDF and the new .txt file here;
   // simple NSURLConnection stuff, no need to elaborate
}
4

1 回答 1

2

Off the top of my head, the HTTP HEAD method can be used to fetch just the headers rather than the data. If the creation date in the headers is later than the last date you downloaded the data, it's time to fetch a new copy. This can be done with NSURLRequest and NSURLConnection.

If your server doesn't support the HEAD request or something else prevents its use, you could have a second TXT file with a version/hash/unique identifier in it. Download that instead and compare against the stored value. If it's changed, download the full PDF and save the version text file to disk for reference later.

If security is an issue and you need to be able to trust the server, use HTTPS in either case.

于 2012-11-25T18:24:41.983 回答