简体   繁体   English

自定义沙盒NSSavePanel警报

[英]Customizing sandboxed NSSavePanel alert

I am validating urls from NSSavePanel using the delegate's panel(_:validate) method, throwing error in case of invalid url. 我正在使用委托的面板(_:validate)方法验证来自NSSavePanel的URL,如果网址无效则抛出错误。 In such case the NSSavePanel presents an alert, which I want to customize (meaning present some human readable description) depending on the error thrown, keeping the save panel window open and then letting you choose another path. 在这种情况下,NSSavePanel会显示一个警报,我想根据抛出的错误自定义(意味着呈现一些人类可读的描述),保持保存面板窗口打开,然后让您选择另一个路径。

LocalizedError works just fine when not using App Sandbox but in a sandboxed app the getter for error description is never called and the message in the alert is generic "Operation couldn't be completed. (#yourErrorType)", which I guess is somehow caused by the different inheritance chain for sandboxed NSSavePanels. LocalizedError在不使用App Sandbox时工作得很好,但在沙盒应用程序中,永远不会调用错误描述的getter,并且警报中的消息是通用的“操作无法完成。(#yourErrorType)”,我猜这是以某种方式引起的由沙盒NSSavePanels的不同继承链。

I am struggling figuring a way around this - is it possible to customize the alert somehow while still keeping the app sandboxed? 我正在努力想办法解决这个问题 - 是否有可能以某种方式自定义警报,同时仍然保持应用程序沙箱?

Addendum : Permissions for User Selected File => r/w. 附录 :用户选择文件的权限=> r / w。 Running the following example produces different alerts with/without sandbox. 运行以下示例会生成带/不带沙箱的不同警报。

func runSavePanel()
{
    let panel = NSSavePanel()
    let delegate = SavePanelDelegate()
    panel.delegate = delegate
    _ = panel.runModal()
}

class SavePanelDelegate: NSObject, NSOpenSavePanelDelegate {
    func panel(_ sender: Any, validate url: URL) throws {
        throw CustomError.whatever
    }
}

enum CustomError: LocalizedError {
    case whatever

    var errorDescription: String? {
        get {
            return "my description"
        }
    }
}

So, after a bit of further digging I can tell the solution of the riddle finally although I can only guess the reasons why it was made tricky by Apple. 所以,经过一些进一步的挖掘后,我终于可以告诉谜语的解决方案了,尽管我只能猜出它为什么被苹果变得棘手的原因。 Apparently NSError exclusively needs to be used. 显然只需要使用NSError The customization has to be done in userInfo, say 例如,定制必须在userInfo中完成

let userInfo = [NSLocalizedDescriptionKey: "yourLocalizedDescription", NSLocalizedRecoverySuggestionErrorKey: "yourSuggestion"]
throw NSError(domain: "whatever", code: 0, userInfo: userInfo)

etc. By the way subclassing NSError doesn't work, the Sandbox will just happily ignore you :) 顺便说一下,子类化NSError不起作用,Sandbox会很高兴地忽略你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM