我的应用程序将从Internet下载文件并把它们存储使用NSFileManager
一切似乎工作,直到我使用的Xcode(我不能使用的文件,将其删除 - ......)为什么NSFileManager无法正常工作?
但是,如果我重新运行该应用程序没有用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 /移动/容器/数据/应用程序/ 4B5BE8F6-C71A-4BDC-86E5-3132AD9330B8 /文档/ OneRepublic的 - 无论我Go.mp3确实存在
NSLog的文件的路径,当你运行并重新运行和比较,看看他们是相同的。 – NeverHopeless