Mac CGDataConsumerCreateWithFilename: failed to open `.myfile.pdf' for writing: Operation not permitted
Asked Answered
H

1

6

I am generating a PDF document and offering the user the option to save it to disk. I present them with an NSSavePanel for them to select the filename to save it as. If they choose a file that already exists, it prompts them if they are sure and want to Replace that file. If they agree to replace the file, then I generate the PDF and write it to that chosen URL.

However, my write fails with the following error:

CGDataConsumerCreateWithFilename: failed to open `/path/to/.myfile.pdf' for writing: Operation not permitted

I have the appropriate Entitlements to write to files on disk. The file that's in the way is the one I generated during a previous test. Do I need to explicitly delete the existing file before I write mine to that URL or is there some way I can tell the system that it may overwrite the existing file?

This is the code I use to launch the NSSavePanel (my code saves their last chosen directory so that it can default to the same location for all future saves):

- (NSURL*) requestSaveFilenameForExtension:(NSString*)fileExtension previousSaveDirectorySettingKey:(NSString*)previousDirectorySettingKey defaultFilename:(NSString*)defaultFilename {

    NSSavePanel *panel = [NSSavePanel savePanel];
    [panel setAllowedFileTypes:[NSArray arrayWithObject:fileExtension]];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *previousSaveDirectory = [defaults stringForKey:previousDirectorySettingKey];

    if (previousSaveDirectory == nil) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        previousSaveDirectory = [paths objectAtIndex:0];
    }

    [panel setExtensionHidden:NO];
    [panel setNameFieldStringValue:defaultFilename];
    [panel setDirectoryURL:[NSURL fileURLWithPath:previousSaveDirectory]];

    NSInteger ret = [panel runModal];
    if (ret == NSFileHandlingPanelOKButton) {
        NSString *saveDirectory = [[[panel URL] absoluteString] stringByDeletingLastPathComponent];
        [defaults setValue:saveDirectory forKey:previousDirectorySettingKey];

        return [panel URL];
    } else {
        return nil;
    }

}

and here is the code I'm using to write the file to that given URL, it's just standard PDFKit -writeToURL::

PDFDocument *document = [self generateDocument];    
[document writeToURL:documentURL];
Hamm answered 19/3, 2014 at 19:7 Comment(2)
I jut ran into the same problem.. any ideas how to fix this?Recurved
running into this issue as wellGagnon
N
1

i ran into same problem. Go to app target capabilities: check File Access Permission & Access as "read/write" for all the folders.

Then your app will have access rights to write the file at User selected, download, pictures, music and movies folders. Not the other folders.

And it is recommended to use "NSSavePanel" to interact with users for save the file, this way, you will get the hint, whether the access rights is set correctly when you build the project.. Otherwise, you will end up getting the error like now, without having a clue why. sucks..

Nicolettenicoli answered 6/2, 2021 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.