Both the Mac OS X and iPhone OS, we can access easily and directly to the directory - most important - the system which are:
1 2 3 | NSHomeDirectory Returns the path to the current user's home directory. Returns the path to NSHomeDirectoryForUser Given a user's home directory. NSTemporaryDirectory Returns the path of the temporary directory for the current user. |
Here is the code needed to use them:
1 2 3 4 5 6 7 8 9 10 11 | / / Main directory homePath = NSHomeDirectory ( ) ; NSString * NSHomeDirectory HOMEPATH = (); tempPath = NSTemporaryDirectory ( ) ; NSString * TempPath NSTemporaryDirectory = (); userPath = NSHomeDirectoryForUser ( NSUserName ( ) ) ; NSString * = userPath NSHomeDirectoryForUser (NSUserName ()); "homePath = %@" , homePath ) ; NSLog (@ "HOMEPATH =% @", HOMEPATH); "tempPath = %@" , tempPath ) ; NSLog (@ "TempPath =% @", TempPath); "userPath = %@" , userPath ) ; NSLog (@ "userPath =% @", userPath); / / HOMEPATH = / var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905 / / = TempPath / private/var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905/tmp / / / = UserPath / var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905 |
Note: you have noticed, line 4, the call to
NSUserName()which returns the "current user"
There are, then, a number of other "folders" that we can access. These can be obtained by calling the function NSSearchPathForDirectoriesInDomains() passing as inputs a set of constants. For example, we can get the Documents folder in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES ) ; NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); "paths = %@" , paths ) ; NSLog (@ "paths =% @", paths); / / Paths = ("/ var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905/Documents") / / Documents folder documentsPath = [ paths objectAtIndex : 0 ] ; NSString * documentsPath = [paths objectAtIndex: 0]; "documentsPath = %@" , documentsPath ) ; NSLog (@ "documentsPath =% @", documentsPath); / / = DocumentsPath / var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905/Documents / / Or ... documentsDirectory = [ NSHomeDirectory ( ) stringByAppendingPathComponent : @ "Documents" ] ; NSString * documentsDirectory = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"]; "documentsDirectory = %@" , documentsDirectory ) ; NSLog (@ "documentsDirectory =% @", documentsDirectory); / / = DocumentsDirectory / var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905/Documents |
Note: In this last case, note the use of
stringByAppendingPathComponentthat "concatenates" Documents to the path, adding the slash
NSSearchPathForDirectoriesInDomains() always returns an array of strings, although in cases maggiornaza elements are only one. If you look closely the output paths , in fact, is indicated in brackets, ie an array. In the first case is extracted with the first element [paths objectAtIndex:0] .
Resources
delle applicazioni Mac è “virtuale”, per non dire fittizia. The object through NSBundle , instead, we access the resources folder of an application, as you know, the extension .app Mac applications is "virtual", if not fictitious. Indeed, it conceals a kind of "folder" with all the necessary resources within the application itself, including the executable itself! ![]()
1 2 3 4 5 | / / Application appFolderPath = [ [ NSBundle mainBundle ] resourcePath ] ; NSString * appFolderPath = [[ NSBundle mainBundle] resourcePath]; "appFolderPath = %@" , appFolderPath ) ; NSLog (@ "appFolderPath =% @", appFolderPath); / / = AppFolderPath / var/mobile/Applications/54A1A3C9-322E-4C23-B096-45869B0332A5/MyApp.app |
List the contents of a folder (browsing)
The File Management, managed by the object NSFileManager , used to perform many useful operations such as:
- Create directories and files
- Copy, move, and "link" files and directories
- Compare files and directories
- Getting the directory content
- and more ...
For example ...
1 2 3 4 5 6 7 8 9 10 11 | / / List Directory homePath = NSHomeDirectory ( ) ; NSString * NSHomeDirectory HOMEPATH = (); fileManager = [ NSFileManager defaultManager ] ; NSFileManager FileManager * = [ NSFileManager defaultManager]; "La cartella %@ contiene = %@" , homePath, [ fileManager directoryContentsAtPath : homePath ] ) ; NSLog (@ "The folder contains% @ =% @", HOMEPATH, [FileManager directoryContentsAtPath: HOMEPATH]); / / The folder / var/mobile/Applications/C7B94C13-DCEC-40CD-942F-93FD302BF905 / contains = ( / / Documents, / / Library / / "MyApp.app" / / Tmp / /) |
Bonus
With regard to the information "user" may have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | / / FullUserName fullUserName = NSFullUserName ( ) ; NSString * fullUserName NSFullUserName = (); "fullUserName = %@" , fullUserName ) ; NSLog (@ "fullUserName =% @", fullUserName); / / = Mobile User fullUserName / / NSUserName userName = NSUserName ( ) ; NSString * userName = NSUserName (); "userName = %@" , userName ) ; NSLog (@ "userName =% @", userName); / / UserName = mobile / / NSHomeDirectoryForUser directoryForUser = NSHomeDirectoryForUser ( userName ) ; NSString * directoryForUser NSHomeDirectoryForUser = (userName); "directoryForUser = %@" , directoryForUser ) ; NSLog (@ "directoryForUser =% @", directoryForUser); / / = DirectoryForUser / var/mobile/Applications/DFD6AED2-6B14-4515-8DCD-2AF6F6C2E00C |
I leave you to try the "differences" between the use of the simulator and the device.










[...] Very short snippet: Apple iPhone file system on both Mac OS X and iPhone OS, we can access easily and directly to the directory - most important - the system which are: PLAIN TEXT CODE: NSHomeDirectory Returns the path to the current user's home directory. Returns the path to NSHomeDirectoryForUser Given a user's home directory. Blog: undolog | read more [...]