swiftcrashtry-catchthrow

Prevent to crash bc. missing access rights


I have a folder, I like to be opened by NSWorkspace. But if access rights are missing, the entire app is crashing. How can I aviod this and show a message, that access rights were missing? I tried "try-catch", but this function does not throw errors!

NSWorkspace.shared.open(urlOutput)   // this could crash 

Solution

  • You can use isReadableFile method in FileManager.

    if FileManager.isReadableFile(atPath: urlOutput.path) {
         NSWorkspace.shared.open(urlOutput)
        
    } else {
         //Show an Error Message
    }