3

For example if I have an XML file (exported from another app, so I have no control over the contents) that references images in different folders to the XML file, I am not able to read the images until the user has browsed to them using my app.

I can get around it by making them browse to a higher level folder (even browsing to "/"), but it seems a bit crap to be telling the user "I can't access /path/to/file, can you please browse to it now, so I can open it?"

Are there any better solutions to this? Isn't there some way to show a prompt like "AppX is trying to read from /Users/UserX/FolderX, will you grant permission?"

Grezzo
  • 2,220
  • 2
  • 22
  • 39
  • I'm not sure there is anything you can do other than prompting the user to open the files you want and copying them to your sandboxed documents folder? – trojanfoe Aug 02 '12 at 15:11

2 Answers2

3

See my question and answer here: App Sandbox: document-scoped bookmark not resolving; not returning any error

I'm embedding collection-scoped bookmarks into my XML files.

Update

Given the specification that you have no control over the input XML format, the only way to get access for your app to a file specified in the XML is by having the user select that file or one of the file's parent directories in an NSOpenPanel. If the XML contains multiple file references, you can have the user grant access to a directory that contains all of them.

There is no way around asking permission, since that would defeat the purpose of sandboxing. If your app could get around the user's direct permission, so could malware. Once given access, though, you can create a security-scoped bookmark to each file so you don't need to ask the user for permission multiple times.

Update 2

Answering the question in your comment, you can make the dialog easier for the user with the following:

[openPanel setMessage:@"Click 'OK' to allow access to files contained in the selected directory"];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:pathFromTheXMLFile]];

Here's the documentation on NSSavePanel (NSOpenPanel inherits from it, and the properties above are defined there).

Community
  • 1
  • 1
Dov
  • 15,530
  • 13
  • 76
  • 177
  • The XML file is not created by me, it's exported from another application, so I can't control what goes into it. – Grezzo Aug 10 '12 at 08:59
  • Fair enough, I was just hoping to find abetter way of asking for permission than having the user browse. It seems there is no other way. The solution we have implemented is to tell the user which folder contains files that we need to access, then get them to browse to that folder. The files are often in subfolders of a folder like ~/Documents, so I am telling them that a higher folder is fine. I am storing the bookmarks, so they hopefully won't have to browse to them very often. – Grezzo Aug 17 '12 at 06:32
  • You can also initialize the browser to the folder you suggest, and give explanatory text in the browser. All they have to do is click "OK" (or you can even change that button's caption to something like "Allow"). – Dov Aug 17 '12 at 10:27
2

This is a hack (somewhat like the one you mentioned) - if you can detect that the file has embedded file paths - after you have opened the file - can you prompt the user to "Import Files", and select the parent directory and copy the parent directory to the sandbox to read the files?

I know that sucks but having watched the apple vids on sandboxing without embedded bookmarks into the files Im not sure what else you could really do.

deleted_user
  • 3,817
  • 1
  • 18
  • 27