func downloadGameAudioWithUrl(urlStr: String) {
let url = NSURL(string: urlStr)
let request = NSURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let downloadSession = NSURLSession(configuration:
config, delegate: self, delegateQueue: nil)
let downloadTask = downloadSession.downloadTaskWithRequest(request) { (location, response, error) in
print("文件下载路径: \(location.path)")
document 备份,下载的文件不能放在此文件夹中
cache 缓存的,不备份,重新启动不会被清空,如果缓存内容过多,可以考虑新建一条线程检查缓存目录中的文件大小,自动清理缓存,给用户节省控件
tmp 临时,不备份,不缓存,重新启动iPhone,会自动清空
*/
let path = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).lastEle
self.downloadFileName = (url?.lastPathComponent)!
let filePath = path.stringByAppendingString("/" + self.downloadFileName)
print("文件保存路径: \(filePath)")
let fileManager = NSFileManager.defaultManager()
do {
try fileManager.copyItemAtPath(location!.path!, toPath: filePath)
} catch let error as NSError {
print(error.localizedDescription)
}
}
downloadTask.resume()
}
}