Swift 如何在UserDirectory中获取url下载文件?
如何获取文件的url?或者查看用户目录中有哪些文件Swift 如何在UserDirectory中获取url下载文件?,swift,alamofire,Swift,Alamofire,如何获取文件的url?或者查看用户目录中有哪些文件 let destination = Alamofire.Request.suggestedDownloadDestination(directory: .UserDirectory, domain: .UserDomainMask) Alamofire.download(.GET, "http://best-muzon.com/dl/NNE2IxI6LYNtocG2fmFn3Q/1442201485/songs12/2015/02
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .UserDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, "http://best-muzon.com/dl/NNE2IxI6LYNtocG2fmFn3Q/1442201485/songs12/2015/02/vremja-i-steklo-imja-505-(best-muzon.com).mp3", destination: destination)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
dispatch_async(dispatch_get_main_queue()) {
//print("Total bytes read on main queue: \(totalBytesRead)")
self.label.text = String(totalBytesRead)
}
}
.response { request, response, _, error in
}
`
目标目录是.UserDirectory参数,文件名是URL的最后一个路径组件
let url = "http://best-muzon.com/dl/NNE2IxI6LYNtocG2fmFn3Q/1442201485/songs12/2015/02/vremja-i-steklo-imja-505-(best-muzon.com).mp3"
let fileName = url.lastPathComponent
我不知道.UserDirectory是主文件夹~还是下载文件夹~/downloads,但是连接目录和路径并创建文件URL也很容易
let destinationPath = NSHomeDirectory().stringByAddingPathComponent(fileName)
或
let destinationPath = NSHomeDirectory().stringByAddingPathComponent("Downloads/\(fileName)")
let destinationURL = NSURL(filePath: destinationPath)!