I have functions to create directorys:
func createSystemFolders(){
// Create a FileManager instance
let fileManager = FileManager.default
do {
try fileManager.createDirectory(atPath: "json", withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError {
debugPrint("\(ErrorsLabels.AppDelegate01): \(error)")
}
do {
try fileManager.createDirectory(atPath: "inspirations", withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError {
debugPrint("\(ErrorsLabels.AppDelegate02): \(error)")
}
do {
try fileManager.createDirectory(atPath: "products", withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError {
debugPrint("\(ErrorsLabels.AppDelegate03): \(error)")
}
}
I need second function to check directory exist.
Haw can I check it?
You can use this,
fileprivate func directoryExistsAtPath(_ path: String) -> Bool {
var isdirectory : ObjCBool = true
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory.boolValue
}
You can do smarter solution Like that without Two function completion(isExit,directoryURL) :
And simple use it in one line :
self.createSystemFolders("json") { (isExit, url) in
print(isExit)
print(url)
}
CreateSystemFolders:
func createSystemFolders(_ folderName:String ,_ completion:(_ isExit:Bool?,_ directoryURL:URL?) -> Void){
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let directory = paths[0]
let fileManager = FileManager.default
let url = URL(fileURLWithPath: directory).appendingPathComponent(folderName)
if !fileManager.fileExists(atPath: url.path) {
do {
try fileManager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
completion(false,url)
}
catch {
print("Error: Unable to create directory: \(error)")
completion(nil,nil)
}
var url = URL(fileURLWithPath: directory)
var values = URLResourceValues()
values.isExcludedFromBackup = true
do {
try url.setResourceValues(values)
completion(false,url)
}
catch {
print("Error: Unable to exclude directory from backup: \(error)")
completion(nil,nil)
}
}else{
completion(true,url)
}
}
Try this:
let fileManager = FileManager.default
var isdirectory = true
if fileManager.fileExists(atPath: fullPath, isDirectory:&isdirectory) {
if isdirectory.boolValue {
// file exists and is a directory
} else {
// file exists and is not a directory
}
} else {
// file does not exist
}
here how you can
1 Find a Documents directory on device
2 Check if file exists at specified file path
let fileNameToDelete = "myFileName.txt"
var filePath = ""
// Fine documents directory on device
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0 {
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
print("Local path = \(filePath)")
} else {
print("Could not find local directory to store file")
return
}
let fileManager = FileManager.default
// Check if file exists
if fileManager.fileExists(atPath: filePath) {
print("File exists")
} else {
print("File does not exist")
}
Related
I need to save a video file to a temp directory and save the reference to it URL. Currently I have tried using fileManager to create a temp directory and then createFile(atPath: tempDirString, contents: vidData, attributes: nil). But I dont/am not able to save the full reference to the file.
What I have tried:
PHCachingImageManager().requestAVAsset(forVideo: (cell?.assetPH)!, options: nil) { (avAsset, _, _) in
if let avAsset = avAsset {
print(avAsset, " the avasset?")
// vidAVAsset = avAsset
let avPlayerItem = AVPlayerItem(asset: avAsset)
let avPlayer = AVPlayer(playerItem: avPlayerItem)
print(avPlayer, "<-- good?")
let finalURL = self.urlOfCurrentlyPlayingInPlayer(player: avPlayer)
print(finalURL, "<-- finalURL YEAH EYAHY YEAH?!?!?")
// newUserTakenVideo.videoURL = finalURL
let url = self.addVidToTempURL(vidURL: finalURL!)
newUserTakenVideo.videoURL = url
// let newUserTakenVideo = SelectedMedia(videoURL: finalURL, phAsset: (cell?.assetPH)!, thumbnailImg: (cell?.imageView.image)!, uniqueCellID: indexPath)
// GlobalSharedData.shared.arrayOfCurrentCreateMedia.append(newUserTakenVideo)
} else {
print("did noot work")
}
}
This is the function called:
func addVidToTempURL(vidURL: URL) -> URL? {
let fileManager = FileManager.default
let tempDir = fileManager.temporaryDirectory
let tempDirString = tempDir.path
do {
print( "tempDir: \(tempDir)" )
print( "tempDirString: \(tempDirString)" )
if fileManager.fileExists(atPath: tempDirString ) {
print( "tempDir exists" )
do {
try fileManager.createDirectory( at: tempDir, withIntermediateDirectories: true, attributes: nil )
print( "tempDir created" )
if fileManager.fileExists(atPath: tempDirString ) {
print( "tempDir exists" )
let vidData = try Data(contentsOf: vidURL)
fileManager.createFile(atPath: tempDirString, contents: vidData, attributes: nil)
print(tempDirString, " the stringfsdsda")
// fileManager.urls(for: fileManager.temporaryDirectory, in: fileManager.)
let url = URL(string: tempDirString)
return url
} else {
print( "tempDir STILL DOES NOT exist" )
return nil
}
} catch {
print( "tempDir NOT created" )
return nil
}
} else {
print( "tempDir DOES NOT exist" )
do {
try fileManager.createDirectory( at: tempDir, withIntermediateDirectories: true, attributes: nil )
print( "tempDir created" )
if fileManager.fileExists(atPath: tempDirString ) {
print( "tempDir exists" )
let vidData = try Data(contentsOf: vidURL)
fileManager.createFile(atPath: tempDirString, contents: vidData, attributes: nil)
print(tempDirString, " the fsdfdsdfsdfsfsd")
let url = URL(string: tempDirString)
return url
} else {
print( "tempDir STILL DOES NOT exist" )
return nil
}
} catch {
print( "tempDir NOT created" )
return nil
}
}
}
}
How can I get a URL reference to this files location?
I appreciate any help and can add more information if needed. Thanks, me
Since you don't seem to want your file to be visible to users or persisted between app launches, the Temporary directory sounds perfectly fine for your use case:
var tempVideoFileUrl: URL {
return FileManager.default.temporaryDirectory.appendingPathComponent("my_video_name")
}
func storeVideoToTemporaryFolder(videoData: Data) {
guard !FileManager.default.fileExists(atPath: tempVideoFileUrl.path) else {
return
}
do {
try videoData.write(to: tempVideoFileUrl)
}
catch {
fatalError()
}
}
func loadVideoFromTemporaryFolder() -> Data? {
if let data = try? Data(contentsOf: tempVideoFileUrl) {
return data
}
return nil
}
Worth mentioning though, the system may (and most likely will) purge this directory after the app is exited. It's recommended that you remove any temporary directories/files after they're no longer needed.
So in your case, you can simply remove it once you finished uploading to Firebase Storage:
func deleteVideoFromTemporaryFolder() {
do {
try FileManager.default.removeItem(at: videoFileUrl)
}
catch {
fatalError()
}
}
If you prefer to keep your file around between app launches though, you could use Application Support directory. But since Application Support and Documents directories gets automatically backed up, you may want to exclude your file from iCloud backup by setting its URL's isExcludedFromBackupKey key:
var applicationSupportVideoFileUrl: URL {
let applicationSupportFolderUrl = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
return applicationSupportFolderUrl.appendingPathComponent("my_video_name")
}
func excludeFromCloudBackup(url: URL) {
var targetUrl = url
var isAlreadyExcludedFromBackup: Bool
do {
let storedRessourceValues = try targetUrl.resourceValues(forKeys: [URLResourceKey.isExcludedFromBackupKey])
isAlreadyExcludedFromBackup = storedRessourceValues.isExcludedFromBackup ?? false
}
catch {
fatalError()
}
guard !isAlreadyExcludedFromBackup else {
return
}
var ressourceValues = URLResourceValues()
ressourceValues.isExcludedFromBackup = true
do {
try targetUrl.setResourceValues(ressourceValues)
}
catch {
fatalError()
}
}
Edit: To get the data from your PHAsset, this should work:
import Photos
func loadVideoData(phAsset: PHAsset, completion: #escaping (Data?)->()) {
guard phAsset.mediaType == .video else {
return completion(nil)
}
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = true
options.deliveryMode = .highQualityFormat
PHCachingImageManager().requestAVAsset(forVideo: phAsset, options: options) { (avAsset, _, _) in
guard let avUrlAsset = avAsset as? AVURLAsset else {
return
}
var videoData: Data?
do {
videoData = try Data(contentsOf: avUrlAsset.url)
} catch {
fatalError()
}
DispatchQueue.main.async {
completion(videoData)
}
}
}
Then simply call this method and store your video in the Temporary folder:
loadVideoData(phAsset: yourPhAsset) { [weak self] videoData in
guard let strongSelf = self else { return }
guard let videoData = videoData else {
return
}
strongSelf.storeVideoToTemporaryFolder(videoData: videoData)
}
I have been trying to save local index.html file to document directory but it does not save to local document directory.
here is my folder structure:
sampleHtml.xcodeproj
---->sampleHtml
-------->Web_Assets
------------> index.html
---->ViewController.swift
Here is the code for saving and checking file path exist is not, if path exist then overwrite it else copy the new one.
func saveHtmlDoc(){
let filemgr = FileManager.default
let docURL = filemgr.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destPath = docURL.path+"/www/"
print("des path", destPath)
let sourcePath = Bundle.main.resourceURL!.appendingPathComponent("Web_Assets").path
print("sourc", sourcePath.appending("/index.html"))
//COPY Web_Assets content from Bundle to Documents/www
do {
try filemgr.removeItem(atPath: destPath)
} catch {
print("Error: \(error.localizedDescription)")
}
do {
try filemgr.copyItem(atPath: sourcePath + "/", toPath: destPath)
} catch {
print("Error: \(error.localizedDescription)")
}
}
My issue is how to save index.html file to document directory and check the file path exist are not, if exist overwrite it or copy the new one.
Any help much appreciates it...
I have just done this code in a test project which works.
You should ensure that you are performing checks along the way and ensure that your HTML file is in your Copy resources build phase
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
moveHtmlFile()
}
private func moveHtmlFile() {
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
guard let sourcePath = Bundle.main.path(forResource: "index", ofType: "html") else {
return
}
if fileManager.fileExists(atPath: sourcePath) {
let sourceUrl = URL(fileURLWithPath: sourcePath)
try? fileManager.createDirectory(atPath: documentsDirectory.appendingPathComponent("www").path,
withIntermediateDirectories: false,
attributes: nil)
let destination = documentsDirectory.appendingPathComponent("www/index.html", isDirectory: false)
try? fileManager.copyItem(at: sourceUrl, to: destination)
if fileManager.fileExists(atPath: destination.path) {
print("file copied")
} else {
print("file copy failed")
}
}
}
}
Result:
I try and test below code, it work okey. Before add file, you need create folder first.
func saveHtmlDoc() {
let filemgr = FileManager.default
let docURL = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let destPath = URL(string:docURL)?.appendingPathComponent("www")
guard let newDestPath = destPath, let sourcePath = Bundle.main.path(forResource: "image", ofType: ".png"), let fullDestPath = NSURL(fileURLWithPath: newDestPath.absoluteString).appendingPathComponent("image.png") else { return }
//CREATE FOLDER
if !filemgr.fileExists(atPath: newDestPath.path) {
do {
try filemgr.createDirectory(atPath: newDestPath.path, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error.localizedDescription);
}
}
else {
print("Folder is exist")
}
if filemgr.fileExists(atPath: fullDestPath.path) {
print("File is exist in \(fullDestPath.path)")
}
else {
do {
try filemgr.copyItem(atPath: sourcePath, toPath: fullDestPath.path)
} catch {
print("Error: \(error.localizedDescription)")
}
}
}
Here is my Folder Structure
I want to copy this Stickers folder to Document Directory for this purpose i have used following code
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return // Could not find documents URL
}
let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("Stickers")
if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
print("DB does not exist in documents folder")
let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("Stickers")
do {
try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path)
} catch let error as NSError {
print("Couldn't copy file to final location! Error:\(error.description)")
}
} else {
print("Database file found at path: \(finalDatabaseURL.path)")
}
but it won't work. I am getting an Error as
NOTE: If Copying folder is not possible then i want to copy Assets.xcassets to Document Directory. How can i achieve it?
Please below code..
I update your code in two functions to copy all files from folder to document directory.
Hope it will work.
func copyFolders() {
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return // Could not find documents URL
}
let finalDatabaseURL = documentsUrl.first!.appendingPathComponent("Stickers")
if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
print("DB does not exist in documents folder")
let documentsURL = Bundle.main.resourceURL?.appendingPathComponent("Stickers")
do {
if !FileManager.default.fileExists(atPath:(finalDatabaseURL?.path)!)
{
try FileManager.default.createDirectory(atPath: (finalDatabaseURL.path), withIntermediateDirectories: false, attributes: nil)
}
copyFiles(pathFromBundle: (documentsURL?.path)!, pathDestDocs: finalDatabaseURL.path)
} catch let error as NSError {
print("Couldn't copy file to final location! Error:\(error.description)")
}
} else {
print("Database file found at path: \(finalDatabaseURL.path)")
}
}
func copyFiles(pathFromBundle : String, pathDestDocs: String) {
let fileManagerIs = FileManager.default
do {
let filelist = try fileManagerIs.contentsOfDirectory(atPath: pathFromBundle)
try? fileManagerIs.copyItem(atPath: pathFromBundle, toPath: pathDestDocs)
for filename in filelist {
try? fileManagerIs.copyItem(atPath: "\(pathFromBundle)/\(filename)", toPath: "\(pathDestDocs)/\(filename)")
}
} catch {
print("\nError\n")
}
}
Currently I'm trying to create files during a unit test to see if I can retrieve the data after its created but the fileManager.createFileAtPath always fails. I looked at this post: fileManager.createFileAtPath always fails I implemented the solutions to that answer but it still fails. This is what the unit test and accompanying function looks like:
internal func getCacheFolderPath() -> String {
var folderPath = ""
guard let path = self.urls(for:.cachesDirectory , in: .userDomainMask).last?.path else {
return folderPath
}
folderPath = path + "/NetworkCache"
if !self.fileExists(atPath: folderPath) {
do {
try self.createDirectory(atPath: folderPath, withIntermediateDirectories: false, attributes: nil)
} catch {
print(error.localizedDescription)
}
}
return folderPath
}
func test_createFile(){
let path = sut?.getCacheFolderPath()
let fileManager = FileManager.default
if !FileManager.default.fileExists(atPath: path!) {
do {
try FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}
let isFileCreated = fileManager.createFile(atPath: path!, contents: mockData, attributes: nil)
print(isFileCreated)//This always returns false
}
Using the data.write(to:) instead of create file helped determine what the issue was. It looks like I was creating a folder as a file path and trying to save a file to that path. Since it was a file and not a folder it was returning that something already exists there.
Here is what the completed working code looks like:
func saveData(data: Data, forFileName fileName: String, completionHandler: #escaping (URL?)->()) {
let url = URL(fileURLWithPath: self.filePathForFileName(name: fileName))
print("saveData:\(url)")
do {
// [data writeToFile:[self thdncFilePathForFileName:name] atomically:YES];
try data.write(to: url, options: .atomic)
completionHandler(url)
} catch {
print(error.localizedDescription)
completionHandler(nil)
}
}
func test_createFile(){
var isSaved: Bool = false
let path = sut?.getCacheFolderPath()
let promise = expectation(description: "Completion Hander invoked")
if !FileManager.default.fileExists(atPath: path!)
{
do {
try FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}
sut?.saveData(data: mockData!, forFileName: filePath, completionHandler: { (url) in
let fileManager = MockFileManager.default
if fileManager.fileExists(atPath: self.sut!.filePathForFileName(name: self.filePath)) {
isSaved = true
print("FILE AVAILABLE")
} else {
isSaved = false
print("FILE NOT AVAILABLE")
}
promise.fulfill()
})
wait(for: [promise], timeout: 10, enforceOrder: true)
XCTAssertTrue(isSaved)
}
I created this FileManager extension. With this extension, I want to create a file hierarchy like so:
Application Support
Favorites
Feed
Images
This is the code I have in FileManager extension which I would call in app delegate as soon as the app launches. Then I would use this code to always retrieve the path's of the folders.
Is this a good way to create this hierarchy and retrieve the paths when I need them? Is this good practice?
extension FileManager {
static func createOrFindApplicationDirectory() -> URL? {
let bundleID = Bundle.main.bundleIdentifier
// Find the application support directory in the home directory.
let appSupportDir = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
guard appSupportDir.count > 0 else {
return nil
}
// Append the bundle ID to the URL for the Application Support directory.
let dirPath = appSupportDir[0].appendingPathComponent(bundleID!)
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating Application Support directory with error: \(error)")
return nil
}
}
static func createOrFindFavoritesDirectory() -> URL? {
guard let appSupportDir = createOrFindApplicationDirectory() else {
return nil
}
let dirPath = appSupportDir.appendingPathComponent("Favorites")
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating Favorites directory with error: \(error)")
return nil
}
}
static func createOrFindFeedDirectory() -> URL? {
guard let appSupportDir = createOrFindFavoritesDirectory() else {
return nil
}
let dirPath = appSupportDir.appendingPathComponent("Feed")
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating Favorites directory with error: \(error)")
return nil
}
}
static func currentImagesDirectory() -> URL? {
guard let feedDir = createOrFindFeedDirectory() else {
return nil
}
let dirPath = feedDir.appendingPathComponent("Images")
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating Images directory with error: \(error)")
return nil
}
}
}
It looks pretty good, but you could combine a bit of the code and have better error checking:
extension FileManager {
static func createOrFindApplicationDirectory() -> URL? {
guard let bundleID = Bundle.main.bundleIdentifier else {
return nil
}
// Find the application support directory in the home directory.
let appSupportDirArray = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
guard let appSupportDir = appSupportDirArray.first else {
return nil
}
// Append the bundle ID to the URL for the Application Support directory.
let dirPath = appSupportDir.appendingPathComponent(bundleID)
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating Application Support directory with error: \(error)")
return nil
}
}
static func createOrFindDirectory(named name: String) -> URL? {
guard let appSupportDir = createOrFindApplicationDirectory() else {
return nil
}
let dirPath = appSupportDir.appendingPathComponent(name)
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating \(name) directory with error: \(error)")
return nil
}
}
static func currentImagesDirectory() -> URL? {
guard let feedDir = createOrFindDirectory(named: "Feed") else {
return nil
}
let dirPath = feedDir.appendingPathComponent("Images")
// If the directory does not exist, this method creates it.
do {
try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil)
return dirPath
} catch let error {
print("Error creating Images directory with error: \(error)")
return nil
}
}
}