Return file if exists, otherwise return false - swift

I'm trying to make a String extension that searches for a file in my app's directory and either returns that file or returns false if it does not exist. Here's what I have:
extension String {
func doesFileWithNameExist() -> Bool {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.appendingPathComponent(self+".png")?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath!) {
return true
} else {
return false
}
}
}
Right now my function just returns a Bool, but I'm wondering if there's a way to just return the file if it exists, otherwise return false. Is there a way to return different value types from a function?

It's highly recommended to use the URL related API. This returns an optional UIImage:
extension String {
func doesFileWithNameExist() -> UIImage? { // maybe better pngImageInDocumentsFolder()
let fileManager = FileManager.default
do {
let url = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fullURL = url.appendingPathComponent(self).appendingPathExtension("png")
_ = try fullURL.checkResourceIsReachable()
let data = try Data(contentsOf: fullURL)
return UIImage(data: data)
} catch {
return nil
}
}
}
or using Leo's great reduction (slightly still reduced):
var image: UIImage? {
guard let url = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(self).appendingPathExtension("‌​png"),
let data = try? Data(contentsOf: url) else { return nil }
return UIImage(data: data)
}

You can return an optional value here, which in Swift is "a thing or nil" – very close to "that file or false." For example, here's a quick tweak to your extension function that returns the path if a file exists there, or nil otherwise:
extension String {
func doesFileWithNameExist() -> String? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.appendingPathComponent(self+".png")?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath!) {
return filePath
} else {
return nil
}
}
}
A quick editorial comment, though: this kind of behavior doesn't seem especially well-suited to a String extension. I'd consider writing an extension on FileManager instead, passing in a string for the file's name. Given the hardcoding of the "png" extension, maybe something with the following signature?
extension FileManager {
func pathToExistingPNGFile(named name: String) -> String? {
// …
}
}

Related

Returning contents of file as Data in Swift

