Ios 为什么NSFileManager不能正常工作?
我的应用程序从internet下载文件,并使用Ios 为什么NSFileManager不能正常工作?,ios,swift2,nsfilemanager,Ios,Swift2,Nsfilemanager,我的应用程序从internet下载文件,并使用NSFileManager存储这些文件。在我使用xCode重新运行应用程序之前,一切似乎都正常(我无法使用这些文件,请删除它们-…) 但是如果我没有用xCode重新运行它,并且在我的手机上正常使用它,它似乎没有这个问题。(示例:我可以播放下载的mp3) 下面是我的项目中的一些代码 // Creates a new path for the download func createFilePathForDownload(filePath: String
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()
}
}
应用程序大小和输出表明文件仍然存在,但尚未打开
/私有/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-where I Go.mp3确实存在
我让它工作了,问题似乎是每次xCode构建和安装应用程序时,xCode都会为应用程序提供一个新的路径 所以解决方法是我使用下载的文件路径最后一个组件,如下所示
let newpath = createFilePathForDownload(path.lastPathComponent!)
这将返回下载文件的新路径
因此,为了使描述中的代码的最后一部分起作用,我使用了如下内容:
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()
}
}
在运行和重新运行时记录文档路径,并进行比较,查看它们是否相同。