Hey all,

I’m currently in the process of re-writing one of my old apps and I have 
decided to do so using modern Mac technology and APIs. The target is the Mac 
App Store and thus my app needs to be sandboxed.

My app, for everyone's sake, is not document-based. The user can open files by 
dragging them onto my app, or they can open them with the Open menu item. What 
I’m looking to do is allow the user to press a button and have an external XPC 
service start which then reads the opened file(s) and deletes them off of the 
main thread (this is a simplification).

Since XPC services cannot present UI, I need to find a way to give the XPC 
service access to the file(s) the user has selected. I know about 
security-scoped bookmarks, and I have used them before, but I’m not certain how 
to implement this. First off all:

1) Apple’s docs say that non-document based apps don’t get sandboxing support 
automatically handled for them, requiring the manual use of the NSFile* APIs. 
Although when the user opens the file that should give my app access, I can’t 
get this to translate into a working security-scoped bookmark.

2) Apple says that app-scoped bookmarks can only be used by apps with the same 
container as the app that created it. I don’t know if this means that 
security-scoped bookmark data based across XPC will remain valid inside of the 
XPC service. On the other hand, document-scoped bookmarks *can* be used by 
other apps, but can’t point to folders, which might be a deal-breaker for me.

Can someone enlighten me as to the best way to handle this? Here’s some 
relevant code snippets:

- (void)application:(NSApplication *)sender openFiles:(NSArray *)fileNames {
        NSLog(@"Files dragged on: %@", fileNames);
        IncinerationWindowController *controller = 
[[IncinerationWindowController alloc]
 initWithWindowNibName:@“IncinerationWindowController" 
andListOfFiles:fileNames];
}

- (instancetype)initWithWindowNibName:(NSString *)windowNibName 
andListOfFiles:(NSArray *)dcArray {
        self = [super initWithWindowNibName:windowNibName];
        if (self) {
                self->documentArray = dcArray;
                self->securityURLArray = [[NSMutableArray alloc] 
initWithCapacity:dcArray.count];

                self->isCurrentlyIncinerating = NO;
                [self createSecurityBookmarks];
        }

        return self;
}

- (void)createSecurityBookmarks {
        for (NSString *path in self->documentArray) {
                NSError *error = nil;
                NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

                NSData *bookmarkData = [url 
bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope 
includingResourceValuesForKeys:nil relativeToURL:nil error:&error];
                if (!bookmarkData) {
                        NSLog(@"Failed to create security scoped bookmark: %@", 
error);
                } else [securityURLArray addObject:bookmarkData];
        }
}

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to