Good time of day, the task is as follows: you need to select a folder and print the full path of this folder to the label, where to dig?

  • Where do you choose the folders? From the app bundle or from the file system? Where exactly? - Stanislav Pankevich
  • The fact of the matter is that I do not choose, I do not know how to do it in principle; ( - leonid
  • one
    I wrote the answer, but in general your question really falls into the category of questions like "work for the author", but such questions are not very much loved here (in the sense they like to close). - Stanislav Pankevich

1 answer 1

Too vague you set the task, so the answer will be in general:

Firstly, I remind you that in iOS there is such a thing as Sandboxing (Google Help), which, briefly, means that for any of your applications, access to the device’s file system is limited to several directories such as Documents/ and tmp/ (there are several , but it is really recommended to work with Documents and tmp/ ), that is, you can only access these directories, but you cannot access others.

Most likely you need to work with NSFileManager .

Try this (since you do not know what and where to output, I will show it for the temporary directory):

To see what is in the directory without passing its subdirectories:

 NSString *path = NSTemporaryDirectory(); NSArray *directoryContents = [NSFileManager.defaultManager subpathsOfDirectoryAtPath:path error:nil]; NSLog(@"Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΈΠΌΠΎΠ΅ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ Π±Π΅Π· заглядывания Π² ΠΏΠΎΠ΄Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ: %@", directoryContents); 

To see what is in the directory with regard to all subdirectories, their subdirectories, etc.:

 NSString *path = NSTemporaryDirectory(); NSLog(@" %@", [NSFileManager.defaultManager contentsOfDirectoryAtPath:path error:nil]); NSLog(@"Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠΈΠΌΠΎΠ΅ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ с ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΎΠΌ ΠΏΠΎ всСм поддирСкториям: %@", directoryContents); 

NSTemporaryDirectory () - helps to get the path to the folder tmp/ your application. And here is a way to get the path to the Documents folder of your application:

 NSString *pathToDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSLog(@"ΠŸΡƒΡ‚ΡŒ ΠΊ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ Documents вашСго прилоТСния: %@", pathToDocumentsDirectory); 

Using the methods above, you get arrays containing the internals of the directory as strings: the path to the folders and files that are in this directory.

Then you use something like a loop.

 for (NSString *elementPath in directoryContents) { NSLog(@"ΠžΡ‡Π΅Ρ€Π΅Π΄Π½ΠΎΠΉ элСмСнт (ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ ΠΈΠ»ΠΈ ΠΏΠ°ΠΏΠΊΡƒ) Π»Π΅ΠΆΠ°Ρ‰ΠΈΠΉ Π² Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ path: %@", elementPath); / Π—Π΄Π΅ΡΡŒ Π²Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π²Π·ΡΡ‚ΡŒ этот elementPath ΠΈ вывСсти Π΅Π³ΠΎ ΠΊΡƒΠ΄Π°-Π½ΠΈΠ±ΡƒΠ΄ΡŒ Π² свой UI интСрфСйс. } 

I advise you to find the documentation for the class NSFileManager in Russian .

There are methods for creating files:

 Creating and Deleting Items – createDirectoryAtURL:withIntermediateDirectories:attributes:error: – createDirectoryAtPath:withIntermediateDirectories:attributes:error: – createFileAtPath:contents:attributes: – removeItemAtURL:error: – removeItemAtPath:error: – replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: – trashItemAtURL:resultingItemURL:error: 

Methods for checking the existence of files or directories:

 Determining Access to Files – fileExistsAtPath: – fileExistsAtPath:isDirectory: – isReadableFileAtPath: – isWritableFileAtPath: – isExecutableFileAtPath: – isDeletableFileAtPath: 

And the most interesting group of methods for you to work with the contents of directories . I already mentioned two of them with examples above.

 Discovering Directory Contents – contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: – contentsOfDirectoryAtPath:error: – enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: – enumeratorAtPath: – mountedVolumeURLsIncludingResourceValuesForKeys:options: – subpathsOfDirectoryAtPath:error: – subpathsAtPath: 

The NSFileManager documentation also describes another way to NSFileManager through the contents of a directory (see the description of the enumeratorAtURL method there: includingPropertiesForKeys: options: errorHandler:. Here’s an example from there that goes through the directory, through all the guts and it ignores the extras folder in it and all its contents:

 -(void)scanURLIgnoringExtras:(NSURL *)directoryToScan { // Create a local file manager instance NSFileManager *localFileManager=[[NSFileManager alloc] init]; // Enumerate the directory (specified elsewhere in your code) // Request the two properties the method uses, name and isDirectory // Ignore hidden files // The errorHandler: parameter is set to nil. Typically you'd want to present a panel NSDirectoryEnumerator *dirEnumerator = [localFileManager enumeratorAtURL:directoryToScan includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey,nil] options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]; // An array to store the all the enumerated file names in NSMutableArray *theArray=[NSMutableArray array]; // Enumerate the dirEnumerator results, each value is stored in allURLs for (NSURL *theURL in dirEnumerator) { // Retrieve the file name. From NSURLNameKey, cached during the enumeration. NSString *fileName; [theURL getResourceValue:&fileName forKey:NSURLNameKey error:NULL]; // Retrieve whether a directory. From NSURLIsDirectoryKey, also // cached during the enumeration. NSNumber *isDirectory; [theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL]; // Ignore files under the _extras directory if (([fileName caseInsensitiveCompare:@"_extras"]==NSOrderedSame) && ([isDirectory boolValue]==YES)) { [dirEnumerator skipDescendants]; } else { // Add full path for non directories if ([isDirectory boolValue]==NO) [theArray addObject:theURL]; } } // Do something with the path URLs. NSLog(@"theArray - %@",theArray); } 

What I have written is more than enough. Turn on your mind and everything should work out.