A simple library for use iOS File manager
Add ESFileManager git url to you project and it install automatically
Just add ESFileManager to your podfile like this
pod 'ESPFileManager'
Why it called esPfilemanager? Cause one guy create lib with name ESFIleManager before i do
- Import in project
import ESFileManager
- Create property of ESFileManager. In argument used default way for write/read data, but you can use init with default value "Document folder without iCloud sync". You can read about this folders and when use kind of this
private var fileManager: ESFileManagerProtocol = ESFileManager()
- You can read data with read func. For read and write used model of file which contain model with name and extension of file
private func readFile() {
// create name and extension of file to read
let file = ESFileNameModel(name: "SwiftDoc", fileExtension: .txt)
//read this file. At = nil cause we use default directory
fileManager.read(fileStorage: file, at: nil) { (file, error) in
//fetch error
if let error = error {
//fetch error
return
}
//fetch empty state of file
guard let data = file?.data else {
// file is empty!
return
}
//show our file
let stringFile = String(data: data, encoding: .utf8)
//You successful read the file!
}
}
- You can write your file on disk with write func
private func writeFile() {
// create data to write
let data = "Swift is amazing!".data(using: .utf8)
let fileName = ESFileNameModel(name: "SwiftDoc", fileExtension: .txt)
let file = ESFileModel(data: data, name: fileName)
// write data. At = nil cause we use default directory from prepareVC method
fileManager.write(file: file, at: nil) { (error) in
if let error = error {
//error
return
}
//You successful recorded the file!
}
}
- For remove file you remove func
private func removeFile() {
// create name and extension of file to remove
let file = ESFileNameModel(name: "SwiftDoc", fileExtension: .txt)
//remove this file. At = nil cause we use default directory
fileManager.remove(file: file, at: nil) { (error) in
//fetch error
if let error = error {
// fetch error
return
}
//You successful remove this file!
}
}
- You can geet list of files in your directory with list func