2016-06-10 204 views
-1

我的应用程序将从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确实存在

+1

NSLog的文件的路径,当你运行并重新运行和比较,看看他们是相同的。 – NeverHopeless

回答

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() 
    } 
} 
相关问题