我的應用程序將從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