1

我有一个严重的问题。在我的应用程序中,我正在使用 NSURLConnection 下载 XML 文件。到目前为止,一切都很好。但是有时,NSURLConnection 会返回请求文件的旧版本,尽管我将 cachePolicy 设置为 NSURLRequestReloadIgnoringLocalAndRemoteCacheData。该错误真正令人作呕的部分是,连接总是返回旧版本的文件,直到我激活然后停用飞行模式或重新启动设备。

另一个有趣的事实是:当我激活 Wifi 并调用应用程序重新加载文件时,会返回文件的正确和实际版本。但是关闭 Wifi 后,旧版本会再次重新调整。

我真的对这个问题感到绝望,希望有人能帮助我吗?

这是我班上的代码,下载文件:

#import "HTTPReciever.h"
#import "Constants.h"

@interface HTTPReciever ()
@property (strong, nonatomic) NSMutableData *receivedData;
@property (strong, nonatomic) NSString *directoryPath;
@property (strong, nonatomic) NSString *filename;
@property (assign, nonatomic) BOOL storeData;
@end

@implementation HTTPReciever
@synthesize receivedData = _receivedData;
@synthesize delegate = _delegate;
@synthesize directoryPath = _directoryPath;
@synthesize filename = _filename;


- (BOOL)recieveDataFromURL:(NSURL *)url
      storeAtDirectoryPath:(NSString *)directoryPath
                  withName:(NSString *)name
{
    [self setDirectoryPath:directoryPath];
    [self setFilename:name];
    [self setStoreData:YES];
    return [self recieveDataFromURL:url];
}

- (BOOL)recieveDataFromURL:(NSURL *)url
{
    NSURLRequest *theRequest = [[NSURLRequest alloc] initWithURL:url
                                                     cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                 timeoutInterval:cDefaultHTTPTimeOutInterval];

    self.receivedData = nil;
    self.receivedData = [[NSMutableData alloc] initWithLength:0];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest
                                                                  delegate:self
                                                          startImmediately:NO];
    if ([NSURLConnection canHandleRequest:theRequest] && connection) {
        [connection start];
        return YES;
    } else {
        return NO;
    }
}



#pragma mark - NSURLConnectionDelegate protocol
- (void)connection:(NSURLConnection *)connection
     didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self.receivedData setLength:0];
    if ([response isKindOfClass:[NSHTTPURLResponse self]]) {
        NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
        NSString *modified = [headers objectForKey:@"Last-Modified"];
        if (modified) {
            // not yet used
        }
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    if (self.storeData) {
        [[NSFileManager defaultManager] createDirectoryAtPath:self.directoryPath
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:nil];
        [self.receivedData writeToFile:[NSString stringWithFormat:@"%@%@", self.directoryPath, self.filename]
                            atomically:YES];
    }

    [self.delegate receiver:self
       didFinishLoadingData:self.receivedData
                lastVersion:YES];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    [self.delegate receiverFailedLoadingData:self];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}


@end
4

0 回答 0