Yes, Try/Catch is the way here. You may need to experiment a bit to find exactly which statement provokes the OS to issue the pop-up to the user. In my case, I had made a FolderItem for the Documents folder, created another folder inside it, checked that this new folder was readable and writeable, and it was then when I wanted to open a logfile there that I needed the Try/Catch:
try // Here we see whether the user allowed access to these files
op = TextOutputStream.Open (fptr)
catch
msg = "Unable to create or access data files" // Big Sur and later
quitDialog (msg) // Here, quit with a message to the user.
end try
I must add that Error 1: Might be incorrect permissions or macOS Security interfering. I’m going to try to find an easy way to determine this. So that when we tell the customer they can either correct the permissions or update the macOS security.
The debugger is going to stop there, it’s up to you to hit Run to go into the Catch. You can turn off Break on Exceptions in the Project menu of the IDE, or surround your code in the following:
#Pragma BreakOnExceptions Off
Try
// your code
Catch e As NilObjectException
// handle exception
End Try
#Pragma BreakOnExceptions Default // Restore setting from Project menu
In this case, the file holds data that is used to populate a PopupMenu with default names.
When the user refuse the permissions, the PopupMenu stays empty, the user can type text into a TextField.
The user can also quit the application, run it a second time and then accept to give the permission.
Remember: this is from Big Sur new security. I totally dislike that, but no one ask my advice nor my permission
Read back my code screenshot above: I tell the user what happens (the sentence can be improved for real users instead of me) and why. I will add something about re-run the application and give permission.
I was unaware of the “Default” option in pragmas
Even though I’m using Xojo since it was RealBasic, there are a lot of things that goes unnoticed from releases to releases; this isn’t the first time.