[英]iOS know when file has changed
I am downloading a mp4 file with my iOS app, it works fine using 我正在使用我的iOS应用程序下载mp4文件,它可以正常使用
NSData dataWithContentsOfURL
NSData dataWithContentsOfURL
but i need to updated only if the file has been updated, 但我只有在文件更新后才需要更新,
can I check the file headers? 我可以检查文件头吗? or what is the best way to determine if the file has been updated so I downloaded it again?
或者确定文件是否已更新的最佳方法是什么,以便我再次下载?
Thanks 谢谢
You can access the metadata using the http HEAD
request, as explained in this SO answer . 您可以使用
http HEAD
请求访问元数据,如本SO答案中所述 。 You'll need to create some sort of parser to pick out the information you need, though. 但是,您需要创建某种解析器来选择所需的信息。 Note that this might not work with every server, depending on how it has been set up.
请注意,这可能不适用于每台服务器,具体取决于它的设置方式。
If you have control of the server yourself, I'd recommend using a php script to output the date the file has been last changed, which you would call before downloading the file. 如果您自己控制服务器,我建议您使用php脚本输出上次更改文件的日期,然后在下载文件之前调用该日期。
Personally, I prefer placing a manifest file (usually a plist) alongside the file in question, as it can hold even more data, for example metadata for several files, the number of entries in a database and the like. 就个人而言,我更喜欢将清单文件(通常是plist)放在有问题的文件旁边,因为它可以容纳更多数据,例如几个文件的元数据,数据库中的条目数等。 A backdraw of this approach is that you'll need to keep this file up to date, though.
这种方法的退款是你需要保持这个文件是最新的。 But often, that is worth the while.
但通常,这是值得的。
Lastly, as rckoenes has mentioned, dataWithContentsOfURL is not a very good way to download files, espcially large ones. 最后,正如rckoenes所提到的,dataWithContentsOfURL并不是一种非常好的下载文件的方法,特别是大文件。 You really should be using some sort of datamanager class, which manages a
NSURLConnection
. 你真的应该使用某种datamanager类来管理
NSURLConnection
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.