Write a file on iOS - ios

How do I write a file on iOS? I'm trying to do it with the following code but I'm doing something wrong:
char *saves = "abcd";
NSData *data = [[NSData alloc] initWithBytes:saves length:4];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"MyFile"];
[data writeToFile:appFile atomically:YES];
I have created MyFile.txt on resources.

May be this is useful to you.
//Method writes a string to a text file
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"One\nTwo\nThree\nFour\nFive";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSUTF8StringEncoding
error:nil];
}
//Method retrieves content from documents directory and
//displays it in an alert
-(void) displayContent{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
//use simple alert from my library (see previous post for details)
[ASFunctions alert:content];
[content release];
}

Your code is working at my end, i have just tested it. Where are you checking your changes? Use Documents directory path. To get path -
NSLog(#"%#",documentsDirectory);
and copy path from console and then open finder and press Cmd+shift+g and paste path here and then open your file

Swift
func saveFile() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as! String
let fileName = "\(documentsDirectory)/textFile.txt"
let content = "Hello World"
content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
}
func loadFile() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as! String
let fileName = "\(documentsDirectory)/textFile.txt"
let content: String = String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding, error: nil)!
println(content)
}
Swift 2
func saveFile() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content = "Hello World"
do{
try content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding)
}catch _ {
}
}
func loadFile()->String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content: String
do{
content = try String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding)
}catch _{
content=""
}
return content;
}
Swift 3
func saveFile() {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content = "Hello World"
do{
try content.write(toFile: fileName, atomically: false, encoding: String.Encoding.utf8)
}catch _ {
}
}
func loadFile()->String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content: String
do{
content = try String(contentsOfFile: fileName, encoding: String.Encoding.utf8)
} catch _{
content=""
}
return content;
}

Try making
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"MyFile"];
as
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"MyFile.txt"];

Related

Error Cannot subscript a value of type 'Dictionary<NSObject, AnyObject>' with an index of type 'String'

