为什么 NSFileManager 不能正常工作?

Why is NSFileManager not working properly?

我的应用程序从互联网下载文件并使用 NSFileManager 存储它们一切似乎都正常,直到我使用 xCode 重新运行应用程序(我无法使用这些文件,删除它们-。 ...)

但如果我没有用 xCode 重新运行它并在我的 phone 上正常使用它,它似乎没有这个问题。 (例如:我可以播放下载的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 does exist

/var/mobile/Containers/Data/Application/4B5BE8F6-C71A-4BDC-86E5-3132AD9330B8/Documents/OneRepublic - Wherever I Go.mp3 does exist

我成功了,问题似乎是每次 xCode 构建和安装应用程序时,xCode 都会为应用程序提供一个新路径。

所以解决方法是我使用下载的文件路径作为最后一个组件。

let newpath = createFilePathForDownload(path.lastPathComponent!)

这将return下载文件的新路径。

所以为了使描述中代码的最后一部分起作用,我使用了这样的东西:

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()
    }
}