File Operations Preview
A library that helps developers to easily perform file-related operations. In iOS,
We write our files mainly into three directories Documents Directory, Temporary Directory, Cache Directory
Requirements
File Operations Version | 0.0.15 |
---|---|
iOS Version | 10.0+ |
Xcode | 10+ |
Swift | 4.2 |
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamofire into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'FileOperations'
Directory Types
DirectoryPath defines three directories which commonly used in our iOS application
-
Documents Directory
-
Temporary Directory
-
Cache Directory
URL Methods
Get Document Directory URL
let documentDirectoryURL = FileOperations.getDocumentDirectoryURL()
Get Temporary Directory URL
let temporaryDirectoryURL = FileOperations.getTemporaryDirectoryURL()
Get Cache Directory URL
let cacheDirectoryURL = FileOperations.getCacheDirectoryURL()
Directory Methods
Delete all contents of Directory Path
try? FileOperations.clearDirectory(path: .document)
Create directory in document/temp/cache directory with given file name. Method returns path of the directory created
let path = try? FileOperations.createDirectory(in: .document, direcotryName: "Image")
Create directory form given URL path. Method returns path of the directory created
let documentDirectoryURL = FileOperations.getDocumentDirectoryURL().appendingPathComponent("Images")
let path = try? FileOperations.createDirectory(with: documentDirectoryURL)
Remove directory from document/temp/cache directory with given file name
try? FileOperations.removeDirectory(by: .document, with: "Images")
Remove directory with provided URL
let documentDirectoryURL = FileOperations.getDocumentDirectoryURL().appendingPathComponent("test")
try? FileOperations.removeDirectory(with directoryURL:documentDirectoryURL)
Bundle Related Methods
Readt text file from bundle
let fileContent = try? FileOperations.getText(form: Bundle.main, fileName: "sample")
Text File Operations
Create text file with file name and directory path
let filePath = try? FileOperations.createTextFile(in: .document, fileName: "About", content: "Sample Text")
Create text file form path
let filePath = try? FileOperations.createTextFile(with: path, content: "Sample Text")
Delete file with specified path
let filePath = try? FileOperations.createTextFile(with: path, content: "Sample Text")