Can someone help me? I am just reprogramming my small project from Objective-C to Swift and get the following error:
Cannot subscript a value of type 'Dictionary' with an index of type 'String'
eins, zwei, drei,and vier are strings that get their data from another view controller.
Objective-C Code:
#property (strong, nonatomic) IBOutlet UISwitch *bluetoothSwitch;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *SwitchState;
NSArray *myStrings = [[NSArray alloc] initWithObjects:eins,zwei,drei, vier, nil];
SwitchState = [myStrings componentsJoinedByString:mad:" | | "];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:mad:"store.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:mad:"store.plist"] ];
}
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if ([[savedValue objectForKey:SwitchState] boolValue]) {
self.bluetoothSwitch.on = [[savedValue objectForKey:SwitchState] boolValue];
} else {
}
}
My Swift Code:
var eins = "Test"
var zwei = "Text"
var drei = "Test"
var vier = "Text"
var SwitchState = ""
let myStrings = [eins, zwei, drei, vier]
SwitchState = myStrings.joined(separator: " | | ")
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
var path = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("store.plist").absoluteString
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: path) {
path = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("store.plist").absoluteString
}
var savedValue = NSDictionary(contentsOfFile: path) as Dictionary?
print("Das bekommt viewdid: \(SwitchState)")
if (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false {
bluetoothSwitch.isOn = (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false
} else {
print("nein")
}
This two Lines "produce" the Errors
if (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false {
bluetoothSwitch.isOn = (savedValue?[SwitchState] as? NSNumber)?.boolValue ?? false
Please don't try to translate Objective-C code literally to Swift. There are many, many bad practices from the Swift perspective.
Basically don't use NS... classes in Swift if there is a native counterpart.
The error occurs because the result of a bridge cast from NSDictionary to Dictionary (without specifying the Swift types) is Dictionary<NSObject, AnyObject> which is not compatible with value type String
This is a real Swift version:
let eins = "Test"
let zwei = "Text"
let drei = "Test"
let vier = "Text"
let myStrings = [eins, zwei, drei, vier]
let switchState = myStrings.joined(separator: " | | ")
let fileManager = FileManager.default
do {
let applicationSupportDirectory = try fileManager.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let storeURL = applicationSupportDirectory.appendingPathComponent("store.plist")
let data = try Data(contentsOf: storeURL)
print("Das bekommt viewdid: \(switchState)")
if let savedDictionary = try PropertyListSerialization.propertyList(from: data, format: nil) as? [String:Any],
let savedState = savedDictionary[switchState] as? Bool {
bluetoothSwitch.isOn = savedState
} else {
bluetoothSwitch.isOn = false
print("nein")
}
} catch { print(error) }
The fileExists(atPath check is redundant so I left it out.

Copy Sqlite DataBase in Swift does not work properly

I am using following code for objective c to copy the sqlite database and it works fine. But when I convert this code to swift it shows error on Bool type.
Here is objective c code
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"database.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"database.sqlite"];
}
Here is the CopyDataBase for Swift which is causing issue.
var fileManager = FileManager.default
var error: Error!
var dbPath = self.getDBPath()
var success = fileManager.fileExists(atPath: dbPath)
if !success {
var defaultDBPath = URL(fileURLWithPath: Bundle.main.resourcePath!).appendingPathComponent("CapalinoDataBase.sqlite").absoluteString
do {
success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath)
}
catch {
}
if !success {
assert(false, "Failed to create writable database file with message '\(error.localizedDescription)'.")
}
}
Please try this one.
func copyDatabse() {
let fileMgr = FileManager.default
if let path = Bundle.main.path(forResource: "db", ofType:"sqlite") {
do {
try fileMgr.copyItem(atPath: path, toPath: dbPath())
print("Copy success")
}
catch {
print(error.localizedDescription )
}
}
}
func dbPath() -> String {
let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)
let docsDir = dirPaths[0]
let destPath = (docsDir as NSString).appendingPathComponent("/db.sqlite")
return destPath
}
Best way to use SQLIte using single ton class in swift.
Download example
func methodToCreateDatabase() -> NSURL? {
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
if let documentDirectory:NSURL = urls.first { // No use of as? NSURL because let urls returns array of NSURL
// exclude cloud backup
do {
try documentDirectory.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
} catch _{
print("Failed to exclude backup")
}
// This is where the database should be in the documents directory
let finalDatabaseURL = documentDirectory.URLByAppendingPathComponent("contact.db")
if finalDatabaseURL.checkResourceIsReachableAndReturnError(nil) {
// The file already exists, so just return the URL
return finalDatabaseURL
} else {
// Copy the initial file from the application bundle to the documents directory
if let bundleURL = NSBundle.mainBundle().URLForResource("contact", withExtension: "db") {
do {
try fileManager.copyItemAtURL(bundleURL, toURL: finalDatabaseURL)
} catch _ {
print("Couldn't copy file to final location!")
}
} else {
print("Couldn't find initial database in the bundle!")
}
}
} else {
print("Couldn't get documents directory!")
}
return nil
}
Please try this one it is working on swift 3.0
func copyDatabaseIfNeeded() {
//Using NSFileManager we can perform many file system operations.
let fileManager = FileManager.default
let error: Error?
let dbPath: String = self.getDBPath()
var success: Bool = fileManager.fileExists(atPath: dbPath)
if !success {
let defaultDBPath: String = URL(fileURLWithPath: (Bundle.main.resourcePath)!).appendingPathComponent("database.sqlite").absoluteString
do {
success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath) as Any as! Bool
}
catch let error as NSError {
print("Ooops! Something went wrong: \(error)")
}
if !success {
assert(false, "Failed to create writable database file with message '\(error?.localizedDescription)'.")
}
}
}
func getDBPath() -> String {
let paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDir: String = paths[0] as! String
return URL(fileURLWithPath: documentsDir).appendingPathComponent("database.sqlite").absoluteString
}

writing a file in swift

this is my code in appDelegate didFinishLaunchingWithOptions()
var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory = paths.objectAtIndex(0)
var fileName: String = String(format: "Logger.txt")
var logFilePath : NSString = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)
the error am getting is cannot convert NSString to UnSafepointer.
can anyone help me how should i be implementing this ?
Just remove the NSString from the logFilePath:
var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory = paths.objectAtIndex(0)
var fileName: String = String(format: "Logger.txt")
var logFilePath = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)
And a more safer way to do this would be like this:
let file = "Logger.txt"
let text = "A safer way to do this"
if let directory : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = directory.stringByAppendingPathComponent(file);
print(path)
do {
try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {
}
}
Let go of the casting ;-)
All you need to cast is the NSArray to get you the objectAtIndex() method
let paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths.objectAtIndex(0)
let fileName = String(format: "Logger.txt")
let logFilePath = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)
Extra bonus: use let instead of var.
EDIT:
A version without NSArray & NSString:
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = String(format: "Logger.txt")
let logFilePath = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent(fileName).absoluteString
freopen(logFilePath, "a+", stderr)

