User selected NSImage reload after app relaunch in Objective C for macOS

46 views Asked by At

I am making a small project where a user can select an image and change it with filters.

I've got it working how I want, however I want all the user selected settings to return on relauch of the app. I've got all the filter settings to remain using plists.

But I can't find away of reloading the NSImage into the NSImageView after relaunch due to sandboxing - i've saved the user selected file path into a plist and have tried to load the Image in using the image URL however because of sandboxing it will not load it back.

Is there a special way I need to do this?

EDIT

Following advise to use a secure scoped bookmark i'm still having some issues

On my Awake nib I have written this - when I use NSLog on the path it is displaying the correct path but the image doesn't show in my image view.

 NSData *bookmark =[preferences objectForKey:@"bookmark"];
    NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
                                             options:NSURLBookmarkResolutionWithSecurityScope
                                     relativeToURL:nil bookmarkDataIsStale:nil error:nil];
    NSString *path = url.absoluteString;
    NSImage* imgC = [[NSImage alloc] initWithContentsOfFile: path];
    imageView2.image = imgC;

In my NSOpenPanel I have written this, so you can see how I created the bookmark

NSData *bookmark = nil;
                        NSError *error = nil;
                        bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
                                 includingResourceValuesForKeys:nil
                                                  relativeToURL:nil // Make it app-scoped
                                                          error:&error];
                        if (error) {
                            NSLog(@"Error creating bookmark for URL (%@): %@", url, error);
                            [NSApp presentError:error];
                        }

                       // NSLog(@"bookmark: %@", bookmark);
                    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                    [userDefaults setObject:bookmark forKey:@"bookmark"];
0

There are 0 answers