iPhone - 检查文件是否需要更新/更新
我正在尝试制作一个简单的应用程序来检查 iPhone 上的 .txt 文件是否需要更新。现在我正在检查上次修改的 html 标头,我想将其与我的 iPhone 中的文件进行比较。如果网站的日期晚于 iPhone 上的文件,iPhone 会下载并替换该文件。
我正在使用 NSURL 并且下载文件时遇到了相当困难。
提前致谢
I'm trying to make a simple application that checks to see if a .txt file on the iPhone needs updated. Right now I'm checking the html header of last modified, and I want to compare this to the file within my iPhone. If the website's date is later than the file on the iPhone, the iPhone downloads and replaces the file.
I'm using NSURL and having a pretty hard time with downloading the file.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASIHTTPRequest 是一个封装 HTTP 请求和一堆直观检查(如代理身份验证、缓存等)的库在一个简洁的类中,它是 NSURLRequest 的扩展。我建议使用这个,您可以从此处<的可能选项中选择一个缓存策略/a>.看起来您想要
ASIAskServerIfModifiedCachePolicy
,它总是询问服务器是否有更新的版本,并且仅在较新的情况下下载(它检查Last-Modified:
以及其他标题)。您还可以将此缓存策略与ASIFallbackToCacheIfLoadFailsCachePolicy
结合使用,这样,如果联系服务器失败,仍将使用最后存储的版本。示例代码:
请注意,我只使用
[request startSynchronous]
因为它很容易粘贴到示例代码中。您应该使用:[request setDelegate:self]
,然后在当前类中的某处实现ASIHTTPRequestDelegate
协议来处理requestFinished:
和requestFailed:
,或一个块,您可以使用它进行设置
其中任何一个都需要在
[request startSynchronous]
之前完成,然后您需要更改startSynchronous
到startAsynchronous
。请查看“如何使用”选项卡上的更多文档链接。编辑:您说您想比较文件本身。我不明白你到底想要什么,但如果你想将旧文件中的内容与新文件中的内容进行比较,那么你需要首先获取旧文件文本的副本。要做到这一点:
学习成为一名优秀的程序员的一部分是能够使用和探索可用的文档和资源。我建议您阅读我链接到的文档,阅读 Apple 关于 iOS 的文档,或者通过 Google 搜索您的下一个问题。 Apple 文档中有关比较字符串的部分位于 此处。
ASIHTTPRequest is a library which encapsulates HTTP requests and a bunch of intuitive checks (like proxy authentication, caching etc) in one neat class which is an extension of
NSURLRequest
. I recommend using this, you can pick a caching policy out of the possible options found here. It looks like you wantASIAskServerIfModifiedCachePolicy
, which always asks the server if there is a newer version and only downloads if it is newer (it checksLast-Modified:
as well as other headers). You can also combine this caching policy withASIFallbackToCacheIfLoadFailsCachePolicy
so that if contacting the server fails, the last stored version will still be used.Sample code:
Note that I only use
[request startSynchronous]
because it's easy to paste in sample code. You should use:[request setDelegate:self]
and then implement theASIHTTPRequestDelegate
protocol in the current class somewhere to handlerequestFinished:
andrequestFailed:
, orA block, which you can set with
Either of those need to be done before
[request startSynchronous]
, and then you need to changestartSynchronous
tostartAsynchronous
. Look at the link for more documentation on the "How to use it" tab.Edit: You say you want to compare the files themselves. I don't understand exactly what you want, but if you want to compare the content in the old file with the content in the new file then you'll need to first grab a copy of the old file's text. To do this:
Part of learning to become a good programmer is being able to use and explore the documentation and resources available to you. I suggest that you read the documentation I have linked to, read Apple's documentation on iOS, or do a Google search for your next question. The section on comparing strings in Apple's documentation is here.
一种选择是使用 HTTP 的 ETag 标头 有条件地从服务器加载文件。您需要记住上次更新文件时的 ETag 值,但这并不困难。您还需要一台配置为使用 ETag 的服务器;除非服务器完全不受您的控制,否则这也不困难。
One option is to load the file conditionally from the server using HTTP's ETag header. You'll need to remember the ETag value from the last time you updated the file, but that's not difficult. You'll also need a server that's configured to use ETags; this also isn't difficult unless the server is completely out of your control.
您可以将这些标签与您的 asihttprequest
[request setDownloadCache:[ASIDownloadCache sharedCache]]; 一起使用
[请求setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[请求设置CacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[请求addRequestHeader:@"Cache-Contro" value:@"no-cache"];
然后检查您的完成块“[request responseStatusMessage]”,如果该字符串包含 304 则意味着您的响应未修改,否则字符串包含 200 则意味着您的响应中存在一些新内容。
you can use these tag with your asihttprequest
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request addRequestHeader:@"Cache-Contro" value:@"no-cache"];
and then check in your completion block "[request responseStatusMessage]" , if this string contains 304 that means your response not modified else string contains 200 that means some new things available in your response.