Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
- guard let url = downloadTask.originalRequest?.url else { return }
- let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
- let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
- let booksPath = documentsPath.appendingPathComponent(self.saveTo, isDirectory: true).appendingPathComponent(url.lastPathComponent)
- let pattern = "_\.\+-=!@#\$\%\^\&\*()!"
- let title = url.lastPathComponent.replacingOccurrences(of: pattern, with: " ", options: .regularExpression, range: nil)
- let _ = CoreDataHelper.shared.create(params: ["title" : title], objectType: Book.self)
- // delete original copy
- try? FileManager.default.removeItem(at: destinationURL)
- // copy from temp to Document
- do {
- try FileManager.default.copyItem(at: location, to: booksPath)
- } catch let error {
- print("Copy Error: (error.localizedDescription)")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement