Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. func copyfileToDocumentDirectory() {
  2. let fileName = "Test01"
  3. let fileType = "jpg"
  4. let copyfileName = fileName
  5. let copyfileType = fileType
  6. guard let bundlePath = Bundle.main.path(forResource: fileName, ofType: fileType),
  7. let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first,
  8. let fullDocumentPath = NSURL(fileURLWithPath: documentPath).appendingPathComponent("\(copyfileName).\(copyfileType)")
  9. else { return }
  10.  
  11. let fileManager = FileManager.default
  12. let fullDocumentPathString = fullDocumentPath.path
  13. if fileManager.fileExists(atPath: fullDocumentPathString) {
  14. print("The file exists and can not be copied.")
  15. } else {
  16. do {
  17. try fileManager.copyItem(atPath: bundlePath, toPath: fullDocumentPathString)
  18. print("Copied")
  19. } catch {
  20. print(error)
  21. }
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement