Swift + OS X sandboxing: treat "NSVBOpenPanel" as "NSOpenPanel" :: since I need to get the sender in the delegation method

Im using swift and I show NSOpenPanel. As a delegate, I need to look at the sender’s hint to determine what action to take:

eg.

func show() {
    ... 
    panel.delegate = self
    panel.prompt = "xy"

    panel.run ....
}

func show2() {
    ... 
    panel.delegate = self
    panel.prompt = "abc"

    panel.run ....
}

//delegate
func panel(sender: AnyObject, shouldEnableURL url: NSURL) -> Bool {
    let panelPrompt = (sender as! NSOpenPanel).prompt       ... 
}
  • no sandbox = WORKS fine

    • delegate sender - this is NSOpenPanel really
  • with sandbox = crash, crash

    • the delegate sender is NOT an NSOpenPanel, but an NSVBOpenPanel. A private apple class that remotely talks to the outside world and allows the user to select files NORMALLY in your sandbox. (see Apple Sandbox Guide for details).

, , , ?
/ idk
performSelector?

===

: NSOpenPanel !

+2
2

, NSOpenPanel ( , NSVBOpenPanel), performSelector, , AnyObject , :

func panel(sender: AnyObject, shouldEnableURL url: NSURL) -> Bool {
    let panelPrompt = sender.prompt ?? ""
    // ...
    return true
}

sender, prompt . .

. AnyObject Swift , .

+3

performSelector. :

let panelPromptUnmanaged = (sender as! NSObject).performSelector(NSSelectorFromString("prompt"))
let panelPrompt = panelPromptUnmanaged != nil ? panelPromptUnmanaged.takeRetainedValue() as! String : ""
0

Source: https://habr.com/ru/post/1015952/


All Articles