

This is declared with a ? so that it can be nil, as it will be when a document is first created, and doesn’t have to be initialised when the document is created. So the NSDocument class has to contain a PDFDocument as a variable. Sometimes you can merge classes, and create a composite which uses NSDocument and another class like PDFDocument, but here you can’t do that. First, because you are going to use PDFKit code, you need to import Quartz, which contains PDFKit. There are several changes which need to be made here. By returning false, your app will only open document windows when told to. The way to fix that is to add the function applicationShouldOpenUntitledFile() as shown above.
#Apple pdfkit tutorial pdf#
In the leftmost sidebar of the window, select the AppDelegate.swift file to view and edit its source.īecause this app is a PDF viewer, you don’t want it to keep opening new, empty documents whenever it starts up.
#Apple pdfkit tutorial code#
Having set the project and app configuration, it’s time to add the first source code to the Swift files in your project. There, set the macOS Deployment Target to 10.12 or whatever you set it above. Then select the Project item in the sidebar, above the Targets. As this is going to be a document viewer rather than editor, you could change its Role here, but leaving it set as Editor makes it easier if you do develop it into something bigger.

Because you entered earlier as the document extension, this has been completed for you, but is worth checking and tweaking.Ĭhange the Name to PDF, and add the UTI Identifier. In this case, the important section is that listing Document Types, which you should open. This is where you edit what will end up in your app’s all-important ist. If you’re going on to have your app notarized, this is a good time to turn Hardened Runtime on, though. Leaving this turned on by accident is a common way to make your app behave most oddly. Here, turn off the App Sandbox unless you have aspirations to the App Store and are peculiarly masochistic. Then select the Capabilities section at the top. Set Version to 1.0b1, the Signing section to use your certificate as appropriate, and the Deployment Target to 10.12 or whatever you want. This should open the main window with the Target settings ready to edit. Navigate to an appropriate folder to save your new project, and click Create.

This workthrough assumes that you already have Xcode 10.1 installed, and that you have set it up with some sort of developer ID and certificate(s). The idea is simple: glue together features in AppKit and PDFKit, and let them do all the hard work.
#Apple pdfkit tutorial how to#
Rather than offer something pointless, this is a good chance to show how to use PDFKit and make a real, fully-functional PDF viewer app with hardly any code at all. Several have asked me to step through building an app in Xcode 10.
