Question

When I use

NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"pdf", @"PDF", nil];
NSInteger result    = [openPanel runModalForTypes:fileTypes];

it works as expected (only pdf files can be selected), but when I use

NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"pdf", @"PDF", nil];
[openPanel setAllowedFileTypes:fileTypes];
NSInteger result    = [openPanel runModal];

I can select all types of files, not just pdfs. The documentation says that runModalForTypes is deprecated and we should use the second way. Am I doing something wrong?

Was it helpful?

Solution

Are you building for Mac OS X 10.6 and later? According to the header file: "On versions less than 10.6, this property is ignored."

Otherwise, your code looks correct (assuming you release fileTypes at some point) and works for me (tested on Mac OS X 10.7.2).

One minor suggestion is to use a Uniform Type Identifier to identify PDFs instead of hard-coding file extensions, like so:

NSArray *fileTypes = [NSArray arrayWithObjects:(id)kUTTypePDF, nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top