What I want is very similar to folderBrowsingDialog and folderBrowsingDialog.selectedPath in C#.

I want to build a Mac OS app (Objective C) where user can browse directory from UI and select a file, get the location of the file and read it`s content in a string variable. What UI component should I use for browsing and reading location of the file?

For file manipulation, I understand I need to work with NSFileManager. But how to carry out the first part? Is there any good documentation on cocoaapplication UI programming for Mac OS for carrying out the task?

There is a question here that sounds similar to this one but that does not discuss UI part.

1

There are 1 best solutions below

1
On BEST ANSWER

Here is a simple example of how this can be done:

- (IBAction)buttonAction:(id)sender {
    NSOpenPanel *openPanel = [NSOpenPanel new];
    openPanel.canChooseFiles = YES;
    openPanel.canChooseDirectories = NO;
    openPanel.allowsMultipleSelection = YES;
    [openPanel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            for (NSURL* fileURL in openPanel.URLs) {
                NSData *fileContent = [NSData dataWithContentsOfURL:fileURL];
                NSString* stringFileContent = [[NSString alloc] initWithData:fileContent encoding:NSUTF8StringEncoding];
                NSLog(@"File content: %@", stringFileContent);
            }
        }
    }];
}