Skip to content

ESKARIA/ESFileManager

Repository files navigation

ESFileManager

A simple library for use iOS File manager

Install

Swift Package Manager

Add ESFileManager git url to you project and it install automatically

Cocoapods

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

Usage

Standart use

  1. Import in project
import ESFileManager
  1. 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()
  1. 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!
    }
}
  1. 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!
    }
}
  1. 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!
    }
}
  1. You can geet list of files in your directory with list func