swift - 文档类型关联在应用程序未运行时导致应用程序崩溃

标签 swift xcode cfbundledocumenttypes

我创建了自己的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/

相关文章:

ios - 尝试获取 UITableViewCell 的 indexPath 时出现错误

ios - CFBundleDocumentTypes -- 在 'Photos' 中列出我的应用程序

swift "ambiguous use of operator ' >'"

ios - 如何在表头中不实现 SearchBar

ios - 这个 Xcode 项目文件夹层次结构最可能的设计模式是什么?

ios - 应用程序在 "Open in.."菜单中显示为 "Copy to MyApp"

ios - 您可以动态分配 CFBundleDocumentTypes(使用私有(private) API 吗?)

ios - iOS框架找不到Texture Atlas

ios - 如何获得另一个 NSDate 当天特定时间的 NSDate 表示?

swift - 如何在 Xcode (Swift) 中添加自动完成注释?