Document type association crashing the App when it's not running

209 views Asked by At

I have created my own PDF Viewer and want to associate the pdf file format with it so that i can open a pdf file from Finder, Terminal or with Drag & Drop. If I start the app and then try to open a pdf in one of the 3 ways mentioned before the document it's displayed correctly, but if i do the same thing when the App it's not open then it crashes.

Here my CFBundleDocumentTypes settings (from the info.plist file):

<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>

Here where the crash occur from the crash report:

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

I am also using a custom pdf icon but it's not displayed even if a set my App as the preferred one to open all pdf files.

1

There are 1 answers

0
Gianni On

I have solved the problem. Basically the crash occur in the application(_:openFile:) method, where i was calling my mainViewController before it was instantiated. The solution was to move the code in the applicationWillFinishLaunching(_:) which is sent to the Delegate of the Application before the application(_:openFile:) and not in the applicationDidFinishLaunching(_:) which is sent after.