For the first time, I am trying to return a data file from documents. I am getting an error: No exact matches in call to initializer
Here's my code to retrieve the data file:
func returnGeoJsonFromDocumentsDirectory(eventId: String) -> Data{
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let filePath = paths.appendingPathComponent("\(eventId).json")
let geoJsonFile = Data(contentsOfFile: filePath)
if geoJsonFile != nil {
return geoJsonFile!
}
else {
print("Unable to return json")
let emptyData = Data()
return emptyData
}
If I use NSData, this seems to solve the issue but I was under the impression you shouldn't NSData in Swift.
You are mixing up NS... with native Swift and String with URL related APIs.
This is a pure Swift version preferring the URL related API
func returnGeoJsonFromDocumentsDirectory(eventId: String) -> Data {
let documentsFolderURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documentsFolderURL.appendingPathComponent(eventId).appendingPathExtension("json")
do {
return try Data(contentsOf: fileURL) {
} catch {
print("Unable to return json", error)
return Data()
}
}
Or hand over the error to the caller
func returnGeoJsonFromDocumentsDirectory(eventId: String) throws -> Data {
let documentsFolderURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documentsFolderURL.appendingPathComponent(eventId).appendingPathExtension("json")
return try Data(contentsOf: fileURL)
}
I figured it out. This is for anyone else who has issues with retrieving Data.
func returnGeoJsonFromDocumentsDirectory(eventId: String) -> Data{
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: path)
let pathComponent = url.appendingPathComponent("\(eventId).json")
do {
let geoJsonFile = try Data(contentsOf: pathComponent)
print("Succesfully retrieved json file")
return geoJsonFile
}
catch {
print("Failed to get json file = \(error.localizedDescription)")
}
let emptyData = Data()
return emptyData
}

FileManager doesn't have permission to access Music directory [duplicate]

This question already has answers here:
NSFileManager.defaultManager().fileExistsAtPath returns false instead of true
(2 answers)
Closed 8 months ago.
I want to create one folder in the fileManager root path, but before creating it, I want to check that the folder exist or not, and if not, I will create, otherwise will leave it
here are the function that I use
public func isDirectoryExist(path: String) -> Bool {
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: path, isDirectory:&isDir) {
if isDir.boolValue {
return true
} else {
return false
}
} else {
return false
}
}
public func createNewDirectory(name: String) {
let DocumentDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let DirPath = DocumentDirectory.appendingPathComponent(name)
do
{
try FileManager.default.createDirectory(atPath: DirPath!.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
Logger.logError("Unable to create directory \(error.debugDescription)")
}
Logger.logInfo("Dir Path = \(DirPath!)")
}
Now, when I check the folder existing, it's always false and create a new folder and it happen every time
func createARObjectDirectory() {
let rootURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
if isDirectoryExist(path: "\(rootURL.absoluteString)\(DefaultURL.arObjectUrlDirectoryName)") {
Logger.logServer("ARObject directly found")
} else {
createNewDirectory(name: DefaultURL.arObjectUrlDirectoryName)
}
}
Then I print the root url, and seems the hash in the middle of url is always different, how I can check it?
file:///var/mobile/Containers/Data/Application/5AD0690B-498D-4309-8BD0-191FB88766AC/Documents/AR-Object/
file:///var/mobile/Containers/Data/Application/41D35A54-1807-417E-AE29-311D43FCC21D/Documents/AR-Object/
file:///var/mobile/Containers/Data/Application/F7E385CC-7921-4C37-B9BF-BCEFFC2AEE9E/Documents/AR-Object/
file:///var/mobile/Containers/Data/Application/4748B014-5E55-46BB-BC83-394A6BC27292/Documents/AR-Object/
Thanks for your help
You are using the wrong API. absoluteString (in rootURL.absoluteString) returns the string representation including the scheme file://. The correct API for file system URLs is path
I recommend to use the URL related API as much as possible
public func directoryExists(at url: URL) -> Bool {
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: url.path, isDirectory:&isDir) {
return isDir.boolValue
} else {
return false
}
}
and compose the URL in a more reliable way
func createARObjectDirectory() {
let rootURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
if directoryExists(at: rootURL.appendingPathComponent(DefaultURL.arObjectUrlDirectoryName) {
Logger.logServer("ARObject directly found")
} else {
createNewDirectory(name: DefaultURL.arObjectUrlDirectoryName)
}
}
And this is swiftier too
public func createNewDirectory(name: String) {
let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let dirURL = documentDirectory.appendingPathComponent(name)
do
{
try FileManager.default.createDirectory(at: dirURL, withIntermediateDirectories: false, attributes: nil)
}
catch let error as NSError
{
Logger.logError("Unable to create directory \(error.debugDescription)")
}
Logger.logInfo("Dir Path = \(dirPath.path)")
}
The actual path to your app's documents directory is subject to change. You should use relative paths from the documents directory, and not try to compare paths between runs.
(I believe the path changes each time you rebuild your app or reinstall it, but is {fairly} stable for app store builds.)
Are you saying that the directory you create inside the documents directory goes away between runs? That should not be true. The contents of the documents directory should persist.

Different path URL for FileManager everytime I open the app [duplicate]

This question already has answers here:
NSFileManager.defaultManager().fileExistsAtPath returns false instead of true
(2 answers)
Closed 8 months ago.
I want to create one folder in the fileManager root path, but before creating it, I want to check that the folder exist or not, and if not, I will create, otherwise will leave it
here are the function that I use
public func isDirectoryExist(path: String) -> Bool {
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: path, isDirectory:&isDir) {
if isDir.boolValue {
return true
} else {
return false
}
} else {
return false
}
}
public func createNewDirectory(name: String) {
let DocumentDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let DirPath = DocumentDirectory.appendingPathComponent(name)
do
{
try FileManager.default.createDirectory(atPath: DirPath!.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
Logger.logError("Unable to create directory \(error.debugDescription)")
}
Logger.logInfo("Dir Path = \(DirPath!)")
}
Now, when I check the folder existing, it's always false and create a new folder and it happen every time
func createARObjectDirectory() {
let rootURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
if isDirectoryExist(path: "\(rootURL.absoluteString)\(DefaultURL.arObjectUrlDirectoryName)") {
Logger.logServer("ARObject directly found")
} else {
createNewDirectory(name: DefaultURL.arObjectUrlDirectoryName)
}
}
Then I print the root url, and seems the hash in the middle of url is always different, how I can check it?
file:///var/mobile/Containers/Data/Application/5AD0690B-498D-4309-8BD0-191FB88766AC/Documents/AR-Object/
file:///var/mobile/Containers/Data/Application/41D35A54-1807-417E-AE29-311D43FCC21D/Documents/AR-Object/
file:///var/mobile/Containers/Data/Application/F7E385CC-7921-4C37-B9BF-BCEFFC2AEE9E/Documents/AR-Object/
file:///var/mobile/Containers/Data/Application/4748B014-5E55-46BB-BC83-394A6BC27292/Documents/AR-Object/
Thanks for your help
You are using the wrong API. absoluteString (in rootURL.absoluteString) returns the string representation including the scheme file://. The correct API for file system URLs is path
I recommend to use the URL related API as much as possible
public func directoryExists(at url: URL) -> Bool {
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: url.path, isDirectory:&isDir) {
return isDir.boolValue
} else {
return false
}
}
and compose the URL in a more reliable way
func createARObjectDirectory() {
let rootURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
if directoryExists(at: rootURL.appendingPathComponent(DefaultURL.arObjectUrlDirectoryName) {
Logger.logServer("ARObject directly found")
} else {
createNewDirectory(name: DefaultURL.arObjectUrlDirectoryName)
}
}
And this is swiftier too
public func createNewDirectory(name: String) {
let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let dirURL = documentDirectory.appendingPathComponent(name)
do
{
try FileManager.default.createDirectory(at: dirURL, withIntermediateDirectories: false, attributes: nil)
}
catch let error as NSError
{
Logger.logError("Unable to create directory \(error.debugDescription)")
}
Logger.logInfo("Dir Path = \(dirPath.path)")
}
The actual path to your app's documents directory is subject to change. You should use relative paths from the documents directory, and not try to compare paths between runs.
(I believe the path changes each time you rebuild your app or reinstall it, but is {fairly} stable for app store builds.)
Are you saying that the directory you create inside the documents directory goes away between runs? That should not be true. The contents of the documents directory should persist.

Want to show user a message when no data available in my document directory in swift

I want to check the pdf file with a perticular user name is available in my document directory or not. By using following code i am able to do this. But when there is no data is avalable in the document directory i want to show user a message. But i am unable to show user any message. How do do this? can anyone help me?
private func checkPatientPdfIsPresentOrNot(selectedPatient: String, completion: (_ present: Bool) -> Void){
if let documentsPathString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
let filemanager = FileManager.default
if let files = filemanager.enumerator(atPath: documentsPathString){
while let file = files.nextObject() {
let nameWithDate = (file as! String).components(separatedBy: ".")
let fileName = nameWithDate[0]
let namewithoutDate = fileName.components(separatedBy: "_")
let name = namewithoutDate[0]
if name == selectedPatient.capitalized{
completion(true)
}
else{
completion(false)
}
}
}
else{
completion(false)
}
}
}
First of all there are – in terms of Swift – a lot of outdated APIs in your code.
Second of all as the entire code is synchronous the completion handler is pointless.
The major issue is that the completion handler is called multiple times. You should call it once passing true if the partial file name matches selectedPatient or passing false after the loop.
This is a suggestion with more contemporary code and a boolean return value.
Show the message to the user if method returns false
private func checkPatientPdfIsPresentOrNot(selectedPatient: String) -> Bool {
let fileManager = FileManager.default
do {
let documentsURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: [.nameKey], options: .skipsHiddenFiles)
return fileURLs.first(where: { url -> Bool in
let fileName = url.deletingPathExtension().lastPathComponent
return fileName.components(separatedBy: "_").first == selectedPatient.capitalized
}) != nil
} catch { print(error); return false }
}

