swift - 通过launchIsDefaultUserInfoKey启动应用程序时如何禁用重新打开文档
问题描述
在与 iOS 不同的 macOS 上,如果您想在启动时禁用重新打开文档,您需要依赖应用程序委托通知而不是较新的方法 - 使用选项参数,例如在 iOS 上:
applicationWillFinishLaunching(_:)
, 在这里你要实例化你的子类文档控制器
// We need our own to reopen our "document" urls
_ = DocumentController.init()
applicationDidFinishLaunching(_:)
,在这里您要检查提供的 userInfo
if let info = note.userInfo{
if let launchURL = info[NSApplication.launchIsDefaultUserInfoKey] as? String {
Swift.print("launchIsDefaultUserInfoKey: notif \(launchURL)")
disableDocumentReOpening = launchURL.boolValue
}
if let notif = info[NSApplication.launchUserNotificationUserInfoKey] as? String {
Swift.print("applicationDidFinishLaunching: notif \(notif)")
disableDocumentReOpening = true
}
}
所以当我的文档控制器被调用来进行文档恢复时,它会在应用程序委托中看到这个标志:var disableDocumentReOpening = false
.
func restoreWindow(withIdentifier identifier: NSUserInterfaceItemIdentifier, state: NSCoder, completionHandler: @escaping (NSWindow?, Error?) -> Void) {
if (NSApp.delegate as! AppDelegate).disableDocumentReOpening {
completionHandler(nil, NSError.init(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil) )
}
else
{
NSDocumentController.restoreWindow(withIdentifier: identifier, state: state, completionHandler: completionHandler)
}
}
但不幸的是,我有问题但是什么?在调试器中手动启动应用程序,似乎文档控制器恢复调用在应用程序委托的例程之前检查 userInfo。
我已经阅读了关于此的 SO帖子,显示了一个 Objective-C 代码片段,但标志是控制器本地的,但不明白如何拥有读/写类 var - 正如我所尝试的那样。也不理解它对类函数的使用,因为 doc 说它是一个实例方法,但尝试也没有奏效。
我错过了什么?
解决方案
推荐阅读
- javascript - 如何在不知道 puppeteer 的情况下抓取元素的值?
- javascript - 如何在 html div 的代码中显示 javascript 计时器?
- python - 如何使用 Beautiful Soup 从 HTML 中提取特定的脚本元素
- python - 如何在 Matplotlib 中向 Stack Percentage Barplot 添加注释
- android - 指定flutter使用的Android SDK
- csv - 从 CSV 文件中查找文件名列表,然后将它们复制到目录
- git - 当我的一个分支删除了我想保持删除的文件时,将 2 个分支合并到 master 的最佳方法是什么?
- mesibo - 如何将呼叫屏幕的自定义 ui 中的按钮链接到 mesibo sdk、来电和通话片段
- javascript - 使用 React 的自定义鼠标光标:未捕获的 TypeError:无法读取 null 的属性“clientWidth”
- firebase - 如何在 Vue 中设置身份验证状态更改?