Get document directory, temp directory, and cache directory path iOS Swift 5.1

iOS provides the document directory, temp directory, and cache directory folder to every application so that we can save our files inside these folders. You can save any kind of files inside these folders. Use the below code to get the document directory, temp directory, and cache.
extension UIViewController {
  
  // Get user's documents directory path
  func getDocumentDirectoryPath() -> URL {
    let arrayPaths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let docDirectoryPath = arrayPaths[0]
    return docDirectoryPath
  }
  
  // Get user's cache directory path
  func getCacheDirectoryPath() -> URL {
    let arrayPaths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
    let cacheDirectoryPath = arrayPaths[0]
    return cacheDirectoryPath
  }
  
  // Get user's temp directory path
  func getTempDirectoryPath() -> URL {
    let tempDirectoryPath = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
    return tempDirectoryPath
  }
  
}

Read our next article: Max character limit of UITextField and allowed characters Swift

Leave a Reply

Your email address will not be published. Required fields are marked *