BOOL success;
NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:#"DB"];
success = [fileManager fileExistsAtPath:documentDBFolderPath];
if (success){
return;
}else{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"DB"];
[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath
error:&error];
}
}
Like this.
Resources/DB/words.csv => DB folder copy => Document/DB/words.csv
I want to copy DB subdirectory at Resources folder. I thought that source is good. But that source makes folder and doesn't copy files in DB folder at Resources folder.
I really want to copy files in DB folder at Resources folder. please help me.
1) Do not -autorelease the NSFileManager. You are double-releasing it which will crash your app.
2) No need to call -createDirectoryAtPath:. From the SDK doc of -copyItemAtPath:toPath:error:,
The file specified in srcPath must exist, while dstPath must not exist prior to the operation
and creating the directory the copy to fail.
Swift 3.0
Using String
func copyFolder(){
// Get the resource folder
if let resourceMainPath = Bundle.main.resourcePath{
var isDirectory = ObjCBool(true)
// Get the path of the folder to copy
let originPath = (resourceMainPath as NSString).appendingPathComponent("NameOfFolder")
// Get the destination path, here copying to Caches
let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
// Append the folder name to dest path so that system creates the directory if it doesnt exist
let destPath = (destinationPath as NSString).appendingPathComponent("/NameOfFolder")
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath, isDirectory:&isDirectory ){
// If an overwrite behavior is needed, remove and copy again here
print("Exists")
}else{
// Do the copy
do {
try fileManager.copyItem(atPath: originPath, toPath: destPath)
}catch let error{
print(error.localizedDescription)
}
}
}else{
}
}
Using URL
func copyTheFolder(){
// Get the resource folder
if let resourceMainURL = Bundle.main.resourceURL{
var isDirectory = ObjCBool(true)
// Get the path of the folder to copy
let originPath = resourceMainURL.appendingPathComponent("NameOfFolder")
// Get the destination path, here copying to Caches
let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
// Append the folder name to dest path so that system creates the directory if it doesnt exist
let destURL = URL(fileURLWithPath: destinationPath).appendingPathComponent("/NameOfFolder")
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destURL.path, isDirectory:&isDirectory ){
// If an overwrite behavior is needed, remove and copy again here
print("Exists")
}else{
// Do the copy
do {
try fileManager.copyItem(at: originPath, to: destURL)
}catch let error{
print(error.localizedDescription)
}
}
}
}
Related
I need to add some "default" file during the installation or the first launch of my app in the "Document" directory to let user access to "demo" files or presets. Is there a way to do it properly ?
It's mostly for iOS Apps.
It's very straightforward to get to the app's Documents directory:
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
Using the above function, you would be able to easily refer to the Documents folder to install files (or a folder containing your demo files) into the Documents directory and then read them out of there.
To copy a default file from your iOS application bundle, you could do something like:
let sourceURL = Bundle.main.url(forResource: "defaultfile", withExtension: ".png") // whatever kind of file it is
let destinationFolderURL = self.getDocumentsDirectory()
let fullDestURL = destinationFolderURL.appendingPathComponent("defaultfile.png")
let fileManager = FileManager.default
do{
try fileManager.copyItem(at: sourceURL, to: destURL)
} catch {
print(error)
}
Hopefully I didn't make any typos in the above example. :-)
Obj-C solution for anyone interested
If you want user to just read/view those files, all you need to do is to drag&drop them to your project files in Xcode, and then you can access them programmatically like this:
[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"relative/path/to/your/files"]
Alternatively, if you want your users to modify/re-save those files, you'll need to copy-paste them from your NSBundle to Documents directory on the first launch.
- (void)copyDemoFilesToDocumentsFolder {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *demoFilesPath = [DOCUMENTS_DIR stringByAppendingPathComponent:#"path/to/your/demo/files"];
if (![fileManager fileExistsAtPath:demoFilesPath]) {
NSString *sourceFolderPath = [[[NSBundle mainBundle] bundlePath]
stringByAppendingPathComponent:#"path/to/your/demo/files"];
[fileManager copyItemAtPath:sourceFolderPath
toPath:demoFilesPath
error:&error];
}
}
This question already has answers here:
unzip source code in Iphone
(2 answers)
Is there any zip decompression for iPhone?
(3 answers)
Closed 9 years ago.
After StoreKit downloads the IAP content package it returns an NSURL to me which looks like this:
file://localhost/private/var/mobile/Applications/45EF2B3A-3CAB-5A44-4B4A-631A122A4299/Library/Caches/BA32BC55-55DD-3AA4-B4AC-C2A456622229.zip/
Despite all sources I found claiming that StoreKit unzips the content package once downloaded, it hands me over a ZIP. This ZIP probably contains the file structure of the content package. But how do I unzip this?
Use Zip Foundation if you are working in Swift language. It's easy to use and one of the best swift library for unzipping a zip file.
Zip:
let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("file.txt")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("archive.zip")
do {
try fileManager.zipItem(at: sourceURL, to: destinationURL)
} catch {
print("Creation of ZIP archive failed with error:\(error)")
}
UnZip:
let fileManager = FileManager()
let currentWorkingPath = fileManager.currentDirectoryPath
var sourceURL = URL(fileURLWithPath: currentWorkingPath)
sourceURL.appendPathComponent("archive.zip")
var destinationURL = URL(fileURLWithPath: currentWorkingPath)
destinationURL.appendPathComponent("directory")
do {
try fileManager.createDirectory(at: destinationURL, withIntermediateDirectories: true, attributes: nil)
try fileManager.unzipItem(at: sourceURL, to: destinationURL)
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
}
If you are using Objective-C then SSZipArchive is the best choice for this.
You can unzip using this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *outputPath = [documentsDirectory stringByAppendingPathComponent:#"/ImagesFolder"];
NSString *zipPath = Your zip file path;
[SSZipArchive unzipFileAtPath:zipPath toDestination:outputPath delegate:self];
Hope it helps you.
There is a great 3rd party tool for zipping/unzipping files for iPhone
https://github.com/soffes/ssziparchive
Very simple to use. Hope that helps!!
Edit:
Quick method I created which takes url, downloads the zip and unzips it
-(void)downloadAndUnzip : (NSString *)sURL_p : (NSString *)sFolderName_p
{
dispatch_queue_t q = dispatch_get_global_queue(0, 0);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(q, ^{
//Path info
NSURL *url = [NSURL URLWithString:sURL_p];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
[data writeToFile:filePath atomically:YES];
dispatch_async(main, ^
{
//Write To
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:sFolderName_p];
[SSZipArchive unzipFileAtPath:filePath toDestination:dataPath];
});
});
}
I am downloading images to my app which after a few weeks the user will not care about. I download them to the app so they will not have to be downloaded every launch. The problem is I do not want the Documents folder to get bigger than it has to over time. So I thought I could "clean up" file older than a Month.
The problem is, there will be a few files in there that WILL be older than a month but that I do NOT want to delete. They will be Static Named files so they will be easy to identify and there will only be 3 or 4 of them. While there might be a few dozen old files I want to delete. So heres an example:
picture.jpg <--Older than a month DELETE
picture2.jpg <--NOT older than a month Do Not Delete
picture3.jpg <--Older than a month DELETE
picture4.jpg <--Older than a month DELETE
keepAtAllTimes.jpg <--Do not delete no matter how old
keepAtAllTimes2.jpg <--Do not delete no matter how old
keepAtAllTimes3.jpg <--Do not delete no matter how old
How could I selectively delete these files?
Thanks in advance!
Code to delete files which are older than two days. Originally I answered here. I tested it and it was working in my project.
P.S. Be cautious before you delete all files in Document directory because doing so you might end up losing your Database file(If you are using..!!) there which may cause trouble for your Application. Thats why I have kept if condition there. :-))
// Code to delete images older than two days.
#define kDOCSFOLDER [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"]
NSFileManager* fileManager = [[[NSFileManager alloc] init] autorelease];
NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:kDOCSFOLDER];
NSString* file;
while (file = [en nextObject])
{
NSLog(#"File To Delete : %#",file);
NSError *error= nil;
NSString *filepath=[NSString stringWithFormat:[kDOCSFOLDER stringByAppendingString:#"/%#"],file];
NSDate *creationDate =[[fileManager attributesOfItemAtPath:filepath error:nil] fileCreationDate];
NSDate *d =[[NSDate date] dateByAddingTimeInterval:-1*24*60*60];
NSDateFormatter *df=[[NSDateFormatter alloc]init];// = [NSDateFormatter initWithDateFormat:#"yyyy-MM-dd"];
[df setDateFormat:#"EEEE d"];
NSString *createdDate = [df stringFromDate:creationDate];
NSString *twoDaysOld = [df stringFromDate:d];
NSLog(#"create Date----->%#, two days before date ----> %#", createdDate, twoDaysOld);
// if ([[dictAtt valueForKey:NSFileCreationDate] compare:d] == NSOrderedAscending)
if ([creationDate compare:d] == NSOrderedAscending)
{
if([file isEqualToString:#"RDRProject.sqlite"])
{
NSLog(#"Imp Do not delete");
}
else
{
[[NSFileManager defaultManager] removeItemAtPath:[kDOCSFOLDER stringByAppendingPathComponent:file] error:&error];
}
}
}
You can get the file creation date, look at this SO Post and then just compare the dates. and create two different arrays for files needs to be deleted and non to be deleted..
My two cents worth. Change meetsRequirement to suit.
func cleanUp() {
let maximumDays = 10.0
let minimumDate = Date().addingTimeInterval(-maximumDays*24*60*60)
func meetsRequirement(date: Date) -> Bool { return date < minimumDate }
func meetsRequirement(name: String) -> Bool { return name.hasPrefix(applicationName) && name.hasSuffix("log") }
do {
let manager = FileManager.default
let documentDirUrl = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
if manager.changeCurrentDirectoryPath(documentDirUrl.path) {
for file in try manager.contentsOfDirectory(atPath: ".") {
let creationDate = try manager.attributesOfItem(atPath: file)[FileAttributeKey.creationDate] as! Date
if meetsRequirement(name: file) && meetsRequirement(date: creationDate) {
try manager.removeItem(atPath: file)
}
}
}
}
catch {
print("Cannot cleanup the old files: \(error)")
}
}
To find creation date of your file, you can refer to a very useful StackOverflow post:
iOS: How do you find the creation date of a file?
Refer to this post, this may help you in deleting them. You can just get a rough idea on what needs to be done for deletion of those data from Documents Directory:
How to delete files from iPhone's document directory which are older more than two days
Hope this helps you.
Here's a function that doesn't use string comparison for the dates and prefetches the modification time in the enumerator:
+ (NSArray<NSURL *> *)deleteFilesOlderThan:(NSDate *)earliestDateAllowed
inDirectory:(NSURL *)directory {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator<NSURL *> *enumerator =
[fileManager enumeratorAtURL:directory
includingPropertiesForKeys:#[ NSURLContentModificationDateKey ]
options:0
errorHandler:^BOOL(NSURL *_Nonnull url, NSError *_Nonnull error) {
NSLog(#"Failed while enumerating directory '%#' for files to "
#"delete: %# (failed on file '%#')",
directory.path, error.localizedDescription, url.path);
return YES;
}];
NSURL *file;
NSError *error;
NSMutableArray<NSURL *> *filesDeleted = [NSMutableArray new];
while (file = [enumerator nextObject]) {
NSDate *mtime;
if (![file getResourceValue:&mtime forKey:NSURLContentModificationDateKey error:&error]) {
NSLog(#"Couldn't fetch mtime for file '%#': %#", file.path, error);
continue;
}
if ([earliestDateAllowed earlierDate:mtime] == earliestDateAllowed) {
continue;
}
if (![fileManager removeItemAtURL:file error:&error]) {
NSLog(#"Couldn't delete file '%#': %#", file.path, error.localizedDescription);
continue;
}
[filesDeleted addObject:file];
}
return filesDeleted;
}
If you don't care about the files that got deleted you could make it return BOOL to indicate whether there were any errors, or simply void if you just want to make a best-effort attempt.
To selectively keep some of the files, either add a regular expression argument to the function that should match files to keep, and add a check for that in the while loop (seems to fit your use case best), or if there's a discrete amount of files with different patterns you could accept a NSSet with the filenames to keep and check for inclusion in the set before proceeding to the delete.
Also just mentioning this here since it might be relevant for some: The file system on iOS and OSX doesn't store mtime with greater precision than a second, so watch out for that if you require millisecond-precision or similar.
Corresponding test case to drop into your test suite if you want:
#interface MCLDirectoryUtilsTest : XCTestCase
#property NSURL *directory;
#end
#implementation MCLDirectoryUtilsTest
- (void)setUp {
NSURL *tempdir = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
self.directory = [tempdir URLByAppendingPathComponent:[NSUUID UUID].UUIDString isDirectory:YES];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtURL:self.directory
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
- (void)tearDown {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtURL:self.directory error:nil];
}
- (void)testDeleteFilesOlderThan {
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create one old and one new file
[fileManager createFileAtPath:[self.directory URLByAppendingPathComponent:#"oldfile"].path
contents:[NSData new]
attributes:#{
NSFileModificationDate : [[NSDate new] dateByAddingTimeInterval:-120],
}];
[fileManager createFileAtPath:[self.directory URLByAppendingPathComponent:#"newfile"].path
contents:[NSData new]
attributes:nil];
NSArray<NSURL *> *filesDeleted =
[MCLUtils deleteFilesOlderThan:[[NSDate new] dateByAddingTimeInterval:-60]
inDirectory:self.directory];
XCTAssertEqual(filesDeleted.count, 1);
XCTAssertEqualObjects(filesDeleted[0].lastPathComponent, #"oldfile");
NSArray<NSString *> *contentsInDirectory =
[fileManager contentsOfDirectoryAtPath:self.directory.path error:nil];
XCTAssertEqual(contentsInDirectory.count, 1);
XCTAssertEqualObjects(contentsInDirectory[0], #"newfile");
}
In Swift 3 and 4, to delete a specific file in DocumentsDirectory
do{
try FileManager.default.removeItem(atPath: theFile)
} catch let theError as Error{
print("file not found \(theError)")
}
I want the user to select any file present in her/his iPhone so that it’s used as an e-mail attachment. For this purpose, I want to show the list of all files and folders present in iPhone. But how would I get the list of those files? Is there any way to get that list?
Take into account that your app runs in a sandbox and would not be able to get any folder/file outside of that sandbox.
ObjectiveC
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList){
NSLog(#"%#", s);
}
Swift 4
guard let documentsDirectory = try? FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else { return }
guard let fileEnumerator = FileManager.default.enumerator(at: documentsDirectory, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions()) else { return }
while let file = fileEnumerator.nextObject() {
print(file)
}
Here's the slowest(?) approach:
NSFileManager * fileManager = [NSFileManager new];
NSArray * subpaths = [fileManager subpathsAtPath:path];
but that should at least point you to a more specialized implementation for your needs.
Slightly lower level abstractions which allow you to enumerate on demand include NSDirectoryEnumerator and CFURLEnumerator. Depending on the depth of the directory, these have the potential to save much unnecessary interactions with the filesystem, compared to -[NSFileManager subpathsAtPath:].
You can use NSDirectoryEnumerator via NSFileManager.enumeratorAtPath
From the docs:
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: #"Documents"];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
[localFileManager enumeratorAtPath:docsDir];
NSString *file;
while ((file = [dirEnum nextObject])) {
if ([[file pathExtension] isEqualToString: #"doc"]) {
// process the document
[self scanDocument: [docsDir stringByAppendingPathComponent:file]];
}
}
swift 3
let fileManager:FileManager = FileManager()
let files = fileManager.enumerator(atPath: NSHomeDirectory())
while let file = files?.nextObject() {
print("Files::",file)
}
I'm an author of FileExplorer control which is a file browser for iOS and fulfills most of your requirements. Note that it allows you to browse only those files and directories that are placed inside your sandbox.
Here are some of the features of my control:
Possibility to choose files or/and directories if there is a need for that
Possiblity to remove files or/and directories if there is a need for that
Built-in search functionality
View Audio, Video, Image and PDF files.
Possibility to add support for any file type.
You can find my control here.
I just want to create new folders in the documents folder of my iPhone app.
Does anybody know how to do that?
Appreciate your help!
I do that the following way:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
I don't have enough reputation to comment on Manni's answer, but [paths objectAtIndex:0] is the standard way of getting the application's Documents Directory
http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StandardBehaviors/StandardBehaviors.html#//apple_ref/doc/uid/TP40007072-CH4-SW6
Because the
NSSearchPathForDirectoriesInDomains
function was designed originally for
Mac OS X, where there could be more
than one of each of these directories,
it returns an array of paths rather
than a single path. In iOS, the
resulting array should contain the
single path to the directory. Listing
3-1 shows a typical use of this
function.
Listing 3-1 Getting the path to the
application’s Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Swift 3 Solution:
private func createImagesFolder() {
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath {
// create the custom folder path
let imagesDirectoryPath = documentDirectoryPath.appending("/images")
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: imagesDirectoryPath) {
do {
try fileManager.createDirectory(atPath: imagesDirectoryPath,
withIntermediateDirectories: false,
attributes: nil)
} catch {
print("Error creating images folder in documents dir: \(error)")
}
}
}
}
I don't like "[paths objectAtIndex:0]" because if Apple adds a new folder starting with "A", "B" oder "C", the "Documents"-folder isn't the first folder in the directory.
Better:
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
The Swift 2 solution:
let documentDirectoryPath: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
if !NSFileManager.defaultManager().fileExistsAtPath(documentDirectoryPath) {
do {
try NSFileManager.defaultManager().createDirectoryAtPath(documentDirectoryPath, withIntermediateDirectories: false, attributes: nil)
} catch let createDirectoryError as NSError {
print("Error with creating directory at path: \(createDirectoryError.localizedDescription)")
}
}
This works fine for me,
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *appSupportDir = [fm URLsForDirectory:NSDocumentsDirectory inDomains:NSUserDomainMask];
NSURL* dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:#"YourFolderName"];
NSError* theError = nil; //error setting
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
attributes:nil error:&theError])
{
NSLog(#"not created");
}
Swift 4.0
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
// Get documents folder
let documentsDirectory: String = paths.first ?? ""
// Get your folder path
let dataPath = documentsDirectory + "/yourFolderName"
if !FileManager.default.fileExists(atPath: dataPath) {
// Creates that folder if not exists
try? FileManager.default.createDirectory(atPath: dataPath, withIntermediateDirectories: false, attributes: nil)
}
Following code may help in creating directory :
-(void) createDirectory : (NSString *) dirName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:dirName];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]) {
NSLog(#"Couldn't create directory error: %#", error);
}
else {
NSLog(#"directory created!");
}
NSLog(#"dataPath : %# ",dataPath); // Path of folder created
}
Usage :
[self createDirectory:#"MyFolder"];
Result :
directory created!
dataPath : /var/mobile/Applications/BD4B5566-1F11-4723-B54C-F1D0B23CBC/Documents/MyFolder
Swift 1.2 and iOS 8
Create custom directory (name = "MyCustomData") inside the documents directory but only if the directory does not exist.
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
// create the custom folder path
let myCustomDataDirectoryPath = documentDirectoryPath.stringByAppendingPathComponent("/MyCustomData")
// check if directory does not exist
if NSFileManager.defaultManager().fileExistsAtPath(myCustomDataDirectoryPath) == false {
// create the directory
var createDirectoryError: NSError? = nil
NSFileManager.defaultManager().createDirectoryAtPath(myCustomDataDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirectoryError)
// handle the error, you may call an exception
if createDirectoryError != nil {
println("Handle directory creation error...")
}
}