2014-03-27 58 views
3

對於客觀的C還是新的我已經發現了令人驚歎的AFNetworking網絡類。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改變一些文件中的更改不會出現,我不得不刪除我的應用程序,然後下載已更改的文件。

我不明白爲什麼我必須這樣做。

有人可以幫我嗎?

+0

只是刪除之前下載 – sage444

+0

沒有本地文件,但我想不刪除應用程序要上傳的文件,用戶必須刪除該應用獲取最後的數據? ? – user3237416

+0

不,意思是刪除'Menu.plist'的本地副本不是應用程序本身 – sage444

回答

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; 

}