I'm using NSOpenPanel to allow a user to select a folder to save documents into. I would like to restrict what folder (in terms of hierarchy) they can save into. Essentially, I want to prevent them from choosing any folder above:
/Users/username/
So the folder
/Users/username/cats/
would be acceptable but
/Users/username/
/Applications/cats/
would not be allowed. I was wondering how to implement this restriction.
Thanks.
Note that
NSOpenPanel
inherits fromNSSavePanel
, which in turn defines a delegate and a corresponding delegate protocolNSOpenSavePanelDelegate
. You can use the delegate to extend the behaviour of the open panel so as to include the restriction you’ve listed in your question.For instance, assuming the application delegate implements the open panel restriction, make it conform to the
NSOpenSavePanelDelegate
protocol:In the implementation of your application delegate, tell the open panel that the application delegate acts as the open panel delegate:
And implement the following delegate methods: