iOS9 Swift File Creating NSFileManager.createDirectoryAtPath with NSURL - swift

Before iOS9, we had created a directory like so
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let logsPath = documentsPath.stringByAppendingPathComponent("logs")
let errorPointer = NSErrorPointer()
NSFileManager.defaultManager().createDirectoryAtPath(logsPath, withIntermediateDirectories: true, attributes: nil, error: errorPointer)
But with iOS9 they removed String.stringByAppendingPathComponent. The auto convert tool replaced our use of String with NSURL. createDirectoryAtPath() takes a string so I need to convert the NSURL to a string. We used absolutePath like so: (update for iOS9)
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let logsPath = documentsPath.URLByAppendingPathComponent("logs")
do {
try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
But I am getting the following error:
Unable to create directory Error Domain=NSCocoaErrorDomain Code=513
"You don’t have permission to save the file “logs” in the folder
“Documents”."
UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/,
NSUnderlyingError=0x15664d070 {Error Domain=NSPOSIXErrorDomain Code=1
"Operation not permitted"}}

I figured this one out. createDirectoryAtPath() is unable to process a path with the "file://" prefix. To get a path without the prefix you must use path() or relativePath().
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let logsPath = documentsPath.URLByAppendingPathComponent("logs")
do {
try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
Incorrect path (notice file://):
file:///var/mobile/Containers/Data/Application/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/
Correct path:
/var/mobile/Containers/Data/Application/F2EF2D4F-94AF-4BF2-AF9E-D0ECBC8637E7/Documents/logs/

Swift 3
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
let logsPath = documentsPath.appendingPathComponent("logs")
do {
try FileManager.default.createDirectory(at: logsPath!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}

Swift 4
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let logsPath = paths[0].appendingPathComponent("logs")
do {
try FileManager.default.createDirectory(at: logsPath, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}

Related

Write a bundle as a file

I want to write a bundle I've dragged and dropped into my project folder into a temporary file.
Previously I used fileManager.copyItem but it deleted the original.
Original code
func copyPackagedBundleToDocuments(withDestinationName dest: String) {
if let bundlePath = Bundle.main.path(forResource: embeddedBundle, ofType: nil) {
let fileManager = FileManager.default
let destPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!
// applicationSupportDirectory is not created by default in the sandbox, and therefore we need to make sure that it exists!
let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
if let applicationSupportURL = urls.last {
do {
try fileManager.createDirectory(at: applicationSupportURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
}
let fullDestPath = NSURL(fileURLWithPath: destPath + "/" + dest)
do {
try fileManager.copyItem(atPath: bundlePath, toPath: fullDestPath.path!)
} catch {
print(error)
}
}
}
This tells me the paths etc. are fine, since it does copy the file.
Now I want an atomic copy, so want to use Data(contentsOfFile)
So I rewrote everything:
func copyPackagedBundleToDocuments(withDestinationName dest: String) {
if let bundlePath = Bundle.main.path(forResource: embeddedBundle, ofType: nil) {
let fileManager = FileManager.default
let destPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!
// applicationSupportDirectory is not created by default in the sandbox, and therefore we need to make sure that it exists!
let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)
if let applicationSupportURL = urls.last {
do {
try fileManager.createDirectory(at: applicationSupportURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
}
let urlPath = URL(fileURLWithPath: destPath + "/" + dest)
let dta = try? Data(contentsOf: URL(fileURLWithPath: bundlePath) )
do {
try dta.write(to: urlPath , options: .atomic)
} catch {
print ("error \(error)")
}
}
}
However the data (dta) here is nil!
I think this is because my bundle location (as follows) ends in a backslash /:
file:///Users/user/Library/Developer/CoreSimulator/Devices/37A85B0B-F2B7-4A0C-BAA7-E05A831FFAE0/data/Containers/Bundle/Application/AD665103-E256-4F6C-8248-B62D8FF9FDC8/LocalBundle.app/Bundle.bundle/
and I guess that copyItem can treat the bundle as a single file, but writing the bundle to data is somehow not possible (the path is correct, since the code using copyItem works).
For clarity the following
if let bundleURL = Bundle.main.url(forResource: embeddedBundle, withExtension: nil) {
print ( try? Data(contentsOf: bundleURL ) )
}
prints nil to the console, validating the url for the bundle.
How can I write my bundle to a file, atomically?

Creating a directory at /Users/Shared/ - using Swift

I am using the following code to create a directory in /Users/Shared/ to share data of my application between all users. When i run the code gotthe below output.
2019-03-08 19:41:41.751418+0530 MyApp[7224:488397] Couldn't create document directory
2019-03-08 19:41:41.754026+0530 MyApp[7224:488397] Document directory is file:///Users/Appname
let fileManager = FileManager.default
if let tDocumentDirectory = fileManager.urls(for: .userDirectory, in: .localDomainMask).first {
let filePath = tDocumentDirectory.appendingPathComponent("Appname")
if !fileManager.fileExists(atPath: filePath.path) {
do {
try fileManager.createDirectory(atPath: filePath.path, withIntermediateDirectories: true, attributes: nil)
} catch {
NSLog("Couldn't create document directory")
}
}
NSLog("Document directory is \(filePath)")
}
I don't why this error occured. How this can be done?
Please read the log messages carefully.
You are trying to create the folder file:///Users/Appname which is not in /Users/Shared. You have to append "Shared/Appname".
And you are encouraged to use the URL related API of FileManager (and less confusing variable names 😉)
let fileManager = FileManager.default
let userDirectory = try! fileManager.url (for: .userDirectory, in: .localDomainMask, appropriateFor: nil, create: false)
let folderURL = userDirectory.appendingPathComponent("Shared/Appname")
if !fileManager.fileExists(atPath: folderURL.path) {
do {
try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Couldn't create document directory", error)
}
}
print("Document directory is \(folderURL)")

Swift FileManager - no such file

I am trying to upload videos. I get true for fileExists but in the logs I see -
Body file is unreachable:
/private/var/mobile/Containers/Data/Application/BE71B534-5051-4552-8491-30E1FE34E128/Documents/upload/306A4291-5E5A-467E-B2F7-8FFCA5BFFC1D1520419887-676644831.mp4
Error Domain=NSCocoaErrorDomain Code=260 "The file
“306A4291-5E5A-467E-B2F7-8FFCA5BFFC1D1520419887-676644831.mp4”
couldn’t be opened because there is no such file."
let dir = "/upload/"
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let path = documentsUrl.appendingPathComponent(dir)
do {
if let urlArray = try? FileManager.default.contentsOfDirectory(at: path,
includingPropertiesForKeys: [.contentModificationDateKey],
options:.skipsHiddenFiles) {
let mp4Files = urlArray.filter{ $0.pathExtension == "mp4" }
for videoFile in urlArray {
let fileExists = FileManager().fileExists(atPath: videoFile.path)
if fileExists {
let url = URL.init(fileURLWithPath: videoFile.path)
self.uploadVideo(url: url)
}
}
}
} catch {
print(error.localizedDescription)
}
To save the video I use -
let urlData = NSData(contentsOf: videoUrl)
if((urlData) != nil) {
DispatchQueue.main.async(execute: { () -> Void in
urlData?.write(toFile: path, atomically: true)
})
}
Try this one,
create Directory using this code
var nextItemDirUrl : URL!
func createDownloadSongDirIfNotForNextItem() {
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let logsPath = documentsDirectoryURL.appendingPathComponent("upload")
nextItemDirUrl = logsPath
do {
try FileManager.default.createDirectory(atPath: logsPath.path, withIntermediateDirectories: true, attributes: nil)
print("Directory created at:",logsPath)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
}
After save your file using nextItemDirUrl in DocumentDirectory
let destinationUrl = nextItemDirUrl.appendingPathComponent("yourView.mp4")
yourVideoData.write(to: destinationUrl, atomically: true)
I managed to fix this by doing -
let urlData = NSData(contentsOf: videoFile)
if((urlData) != nil) {
let lastPath = "/videos/" + "temp.mp4"
let path = documentDirectory.appending(lastPath)
urlData?.write(toFile: path, atomically: true)
let fileURL = URL.init(fileURLWithPath: path)
self.uploadVideo(url: url)
}

File cannot be opened - however it does exist - swift 3

I am recording video to a file with this code:
print(documentsPath)
//var/mobile/Containers/Data/Application/FFB207E7-36CC-4C53-A2E2-5FADC7C23A18/Documents/gymnastVideos/test.mp4
let filePath = NSURL(fileURLWithPath: documentsPath)
videoFileOutput.startRecording(toOutputFileURL: filePath as URL!, recordingDelegate: recordingDelegate)
and have verified that the file exists, however, when I try to open the file to check if anything is being saved there, I return an error. The code:
var documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
documentsPath.append("/gymnastVideos")
let filePath = URL(fileURLWithPath: documentsPath)
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: filePath, includingPropertiesForKeys: nil, options: [])
print(directoryContents)
print("!")
} catch let error as NSError {
print(error.localizedDescription)
//The file “gymnastVideos” couldn’t be opened.
print("!!")
}
Why can't I open the file, and what do I need to change in my code to be able to open the file?

Able to write/read file but unable to delete file SWIFT

I store an .jpg image i the iOS documents directory. I can write files and read files but when it comes to deleting them it says that there is no such file but that cannot be because I can read it with the same url.
Reading:
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let path = NSURL(fileURLWithPath: paths[0] as String)
let fullPath = path.appendingPathComponent(info["pi"] as! String)
let data = NSData(contentsOf: fullPath!)
Deleting:
let fileManager = FileManager.default
fileManager.delegate = self
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let path = NSURL(fileURLWithPath: paths[0] as String)
let fullPath = path.appendingPathComponent(info["pi"] as! String)
do {
try fileManager.removeItem(atPath: "\(fullPath!)")
} catch {
print("\(error)")
}
It throws:
Error Domain=NSCocoaErrorDomain Code=4 "“image_496251232.806566.jpg” couldn’t be removed." UserInfo={NSUnderlyingError=0x1758eb40 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}, NSFilePath=file:///var/mobile/Containers/Data/Application/269ADA58-6B09-4844-9FAA-AC2407C1D991/Documents/image_496251232.806566.jpg, NSUserStringVariant=(
Remove
)}
Your fullPath variable is a (optional) URL. To convert that to a file path string, use the .path property, not string interpolation:
fileManager.removeItem(atPath: fullPath!.path)
Or better, use the URL directly without converting it to a path:
fileManager.removeItem(at: fullPath!)
(And get rid of the forced unwrapping in favor of option binding ... :-)