Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOpenPanel not open and not working in swift 4 OS x app

Swift 4 / Xcode 9.2 / OS X 10.12

I am trying to use a NSOpenPanel to get a jpg/png file with the following code, but not able to open panel for choose image, no crash, nothing happened.

let openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
openPanel.allowedFileTypes = ["jpg","png"]

openPanel.beginSheetModal(for: self.view.window!) { (result) in

}
like image 801
user014276 Avatar asked Feb 09 '18 07:02

user014276


2 Answers

Give your app access to user selected files under

Capabilities -> App Sandbox -> File Access -> User Selected File

enter image description here

like image 53
Patrick R Avatar answered Sep 22 '22 04:09

Patrick R


In Xcode 9 apps are sandboxed by default.

You have to enable Read/Write access of User Selected File in the Capabilities tab

enter image description here

like image 39
vadian Avatar answered Sep 20 '22 04:09

vadian