kyle k
kyle k

Reputation: 5512

Cocoa file selection dialog allowed filetypes

I am trying to set the filetypes that are allowed for the file selection dialog, but the dialog is not filtering the allowed types, It is allowing me to upload any file type,

But I only want it to upload HTML files, I also want the non allowed files to be faded in the dialog.

Is this method of filtering filetypes supported under OSX 10.9? I am getting deprecation warnings.

- (IBAction)openfile:(id)sender {
    int i;
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    [openDlg setCanChooseFiles:YES];

    [openDlg setAllowedFileTypes:@[@"html", @"htm"]];

    if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
    {
        NSArray* files = [openDlg filenames];
        for( i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];
            NSLog(@"%@", fileName);
        }
    }
}

Upvotes: 0

Views: 700

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

setAllowedFileTypes is not deprecated and is the correct way

runModalForDirectory is deprecated and should be replaced with a completionHandler

filenames is also deprecated. use NSURLs instead of paths (always :))


modernized:

- (IBAction)openfile:(id)sender {
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    [openDlg setCanChooseFiles:YES];

    [openDlg setAllowedFileTypes:@[@"html", @"htm"]];

    [openDlg beginWithCompletionHandler:^(NSInteger result) {
        if(result==NSFileHandlingPanelOKButton) {
            for (NSURL *url in openDlg.URLs) {
                NSLog(@"%@", url);
            }
        }
    }];
}

Upvotes: 2

Related Questions