How to save an image (path) within my app?

I want to make an app, where the user fills out some kind of form, and attaches an image to it. The user picks a picture form the gallery (using UIImagePickerController). I already know how to display the image on the screen with info[UIImagePickerControllerOriginalImage] as? UIImage.
I want to make a file which will be used next time the app is opened, so the same image can be displayed with the notes. So preferably I want to save the path to the file so the next time I simply open it with UIImage(contentsOfFile: path). How can I get this path?
Some not so interesting code I have so far:
#IBAction func chooseFromGallery(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil)
self.image.image = info[UIImagePickerControllerOriginalImage] as? UIImage // This works
self.imagePath = // What goes here?
println(self.imagePath)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
Edit
After some reading I found out that there is no way of getting the path to an image which is saved outside the applications sandbox. So everybody looking for the same thing, stop, and use Bluehounds answer.
For more information about how files are handled in iOS (and OS X) read: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
A good place to store images is the documents directory. You can save to it and then retrieve images from it. I use these UIImage extensions for saving and retrieving images:
extension UIImage {
func save(fileName: String, type: String) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
if type.lowercaseString == "png" {
let path = "\(documentsPath)/\(fileName).\(type)"
UIImagePNGRepresentation(self).writeToFile(path, atomically: true)
} else if type.lowercaseString == "jpg" {
let path = "\(documentsPath)/\(fileName).\(type)"
UIImageJPEGRepresentation(self, 1.0).writeToFile(path, atomically: true)
} else {
}
}
convenience init?(fileName: String, type: String) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let path = "\(documentsPath)\(fileName).\(type)"
self.init(contentsOfFile: path)
}
}
Using this, I can store a file name rather than the entire path of an image using some data storing method such as Core Data.
Use:
someImage.save("SomeName" , type: "png")
if let image = UIImage(fileName: "SomeName", type: "png") {
}
Here is How you can save your image
- (IBAction)saveImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *image = imageView.image; // imageView is where you are displaying image after selecting one from UIImagePicker
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
You may fetch back this image like this
- (IBAction)getImage {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
}
Update :
I believe that you title for question implies a bit differently. Coming back to your question, If you are planning to use only one image all the time, you can have a single image name and use it. This will override the earlier saved image at all times.
Else another option could be creating a simple plist or db and use the image along with timestamp to uniquely identify each image.
Swift Code to Save Image:
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
if let dirPath = paths[0] as? String {
let image = ***YOUR IMAGE***
let writePath = dirPath.stringByAppendingPathComponent("Image2.png")
UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true)
}
}
}
To get the image can be very easliy relate from the same

Delete specified file from document directory

