首页 > 解决方案 > 通过launchIsDefaultUserInfoKey启动应用程序时如何禁用重新打开文档

问题描述

在与 iOS 不同的 macOS 上,如果您想在启动时禁用重新打开文档,您需要依赖应用程序委托通知而不是较新的方法 - 使用选项参数,例如在 iOS 上:

//  We need our own to reopen our "document" urls
_ = DocumentController.init()
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 说它是一个实例方法,但尝试也没有奏效。

我错过了什么?

标签: swiftmacos

解决方案


推荐阅读