SHARE
TWEET

Untitled

a guest Jun 15th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
  2.     guard let url = downloadTask.originalRequest?.url else { return }
  3.     let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  4.     let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
  5.     let booksPath = documentsPath.appendingPathComponent(self.saveTo, isDirectory: true).appendingPathComponent(url.lastPathComponent)
  6.  
  7.     let pattern = "_\.\+-=!@#\$\%\^\&\*()!"
  8.     let title   = url.lastPathComponent.replacingOccurrences(of: pattern, with: " ", options: .regularExpression, range: nil)
  9.  
  10.     let _ = CoreDataHelper.shared.create(params: ["title" : title], objectType: Book.self)
  11.  
  12.     // delete original copy
  13.     try? FileManager.default.removeItem(at: destinationURL)
  14.  
  15.     // copy from temp to Document
  16.     do {
  17.         try FileManager.default.copyItem(at: location, to: booksPath)
  18.     } catch let error {
  19.         print("Copy Error: (error.localizedDescription)")
  20.     }
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top