I want to delete an image from my app document directory. Code I have written to delete image is:
-(void)removeImage:(NSString *)fileName
{
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsPath = [paths objectAtIndex:0];
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", fileName]];
[fileManager removeItemAtPath:filePath error:NULL];
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:#"Congratulation:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];
}
Its working partially. This code deleting file from directory, but when I'm checking for the contents in directory, it still showing the image name there. I want to completely remove that file from directory. What should I change in the code to do the same? Thanks
I checked your code. It's working for me. Check any error you are getting using the modified code below
- (void)removeImage:(NSString *)filename
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:filename];
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (success) {
UIAlertView *removedSuccessFullyAlert = [[UIAlertView alloc] initWithTitle:#"Congratulations:" message:#"Successfully removed" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[removedSuccessFullyAlert show];
}
else
{
NSLog(#"Could not delete file -:%# ",[error localizedDescription]);
}
}
Swift 3.0:
func removeImage(itemName:String, fileExtension: String) {
let fileManager = FileManager.default
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
guard let dirPath = paths.first else {
return
}
let filePath = "\(dirPath)/\(itemName).\(fileExtension)"
do {
try fileManager.removeItem(atPath: filePath)
} catch let error as NSError {
print(error.debugDescription)
}}
Thanks to #Anil Varghese, I wrote very similiar code in swift 2.0:
static func removeImage(itemName:String, fileExtension: String) {
let fileManager = NSFileManager.defaultManager()
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
guard let dirPath = paths.first else {
return
}
let filePath = "\(dirPath)/\(itemName).\(fileExtension)"
do {
try fileManager.removeItemAtPath(filePath)
} catch let error as NSError {
print(error.debugDescription)
}
}
Swift 2.0:
func removeOldFileIfExist() {
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if paths.count > 0 {
let dirPath = paths[0]
let fileName = "someFileName"
let filePath = NSString(format:"%#/%#.png", dirPath, fileName) as String
if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
do {
try NSFileManager.defaultManager().removeItemAtPath(filePath)
print("old image has been removed")
} catch {
print("an error during a removing")
}
}
}
}
In Swift both 3&4
func removeImageLocalPath(localPathName:String) {
let filemanager = FileManager.default
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as NSString
let destinationPath = documentsPath.appendingPathComponent(localPathName)
do {
try filemanager.removeItem(atPath: destinationPath)
print("Local path removed successfully")
} catch let error as NSError {
print("------Error",error.debugDescription)
}
}
or
This method can delete all local file
func deletingLocalCacheAttachments(){
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
if fileURLs.count > 0{
for fileURL in fileURLs {
try fileManager.removeItem(at: fileURL)
}
}
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
}
Instead of having the error set to NULL, have it set to
NSError *error;
[fileManager removeItemAtPath:filePath error:&error];
if (error){
NSLog(#"%#", error);
}
this will tell you if it's actually deleting the file
I want to delete my sqlite db from document directory.I delete the sqlite db successfully by below answer
NSString *strFileName = #"sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSEnumerator *enumerator = [contents objectEnumerator];
NSString *filename;
while ((filename = [enumerator nextObject])) {
NSLog(#"The file name is - %#",[filename pathExtension]);
if ([[filename pathExtension] isEqualToString:strFileName]) {
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
NSLog(#"The sqlite is deleted successfully");
}
}
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:new_file_path_str error:&error];
if (error){
NSLog(#"%#", error);
}
FreeGor version converted to Swift 3.0
func removeOldFileIfExist() {
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
if paths.count > 0 {
let dirPath = paths[0]
let fileName = "filename.jpg"
let filePath = NSString(format:"%#/%#", dirPath, fileName) as String
if FileManager.default.fileExists(atPath: filePath) {
do {
try FileManager.default.removeItem(atPath: filePath)
print("User photo has been removed")
} catch {
print("an error during a removing")
}
}
}
}
You can double protect your file removal with NSFileManager.defaultManager().isDeletableFileAtPath(PathName) As of now you MUST use do{}catch{} as the old error methods no longer work.
isDeletableFileAtPath() is not a "throws" (i.e.
"public func removeItemAtPath(path: String) throws")
so it does not need the do...catch
let killFile = NSFileManager.defaultManager()
if (killFile.isDeletableFileAtPath(PathName)){
do {
try killFile.removeItemAtPath(arrayDictionaryFilePath)
}
catch let error as NSError {
error.description
}
}
If you are interesting in modern api way, avoiding NSSearchPath and filter files in documents directory, before deletion, you can do like:
let fileManager = FileManager.default
let keys: [URLResourceKey] = [.nameKey, .isDirectoryKey]
let options: FileManager.DirectoryEnumerationOptions = [.skipsHiddenFiles, .skipsPackageDescendants]
guard let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).last,
let fileEnumerator = fileManager.enumerator(at: documentsUrl,
includingPropertiesForKeys: keys,
options: options) else { return }
let urls: [URL] = fileEnumerator.flatMap { $0 as? URL }
.filter { $0.pathExtension == "exe" }
for url in urls {
do {
try fileManager.removeItem(at: url)
} catch {
assertionFailure("\(error)")
}
}

Resources