Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. //Adding download from another class
  2. let downloadItem = DownloadsTableViewCellItem(identifier: identifier, urlString: url3, infoLabelTitle: info, stateLabelTitle: "Downloading...", progressLabelTitle: "", action: DownloadsTableViewCellAction.Pause)
  3. DownloadsViewController().addDownloadItem(downloadItem, withIdentifier: identifier)
  4.  
  5. //Receiving download in DownloadsViewController
  6. func addDownloadItem(downloadItem: DownloadsTableViewCellItem, withIdentifier identifier: CheapjackFile.Identifier) {
  7.  
  8. downloadItems[identifier] = downloadItem
  9. identifiers.append(identifier)
  10.  
  11. CheapjackManager.sharedManager.download(downloadItem.url(), identifier: identifier)
  12. self.tableView.reloadData()
  13.  
  14. }
  15.  
  16. //DidFinishBlock
  17. func cheapjackManager(manager: CheapjackManager, didFinishDownloading withSession: NSURLSession, downloadTask: NSURLSessionDownloadTask, url: NSURL, forFile file: CheapjackFile) {
  18. dispatch_async(dispatch_get_main_queue()) {
  19. print("Did finish downloading file")
  20. SongsTableViewController().tableView.reloadData()
  21.  
  22. let filemanager = NSFileManager.defaultManager()
  23. let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
  24. print("DocumentsPath: (documentsPath)")
  25. let randomUUID: String = NSUUID().UUIDString + ".mp3"
  26. let destinationPath = documentsPath.stringByAppendingString("/" + randomUUID)
  27. print("DestinationPath: (destinationPath)")
  28.  
  29.  
  30. if (!filemanager.fileExistsAtPath(destinationPath as String)) {
  31.  
  32. let url1 = file.request.URL
  33. print("URL1: (url1)")
  34. print("Temp Loc: (String(url))")
  35.  
  36. let data = NSData(contentsOfURL: url)
  37.  
  38. if (data != nil) {
  39.  
  40.  
  41.  
  42. data!.writeToFile(destinationPath, atomically: false)
  43. //data!.writeToURL(NSURL(string:destinationPath)!, atomically: false)
  44.  
  45. print("The music files has been saved.")
  46.  
  47.  
  48. let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
  49.  
  50. let fetchRequest = NSFetchRequest(entityName: "Track")
  51. fetchRequest.predicate = NSPredicate(format: "url = %@", String(url1!))
  52.  
  53. let context = appDel.managedObjectContext
  54.  
  55. do {
  56.  
  57. let results = try context.executeFetchRequest(fetchRequest)
  58.  
  59. fetchResults = results as! [NSManagedObject]
  60.  
  61. if fetchResults.count != 0 {
  62.  
  63. let managedObject = fetchResults[0]
  64. managedObject.setValue(destinationPath, forKey: "fileURL")
  65.  
  66. appDel.saveContext()
  67.  
  68. SongsTableViewController().fetchData()
  69.  
  70. } else {
  71.  
  72. print("No track found")
  73. }
  74.  
  75. } catch let error as NSError {
  76. print("Could not fetch (error), (error.userInfo)")
  77. }
  78.  
  79. }
  80. } else {
  81. print("The files already exist")
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement