The code is as follows:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openpanel setprompt: @ "open"];
openPanel.allowedFileTypes = [NSArray arrayWithObjects: @"txt", @"doc", nil];
openPanel.directoryURL = nil;
[openPanel beginSheetModalForWindow:[self gainMainViewController] completionHandler:^(NSModalResponse returnCode) {
if (returnCode == 1) {
NSURL *fileUrl = [[openPanel URLs] objectAtIndex:0];
//Get file content
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:fileUrl error:nil];
NSString *fileContext = [[NSString alloc] initWithData:fileHandle.readDataToEndOfFile encoding:NSUTF8StringEncoding];
//Pass the acquired data to textview of viewcontroller
ViewController *mainViewController = (ViewController *)[self gainMainViewController].contentViewController;
mainViewController.showCodeTextView.string = fileContext;
}
]
MAC OS x Development Learning-Open file selector and get files