我创建了自己的PDF 查看器,并希望将 pdf 文件格式与其关联,以便我可以从Finder、终端 或使用拖放。如果我启动应用程序,然后尝试以文档之前提到的 3 种方式之一打开 pdf,它会正确显示,但如果我在应用程序未打开时执行相同的操作,则会崩溃。
这是我的 CFBundleDocumentTypes
设置(来自 info.plist 文件):
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>pdf</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>pdfformat</string>
<key>CFBundleTypeName</key>
<string>pdf</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>giannigianino.Mirror</string>
</array>
<key>LSTypeIsPackage</key>
<integer>1</integer>
</dict>
</array>
这里是崩溃报告中发生崩溃的地方:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 username.Mirror 0x0000000106a4d6e2 _T06Mirror11AppDelegateC11applicationSbSo13NSApplicationC_SS8openFiletF + 1090
1 username.Mirror 0x0000000106a4d749 _T06Mirror11AppDelegateC11applicationSbSo13NSApplicationC_SS8openFiletFTo + 89
2 com.apple.AppKit 0x00007fff9f355bd3 -[NSApplication _doOpenFile:ok:tryTemp:] + 322
3 com.apple.AppKit 0x00007fff9f1407df -[NSApplication(NSAppleEventHandling) _openDocumentURLs:withCompletionHandler:] + 996
我还使用自定义 pdf 图标,但即使将我的应用程序设置为打开所有 pdf 文件的首选图标,它也不会显示。
最佳答案
我已经解决了这个问题。基本上崩溃发生在 application(_:openFile:)
方法,我在其中调用我的 mainViewController
在它被实例化之前。解决方案是将代码移至 applicationWillFinishLaunching(_:)
中在 application(_:openFile:)
之前发送给应用程序的代表而不是在applicationDidFinishLaunching(_:)
中这是在之后发送的。
关于swift - 文档类型关联在应用程序未运行时导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50475854/