My app downloads files from the internet and stores them using NSFileManager
everything seems to be working until I rerun the app using xCode (I can't use the files, remove them-....)
But if I didn't rerun it with xCode and used it normally on my phone it doesn't seem to have this issue. (example: I can play the downloaded mp3)
Here is some code from my project
// 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()
}
}
}
Using something like this will not work
func playMediaAtPath(path: NSURL) {
let player = AVPlayer(URL: path)
self.moviePlayer.player = player
self.presentViewController(self.moviePlayer, animated: true) {
self.moviePlayer.player?.play()
}
}
The app size and output indicate that the files are still exist but not opened.
/private/var/mobile/Containers/Data/Application/4B5BE8F6-C71A-4BDC-86E5-3132AD9330B8/tmp/CFNetworkDownload_5rFN0V.tmp does exist
/var/mobile/Containers/Data/Application/4B5BE8F6-C71A-4BDC-86E5-3132AD9330B8/Documents/OneRepublic - Wherever I Go.mp3 does exist
I got it to work, the issue seems that every time xCode builds and installs the Application, xCode will give a new path for the application.
So a workaround this I used the downloaded file path last component like this.
let newpath = createFilePathForDownload(path.lastPathComponent!)
this will return the new path for the downloaded file.
So to make the last part of the code in the description work I used something like this:
func playMediaAtPath(path: NSURL) {
let newpath = sharedStore.filePathForDownload(path.lastPathComponent!)
print(newpath?.path)
let player = AVPlayer(URL: newpath!)
self.moviePlayer.player = player
self.presentViewController(self.moviePlayer, animated: true) {
self.moviePlayer.player?.play()
}
}