Search code examples
sandbox

Use sandbox to open one file and can access to another with the same name


I would to open myfile.plist with the NSOpenpPanel but I would to acess to myfile.png also if the user has just selected the plist one. I've already tried to access to the png doing

NSString* url = [[NSString stringWithFormat:@"file://localhost%@%@%@", pathWithoutFilename, @"/", imageFileName ]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

but doing:

NSData* imageData = [[NSData alloc] initWithContentsOfURL:nsurl];
NSImage *sourceImage = [[NSImage alloc] initWithData:imageData];

NSImage is nil.

How i can?


Solution

  • Here is the right Trick to save and Retreive Bookmark with NSURLBookmarkCreationWithSecurityScope for Sandbox Cocoa App: Finally

    -(void)saveTheNSData:(NSData *)data withFileName:(NSString *)fileName
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
        [data writeToFile:filePath atomically:NO];
    }
    
    -(NSURL*) getNSURLFromBookmarkIfExists:(NSString*) filename forType:(NSString*) type
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filename];
    
        NSData* data = [[NSData alloc] initWithContentsOfFile: filePath];
        if(data != nil)
        {
            NSURL* outUrl = [NSURL URLByResolvingBookmarkData:[[NSUserDefaults standardUserDefaults] objectForKey:type] options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil];
            [outUrl startAccessingSecurityScopedResource];
            return outUrl;
        }
        else
            return nil;
    }
    

    Also plese look at this example for the correct way to set up the bookmark using [NSUserDefaults standardUserDefaults] that does the trick:

    -(void) savePlistWithPlistUrl:(NSURL*) plistUrl andImageUrl:(NSURL*) imageUrl
    {
        NSData *bookmarkPlist = [self bookmarkFromURL:plistUrl];
    
        NSArray* pathSplitted = [pathFilePlist pathComponents];
        NSString* filenamePlist = [pathSplitted objectAtIndex:[pathSplitted count]-1];
    
        NSData *bookmarkImage = [self bookmarkFromURL:imageUrl];
    
        NSArray* pathImageSplitted = [imageUrl pathComponents];
        NSString* filenameImage = [pathImageSplitted objectAtIndex:[pathImageSplitted count]-1];
    
        NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];
        [prefs setObject:bookmarkPlist forKey:@"ExportPlist"];
        [prefs setObject:bookmarkImage forKey:@"ExportImage"];
        [prefs synchronize];
        // save the data bookmark
        [self saveTheNSData: bookmarkPlist withFileName:filenamePlist];
        [self saveTheNSData: bookmarkImage withFileName:filenameImage];
    }