Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Last active November 14, 2019 14:26
Show Gist options
  • Save anupamchugh/bafdea72a505f681f07d623f62dae235 to your computer and use it in GitHub Desktop.
Save anupamchugh/bafdea72a505f681f07d623f62dae235 to your computer and use it in GitHub Desktop.
extension ViewController: QLPreviewControllerDelegate {
func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
return .createCopy
}
func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let currentTimeStamp = String(Int(NSDate().timeIntervalSince1970))
let destinationUrl = documentsDirectoryURL.appendingPathComponent("newFile\(currentTimeStamp).\(modifiedContentsURL.pathExtension)")
if FileManager.default.fileExists(atPath: destinationUrl.path) {
debugPrint("The file already exists at path")
}
else{
do {
try FileManager.default.moveItem(at: modifiedContentsURL, to: destinationUrl)
print("File moved to documents folder")
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment