【问题标题】:Why is NSFileManager not working properly?为什么 NSFileManager 不能正常工作?
【发布时间】:2016-10-10 22:53:09
【问题描述】:

我的应用从 Internet 下载文件并使用 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()
    }
}

应用程序大小和输出表明文件仍然存在但未打开。

/private/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 - 无论我去哪里.mp3 确实存在

【问题讨论】:

  • NSLog运行时的文档路径,重新运行比较一下是否相同。

标签: ios swift2 nsfilemanager


【解决方案1】:

我让它工作了,问题似乎是每次 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()
    }
}

【讨论】:

    猜你喜欢
    • 2010-11-14
    • 2023-04-01
    • 2016-07-16
    • 2019-01-04
    • 2020-09-03
    • 2016-10-24
    • 2017-02-27
    • 2017-07-08
    • 2014-11-23
    相关资源
    最近更新 更多