IPhone - check if file update / update is required

I am trying to create a simple program that checks if an updated .txt file is needed on the iPhone. Right now I am checking the html header of the last change and I want to compare this with the file in my iPhone. If the website’s date is longer than the file on the iPhone, iPhone downloads and replaces the file.

I am using NSURL and downloading the file quite heavily.

Thanks in advance

+3
source share
3 answers

ASIHTTPRequest is a library that encapsulates HTTP requests and a bunch of intuitive checks (such as proxy authentication, caching, etc.) in one neat class that is an extension NSURLRequest. I recommend using this, you can choose a caching policy from the possible options here . It looks like you want a ASIAskServerIfModifiedCachePolicyserver that always requests if there is a newer version, and only loads if it is newer (it checks Last-Modified:, as well as other headers). You can also combine this caching policy with ASIFallbackToCacheIfLoadFailsCachePolicyso that if the server crashes, the last saved version will still be used.

Code example:

#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], . :

  • [request setDelegate:self], ASIHTTPRequestDelegate - requestFinished: requestFailed:,
  • ,

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

[request startSynchronous], startSynchronous startAsynchronous. .

: , . , , , . :

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 - .

+6

- HTTP ETag. ETag , . , ETags; , .

0

asihttprequest

[request setDownloadCache: [ASIDownloadCache sharedCache]]; [request setCachePolicy: ASIAskServerIfModifiedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy]; [setCacheStoragePolicy request: ASICachePermanentlyCacheStoragePolicy]; [request addRequestHeader: @ "Cache-Contro" value: @ "no-cache"];

and then check your completion block "[request responseStatusMessage]" if this line contains 304, which means your answer has not changed. else string contains 200, which means some new things available in your answer.

0
source

All Articles