Ken.T
Ken.T

Reputation: 11

I want to copy the file in sandbox without NSOpenPanel

I want to copy some files to specified folder by NSOpemPanel. The source file is reading from XML and show to list in NSTable.

I can copy file by copyItemAtPath. But now my app will turn to sandbox, then I can’t copy the file by copyItemAtURL.

How do I copy the file in sandbox mode?

I was looked over a lot of post. And I think the Security-scoped Bookmark may be a solution for this. But I can’t create "Security-scoped Bookmark" from XML inside the path (the path was convert to NSURL ready). I was setting to sandbox.entitlements but it's not clear this problem.

Is there any way for this?

Develop in macOS10.12 and Xcode8.3.3

Thanks

Upvotes: 0

Views: 253

Answers (1)

CRD
CRD

Reputation: 53010

How do I copy the file in sandbox mode?

It is unclear what your current code is doing, but the rules under the sandbox are simple: To read or write a file located outside of an application's own container (which is hidden away under the Library folder) your application must either:

  • Use NSOpenPanel to obtain a URL from the user for the file path; or
  • Use NSOpenPanel to obtain a URL from the user for one of the ancestor folders of the file.

The second option gives access to a whole folder, including any sub-folders; i.e. the whole file/folder subtree rooted in the folder.

As you want to copy "some files" it sounds like asking the user for permission for the folder is appropriate. You can customise the NSOpenPanel to be a "request permission" dialog. If you are requesting a specific folder you can also have the dialog open in it's containing folder and only have the specific folder enabled for selection by the user.

Once you have the URL for the folder from NSOpenPanel you can create a security scoped bookmark for it and save that in your app's preferences or other configuration file (stored within the app's container). Doing this enables your app to regain access to the folder on subsequent executions without asking the user again.

If after investigating this issue and writing some code you hit an issue ask a new question, showing your code, and explaining the problem. Someone will undoubtedly help you with the next step.

HTH

Upvotes: 2

Related Questions