iPhone - 检查文件是否需要更新/更新

发布于 2024-10-24 03:00:51 字数 184 浏览 4 评论 0原文

我正在尝试制作一个简单的应用程序来检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赴月观长安 2024-10-31 03:00:51

ASIHTTPRequest 是一个封装 HTTP 请求和一堆直观检查(如代理身份验证、缓存等)的库在一个简洁的类中,它是 NSURLRequest 的扩展。我建议使用这个,您可以从此处<的可能选项中选择一个缓存策略/a>.看起来您想要 ASIAskServerIfModifiedCachePolicy,它总是询问服务器是否有更新的版本,并且仅在较新的情况下下载(它检查 Last-Modified: 以及其他标题)。您还可以将此缓存策略与 ASIFallbackToCacheIfLoadFailsCachePolicy 结合使用,这样,如果联系服务器失败,仍将使用最后存储的版本。

示例代码:

#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h"

/* doing the actual check. replace your existing code with this. */
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:myTxtFileURL];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request startSynchronous];
NSString *latestText = [request responseString];
[request release];

请注意,我只使用 [request startSynchronous] 因为它很容易粘贴到示例代码中。您应该使用:

  1. [request setDelegate:self],然后在当前类中的某处实现ASIHTTPRequestDelegate协议来处理requestFinished:requestFailed:,或
  2. 一个块,您可以使用它进行设置

    [请求setCompletionBlock:^
    {
        /* 下载完成后运行的代码 */
    }];
    [请求设置失败块:^
    {
        /* 下载失败时运行的代码 */
    }];
    

其中任何一个都需要在[request startSynchronous]之前完成,然后您需要更改startSynchronousstartAsynchronous。请查看“如何使用”选项卡上的更多文档链接。

编辑:您说您想比较文件本身。我不明白你到底想要什么,但如果你想将旧文件中的内容与新文件中的内容进行比较,那么你需要首先获取旧文件文本的副本。要做到这一点:

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:myTxtFileURL];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
NSStringEncoding encoding;
NSError *error = nil;
NSString *oldText =
[NSString stringWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForRequest:request]
                      usedEncoding:encoding
                             error:&error];
[request startSynchronous];
NSString *newText = [request responseString];
[request release];

/* now compare the NSString oldText to newText however you like. */

学习成为一名优秀的程序员的一部分是能够使用和探索可用的文档和资源。我建议您阅读我链接到的文档,阅读 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 want ASIAskServerIfModifiedCachePolicy, which always asks the server if there is a newer version and only downloads if it is newer (it checks Last-Modified: as well as other headers). You can also combine this caching policy with ASIFallbackToCacheIfLoadFailsCachePolicy so that if contacting the server fails, the last stored version will still be used.

Sample code:

#import "ASIHTTPRequest.h"
#import "ASIDownloadCache.h"

/* doing the actual check. replace your existing code with this. */
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:myTxtFileURL];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request startSynchronous];
NSString *latestText = [request responseString];
[request release];

Note that I only use [request startSynchronous] because it's easy to paste in sample code. You should use:

  1. [request setDelegate:self] and then implement the ASIHTTPRequestDelegate protocol in the current class somewhere to handle requestFinished: and requestFailed:, or
  2. A block, which you can set with

    [request setCompletionBlock:^
    {
        /* code to run after the download finishes */
    }];
    [request setFailedBlock:^
    {
        /* code to run if the download failed */
    }];
    

Either of those need to be done before [request startSynchronous], and then you need to change startSynchronous to startAsynchronous. 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:

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:myTxtFileURL];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
NSStringEncoding encoding;
NSError *error = nil;
NSString *oldText =
[NSString stringWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForRequest:request]
                      usedEncoding:encoding
                             error:&error];
[request startSynchronous];
NSString *newText = [request responseString];
[request release];

/* now compare the NSString oldText to newText however you like. */

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.

明天过后 2024-10-31 03:00:51

一种选择是使用 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.

吐个泡泡 2024-10-31 03:00:51

您可以将这些标签与您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文