【发布时间】:2016-10-10 22:53:09
【问题描述】:
我的应用从 Internet 下载文件并使用 NSFileManager 存储它们,在我使用 xCode 重新运行应用之前,一切似乎都正常工作(我无法使用这些文件,删除它们-...)
但是如果我没有用 xCode 重新运行它并在我的手机上正常使用它,它似乎没有这个问题。 (例如:我可以播放下载的mp3)
这是我项目中的一些代码
// Creates a new path for the download
func createFilePathForDownload(filePath: String) -> NSURL? {
let searchForDoucumentDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let documentDirectory = searchForDoucumentDirectory.first
return documentDirectory!.URLByAppendingPathComponent(filePath)
}
// After the download is done downloading this NSURLSessionDownloadTaskDelegate method will excute
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
for (_, downloadInProgress) in allDownloads.enumerate() {
if downloadTask.isEqual(downloadInProgress.downloadTask) {
let newPath = createFilePathForDownload((downloadInProgress.downloadTask.response?.suggestedFilename!)!)
downloadInProgress.filePathTemp = location
downloadInProgress.filePath = newPath
if (NSFileManager.defaultManager().fileExistsAtPath(location.path!) == true) {
print("\(location.path!) does exist")
}
do {
try NSFileManager.defaultManager().copyItemAtURL(location, toURL: downloadInProgress.filePath)
} catch {
}
do {
try NSFileManager.defaultManager().removeItemAtURL(location)
} catch {
}
if (NSFileManager.defaultManager().fileExistsAtPath(downloadInProgress.filePath.path!) == true) {
print("\(downloadInProgress.filePath.path!) does exist")
}
downloadInProgress.downloadData = nil
downloadInProgress.downloadStatus = DownloadStatus.Finished
delegate?.downloadHasBeenFinished!(downloadInProgress)
saveChanges()
}
}
}
使用这样的东西是行不通的
func playMediaAtPath(path: NSURL) {
let player = AVPlayer(URL: path)
self.moviePlayer.player = player
self.presentViewController(self.moviePlayer, animated: true) {
self.moviePlayer.player?.play()
}
}
应用程序大小和输出表明文件仍然存在但未打开。
/private/var/mobile/Containers/Data/Application/4B5BE8F6-C71A-4BDC-86E5-3132AD9330B8/tmp/CFNetworkDownload_5rFN0V.tmp 确实存在
/var/mobile/Containers/Data/Application/4B5BE8F6-C71A-4BDC-86E5-3132AD9330B8/Documents/OneRepublic - 无论我去哪里.mp3 确实存在
【问题讨论】:
-
NSLog运行时的文档路径,重新运行比较一下是否相同。
标签: ios swift2 nsfilemanager