开发者

iPhone - Checking to see if file needs update / update

I'm trying to make a simple application that checks to see if a .txt file on the i开发者_开发知识库Phone 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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