Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  2.  
  3. if indexPath.row > 0 {
  4.  
  5. let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
  6. print(path)
  7. let documentDirectoryPath:String = path[0]
  8. let fileManager = FileManager()
  9. let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file(indexPath.row).mp3"))
  10.  
  11.  
  12. if fileManager.fileExists(atPath: destinationURLForFile.path){
  13.  
  14. self.performSegue(withIdentifier: "segue", sender: self)
  15.  
  16. }else{
  17.  
  18. index = indexPath.row
  19.  
  20. let url = URL(string: "http:link(indexPath.row).mp3")!
  21. let downloadTaskLocal = backgroundSession.downloadTask(with: url)
  22. downloadTaskLocal.resume()
  23. }
  24. }
  25. }
  26.  
  27. func urlSession(_ session: URLSession,
  28. downloadTask: URLSessionDownloadTask,
  29. didFinishDownloadingTo location: URL){
  30.  
  31. let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
  32. let documentDirectoryPath:String = path[0]
  33. let fileManager = FileManager()
  34. let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file(index).mp3"))
  35.  
  36. do {
  37. try fileManager.moveItem(at: location, to: destinationURLForFile)
  38. }catch{
  39. print("error")
  40. }
  41. }
  42.  
  43. fileManager.moveItem(at: location, to: destinationURLForFile)
  44.  
  45. let fileName = location.lastPathComponent
  46.  
  47. let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/(fileName)"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement