2013-12-12 33 views
0

的地方這是我的代碼:AFNetworking 2.0返回下載臨時位置,而不是它保存

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
    NSString *URLForSend = [NSString stringWithFormat:@"%@/pk/%ld/", [_downloadFileURLString stringByAppendingString:_userName], (long)pk ]; 
    NSURL *URL = [NSURL URLWithString:URLForSend]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
    __unsafe_unretained typeof(self) weakSelf = self; 
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 
     NSURL *downloadDirectoryPath = [self downloadAreaDirectoryURL]; 
     return [downloadDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]]; 
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 

     NSString *relativeFilePathString = [filePath lastPathComponent]; 
     [weakSelf updateFilePathFromStack:relativeFilePathString withpk:pk]; 
     NSLog(@"File downloaded to: %@", filePath); 

    }]; 
    return downloadTask; 

我閱讀文檔幾次和Im留下了2個問題:

  1. destination block從哪裏得到targetPath
  2. 在完成處理程序中,filePath將我帶到臨時文件,我想要最終保存的文件的名稱。

我希望它被保存到downloadedAreaDirectoryURL(目錄我在緩存文件夾中打開),併爲它是downloadedAreaDirectoryURL/fileNameLikeItsCalledFromServer

請注意,我不知道文件名前其實,我得到它,因爲我與PK數從DB

回答

1
  1. AFURLSessionManager訪問它使用的iOS 7.0中引入的新NSURLSession類。具體而言,它使用NSURLSessionDownloadDelegate和方法URLSession:downloadTask:didFinishDownloadingToURL:,該方法將文件URL發送到臨時文件。

  2. 我只是想你已經發布相同的代碼(略有修改,以得到正確的路徑),我得到了相同的URL返回從destination塊到completionHandlerfilePath參數。另外,該文件已成功移到我的緩存目錄中。我建議你添加一個監聽器AFURLSessionDownloadTaskDidFailToMoveFileNotification來驗證文件移動操作沒有失敗。

+0

因此在filePath中的完成處理程序中,您獲得了移動文件的正確url?不是臨時?也許你是對的,我檢查是否移動失敗... –

+0

是的,我有移動文件的URL。 – StatusReport

+1

Magniv,會嘗試。謝謝。 –

相關問題