AFNetWorking下载会话不会更新我的文件

时间:2014-03-27 09:16:30

标签: ios nsurlconnection nsurl afnetworking-2

仍然是客观C的新手我发现了惊人的AFNetworking网络课程。

使用dic我有我的代码下载我的文件并写入NSDocumentDirectory

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://myAdress/Menu.plist"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
{
    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
    return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);

    NSDictionary *dicMenuPlist = [NSDictionary dictionaryWithContentsOfFile:[filePath path]];
    NSLog(@"Dic Menu Plist = %@",dicMenuPlist);


}];
[downloadTask resume];

这很好但是当我在我的Menu.plist文件中更改某些内容时,更改没有出现,我必须删除我的应用程序然后下载已更改的文件。

我不明白我为什么要这样做。

是的,有人能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

答案here

删除目标块中的现有文件。

destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

   NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
   NSURL *fileURL = [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];

   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
   if ([httpResponse statusCode] == 200) {
      // delete existing file (using file URL above)
   }
   return fileURL;

}

答案 1 :(得分:0)

默认方法AFNetworking 3.0不刷新您的下载程序文件。

如果你想在Objective-C + iOS9.0中重写这个文件,你需要这样做:

- (NSURLSessionDownloadTask *) downloadDocsFromUrl:(NSString *) url withSuccesBlock:(DocModelBlock) docModelBlock withErrorBlock:(ErrorBlock) errorBlock {

    NSURL *URL = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [self.sessionManager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;

        NSURL *documentsDirectoryURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];

        if ([httpResponse statusCode] == 200) {
            NSURL *urlPath = [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

            if ([fileManager fileExistsAtPath:urlPath.path]) {
                [fileManager removeItemAtPath:urlPath.path error:&error];
            }
        }

        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error) {
            errorBlock(error);
        } else {
            docModelBlock(filePath);
        }
    }];
    [downloadTask resume];

    return downloadTask;
}