r/macosprogramming • u/B8edbreth • Jan 12 '24
Programmatically opening documnents
In my app I have a window similar to Xcode's recent projects window. When a user selects a recent item they worked on however, unlike with excode, the project comes up locked. So if they try to save it they get the "are you sure you want to over write the file" message.
In my tableview selection changed notification I have this code to open the file
BOOL success = [retString startAccessingSecurityScopedResource];
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
[fileCoordinator coordinateReadingItemAtURL:retString options:NSFileCoordinatorReadingForUploading error:&error byAccessor:^(NSURL *newURL) {
NSError * outError;
[[NSDocumentController sharedDocumentController]openDocumentWithContentsOfURL:retString display:YES error:NULL];
NSLog(@"Error %@",error);
if (success) {
[retString stopAccessingSecurityScopedResource];
}
}];If I used the method to open the file that isn't deprecated the file simply doesn't open and I get an error that I cannot access it.
Do I have to have a developer account and code sign the app to make this work?
Edit to add: I'm storing the recent items in my apps defaults file like this:
NSData * dta = [self.fileURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
View all comments
Show parent comment
1
u/gybemeister Jan 13 '24
You can ignore this for now. The problem is that bookmarks can become stale (there is a isStale variable in the code and renewLastDbBookmark just asks the user to open the file again and then saves the resulting bookmark).
I think you have a second problem somewhere in your code. When I implemented the recents in my app I got the error file not found until I implemented the bookmarks. I never got an overwrite error. Note that when you create a bookmark you get a second URL and that is the one you need to save to UserDefaults when the user opens the file. Then, when it goes through the recent files you get that saved bookmark and open the file with it, not the actual path of the file.