Programmatically get the path to the application support folder

I am trying to get NSString for a custom application support folder.

I know I can do NSString *path = @"~/Library/Application Support"; but it does not seem very elegant. I played using NSSearchPathForDirectoriesInDomains , but it seems to be quite long and creates some unnecessary objects (at least my implementation).

Is there an easy way to do this?

+43
ios objective-c cocoa swift
Dec 08 '11 at 12:05
source share
7 answers

Best practice is to use NSSearchPathForDirectoriesInDomains with NSApplicationSupportDirectory as "long waving" as it may be.

Example:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); NSString *applicationSupportDirectory = [paths firstObject]; NSLog(@"applicationSupportDirectory: '%@'", applicationSupportDirectory); 

NSLog Output:

 applicationSupportDirectory: '/Volumes/User/me/Library/Application Support' 
+63
Dec 08 '11 at 12:13
source share

Swift 4:

 print(NSHomeDirectory()) 

or

 print(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) 

and

 let yourString = String(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) 
+21
Nov 10 '15 at 20:42
source share

Swift 3:

 FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first 
+14
Sep 27 '16 at 7:54 on
source share

Just to make sure people start using the recommended method:

 - (NSArray<NSURL *> * _Nonnull)URLsForDirectory:(NSSearchPathDirectory)directory inDomains:(NSSearchPathDomainMask)domainMask 

An extended example from the documentation:

 - (NSURL*)applicationDataDirectory { NSFileManager* sharedFM = [NSFileManager defaultManager]; NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask]; NSURL* appSupportDir = nil; NSURL* appDirectory = nil; if ([possibleURLs count] >= 1) { // Use the first directory (if multiple are returned) appSupportDir = [possibleURLs objectAtIndex:0]; } // If a valid app support directory exists, add the // app bundle ID to it to specify the final directory. if (appSupportDir) { NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier]; appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID]; } return appDirectory; } 

Proof link: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW3

+6
Oct 05 '15 at 10:56
source share

This works for me:

 NSError *error; NSURL* appSupportDir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error]; 
+1
Dec 10 '15 at 16:44
source share

This is what I use to get the database. Got it from the Stanford class. It might help someone.

 NSURL *url = [[[NSFileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"database_name"]; NSLog(@"Database URL: %@",url); 
0
Feb 20 '12 at 20:55
source share

Create a separate C object class to read and write to the document directory. I will not rewrite the code. Below is my version.

 //Directory.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #define PATH (NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)) #define BASEPATH (([PATH count] > 0)? [PATH objectAtIndex:0] : nil) @interface DocumentsDirectory : NSObject //Here you can also use URL path as return type and file path. +(void)removeFilesfromDocumentsDirectory:(NSString*)filename; +(NSString*)writeFiletoDocumentsDirectory:(NSString*)filename; @end #import "Directory.h" @implementation DocumentsDirectory UIAlertView *updateAlert; +(void)removeFilesfromDocumentsDirectory:(NSString*)filename { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [BASEPATH stringByAppendingPathComponent:filename]; NSError *error; BOOL success = [fileManager removeItemAtPath:filePath error:&error]; //Remove or delete file from documents directory. if (success) { updateAlert= [[UIAlertView alloc] initWithTitle:@"Congratulations:" message:@"File is updated successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [updateAlert show]; } else { NSLog(@"Could not delete file -:%@ ",[error localizedDescription]); updateAlert= [[UIAlertView alloc] initWithTitle:@"Try again:" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [updateAlert show]; } } +(NSString*)writeFiletoDocumentsDirectory:(NSString*)filename { NSString *foldDestination = BASEPATH; NSString *filePath = [foldDestination stringByAppendingPathComponent:filename]; return filePath; } @end 
0
Jul 14 '16 at 5:58
source share



All Articles