Search code examples
cocoapermissionsappstore-sandbox

Drag and drop into sandboxed App


I have an application that needs to save a file in the same location as the source file drag on the window of the application. The sandbox does not let me even right after you have checked "User selected File" ....

Thanks.


Solution

  • When dropping a file to your App, sandbox grants only access to this file. You can't even rename it. To write a new file in the same directory, use an NSOpenPanel to get write access to the directory.

    eg:

     - (void)showGrantAccessForFolderOfFile:(NSURL *)urlOfFile
     {
        NSURL *urlToGrant = [urlOfFile URLByDeletingLastPathComponent];
        NSOpenPanel *openPanel = [NSOpenPanel openPanel];
        [openPanel setAllowsMultipleSelection:NO];
        [openPanel setCanChooseDirectories:YES];
        [openPanel setCanChooseFiles:NO];
        [openPanel setCanCreateDirectories:NO];
        [openPanel setDirectoryURL:urlToGrant];
    
        [openPanel setTitle:@"Grant Access To Folder"];
        [openPanel setMessage:@"Please grant access to the file’s folder"];
        [openPanel setPrompt:@"Grant Access"];
    
        // then run the panel as in documentation and handle errors
        // could also set a delegate and grey out other directories
        // store the sandboxed to access it later again
    }