Swift 3: Reading content of folder? [duplicate]

What is wrong with my code for getting the filenames in the document folder?
func listFilesFromDocumentsFolder() -> [NSString]?{
var theError = NSErrorPointer()
let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if dirs != nil {
let dir = dirs![0] as NSString
let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir, error: theError) as [NSString]
return fileList
}else{
return nil
}
}
I thought I read the documents correctly and I am very sure about what is in the documents folder, but "fileList" does not show anything? "dir" shows the path to the folder.
Swift 5
do {
// Get the document directory url
let documentDirectory = try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
print("documentDirectory", documentDirectory.path)
// Get the directory contents urls (including subfolders urls)
let directoryContents = try FileManager.default.contentsOfDirectory(
at: documentDirectory,
includingPropertiesForKeys: nil
)
print("directoryContents:", directoryContents.map { $0.localizedName ?? $0.lastPathComponent })
for url in directoryContents {
print(url.localizedName ?? url.lastPathComponent)
}
// if you would like to hide the file extension
for var url in directoryContents {
url.hasHiddenExtension = true
}
for url in directoryContents {
print(url.localizedName ?? url.lastPathComponent)
}
// if you want to get all mp3 files located at the documents directory:
let mp3s = directoryContents.filter(\.isMP3).map { $0.localizedName ?? $0.lastPathComponent }
print("mp3s:", mp3s)
} catch {
print(error)
}
You would need to add those extensions to your project
extension URL {
var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
var isMP3: Bool { typeIdentifier == "public.mp3" }
var localizedName: String? { (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName }
var hasHiddenExtension: Bool {
get { (try? resourceValues(forKeys: [.hasHiddenExtensionKey]))?.hasHiddenExtension == true }
set {
var resourceValues = URLResourceValues()
resourceValues.hasHiddenExtension = newValue
try? setResourceValues(resourceValues)
}
}
}
This solution works with Swift 4 (Xcode 9.2) and also with Swift 5 (Xcode 10.2.1+):
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
// process files
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
Here's a reusable FileManager extension that also lets you skip or include hidden files in the results:
import Foundation
extension FileManager {
func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL]? {
let documentsURL = urls(for: directory, in: .userDomainMask)[0]
let fileURLs = try? contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: skipsHiddenFiles ? .skipsHiddenFiles : [] )
return fileURLs
}
}
// Usage
print(FileManager.default.urls(for: .documentDirectory) ?? "none")
A shorter syntax for SWIFT 3
func listFilesFromDocumentsFolder() -> [String]?
{
let fileMngr = FileManager.default;
// Full path to documents directory
let docs = fileMngr.urls(for: .documentDirectory, in: .userDomainMask)[0].path
// List all contents of directory and return as [String] OR nil if failed
return try? fileMngr.contentsOfDirectory(atPath:docs)
}
Usage example:
override func viewDidLoad()
{
print(listFilesFromDocumentsFolder())
}
Tested on xCode 8.2.3 for iPhone 7 with iOS 10.2 & iPad with iOS 9.3
Apple states about NSSearchPathForDirectoriesInDomains(_:_:_:):
You should consider using the FileManager methods urls(for:in:) and url(for:in:appropriateFor:create:) which return URLs, which are the preferred format.
With Swift 5, FileManager has a method called contentsOfDirectory(at:includingPropertiesForKeys:options:). contentsOfDirectory(at:includingPropertiesForKeys:options:) has the following declaration:
Performs a shallow search of the specified directory and returns URLs for the contained items.
func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options mask: FileManager.DirectoryEnumerationOptions = []) throws -> [URL]
Therefore, in order to retrieve the urls of the files contained in documents directory, you can use the following code snippet that uses FileManager's urls(for:in:) and contentsOfDirectory(at:includingPropertiesForKeys:options:) methods:
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])
// Print the urls of the files contained in the documents directory
print(directoryContents)
} catch {
print("Could not search for urls of files in documents directory: \(error)")
}
As an example, the UIViewController implementation below shows how to save a file from app bundle to documents directory and how to get the urls of the files saved in documents directory:
import UIKit
class ViewController: UIViewController {
#IBAction func copyFile(_ sender: UIButton) {
// Get file url
guard let fileUrl = Bundle.main.url(forResource: "Movie", withExtension: "mov") else { return }
// Create a destination url in document directory for file
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let documentDirectoryFileUrl = documentsDirectory.appendingPathComponent("Movie.mov")
// Copy file to document directory
if !FileManager.default.fileExists(atPath: documentDirectoryFileUrl.path) {
do {
try FileManager.default.copyItem(at: fileUrl, to: documentDirectoryFileUrl)
print("Copy item succeeded")
} catch {
print("Could not copy file: \(error)")
}
}
}
#IBAction func displayUrls(_ sender: UIButton) {
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])
// Print the urls of the files contained in the documents directory
print(directoryContents) // may print [] or [file:///private/var/mobile/Containers/Data/Application/.../Documents/Movie.mov]
} catch {
print("Could not search for urls of files in documents directory: \(error)")
}
}
}
Simple and dynamic solution (Swift 5):
extension FileManager {
class func directoryUrl() -> URL? {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths.first
}
class func allRecordedData() -> [URL]? {
if let documentsUrl = FileManager.directoryUrl() {
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil)
return directoryContents.filter{ $0.pathExtension == "m4a" }
} catch {
return nil
}
}
return nil
}}
This code prints out all the directories and files in my documents directory:
Some modification of your function:
func listFilesFromDocumentsFolder() -> [String]
{
let dirs = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs != [] {
let dir = dirs[0]
let fileList = try! FileManager.default.contentsOfDirectory(atPath: dir)
return fileList
}else{
let fileList = [""]
return fileList
}
}
Which gets called by:
let fileManager:FileManager = FileManager.default
let fileList = listFilesFromDocumentsFolder()
let count = fileList.count
for i in 0..<count
{
if fileManager.fileExists(atPath: fileList[i]) != true
{
print("File is \(fileList[i])")
}
}
Swift 2.0 Compability
func listWithFilter () {
let fileManager = NSFileManager.defaultManager()
// We need just to get the documents folder url
let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
do {
// if you want to filter the directory contents you can do like this:
if let directoryUrls = try? NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsSubdirectoryDescendants) {
print(directoryUrls)
........
}
}
}
OR
func listFiles() -> [String] {
var theError = NSErrorPointer()
let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
if dirs != nil {
let dir = dirs![0]
do {
let fileList = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir)
return fileList as [String]
}catch {
}
}else{
let fileList = [""]
return fileList
}
let fileList = [""]
return fileList
}