2016-06-10 172 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() 
    } 
} 
相關問題