title: set the filename after finish download in Swift
body: <p>I trying to develop Download manager app for iPhone. I am using this class for downloading operation:</p>
  
  <pre><code>import UIKit
  import Foundation
  
  typealias CompleteHandlerBlock = () -&gt; ()
  
  class newDownloadObject: NSObject,NSURLSessionDelegate, NSURLSessionDownloadDelegate {
      var session: NSURLSession!
      var handlerQueue: [String : CompleteHandlerBlock]!
  
      class var sharedInstance: newDownloadObject {
          struct Static {
              static var instance : newDownloadObject?
              static var token : dispatch_once_t = 0
          }
  
          dispatch_once(&amp;Static.token) {
              Static.instance = newDownloadObject()
              Static.instance!.handlerQueue = [String : CompleteHandlerBlock]()
          }
  
          return Static.instance!
      }
  
      //MARK: session delegate
      func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
          println("session error: \(error?.localizedDescription).")
      }
  
      func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -&gt; Void) {
          completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
      }
  
      func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {        
          println("session \(session) has finished the download task \(downloadTask) of URL \(location).")
      }
  
      func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
          println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")
  
      }
  
      func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
          println("session \(session) download task \(downloadTask) resumed at offset \(fileOffset) bytes out of an expected \(expectedTotalBytes) bytes.")
      }
  
      func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
          if error == nil {
              println("session \(session) download completed")
          } else {
              println("session \(session) download failed with error \(error?.localizedDescription)")
          }
      }
      func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
          println("background session \(session) finished events.")  
          if !session.configuration.identifier.isEmpty {
              callCompletionHandlerForSession(session.configuration.identifier)
          }
      }
  
      //MARK: completion handler
      func addCompletionHandler(handler: CompleteHandlerBlock, identifier: String) {
          handlerQueue[identifier] = handler
      }
  
      func callCompletionHandlerForSession(identifier: String!) {
          if(identifier == nil){
              return
          }
          var handler : CompleteHandlerBlock = handlerQueue[identifier]!
          handlerQueue!.removeValueForKey(identifier)
          handler()
      }
  }
  </code></pre>
  
  <p>this works great but i want to access downloaded file from itunes. therefor this file must be in Documents directory. </p>
  
  <p>I try to move this file after finish download operation(<code>didFinishDownloadingToURL</code> method) to Document directory. However I get problem in here. The problem is the file name. It is like that <br>"CFNetworkDownload_qsmwsB.tmp" and after downloading finished file it doesnt change to original name.( The file name must be "myBook.pdf" ) as a result I see ".tmp" file(s) in iTunes. </p>
  
  <p><strong>How i can download a file directly to Documents directory or How i can change the filename after finish download?</strong></p>