I will not be including the PDF file in the repo as I am not sure what license it has and if I am allowed to distribute it.īecause we are adding this PDF to our project we can use Bundle.main to get the file's URL. And that is just a miniature example of all the features your app can adopt with the powerful. I have named this file heaps.pdf, so in the code below you will see that I use the file name heaps. With PDFKit all is done with native code in a matter of minutes. To do that I have used the following PDF file. There are few simple UIKit methods for that. In order to do that we need to have a PDF file that we can add to our project. Create PDF Document using Swift In case you need to create a new PDF file on iOS device you won’t actually need to use PDFKit at all. Now that we know what we are doing, let's get to the code: Step 1: Get URL for local PDFĪs I mentioned earlier in this tutorial we will be focussing on how to open a local PDF file. You can also find the full source code for this tutorial here. Both methods that we are going to use today allows for us to use a URL when trying to load the PDF, so it doesn't matter whether the URL is a local file or not.Įven though this tutorial is not focussed on opening a remote PDF, I will be adding the code at the end of each section that will allow you to open a remote PDF file. If you want to use a hosted file it will work in basically the same way. The PDF can be a local file or a hosted file but we will be using a local file. In this tutorial we explore two ways of opening a PDF file with Swift. So in this article I’ll write about PDF specific stuff as the image gallery was a better example of how to scroll views efficiently.PDF files are common place and with iOS/iPadOS becoming a more powerful operating system, opening PDF files and other regular computer type tasks will become more common place. I wrote about scrolling images in a UIScrollView in another post Let’s start with the UIScrollView containing views with single PDF pages.Īs I mentioned before this was the solution I had to implement a few years ago, I cut some corners for the Swift porting as I don’t expect to use this in production now that iOS supports it in the framework. Using a Webview is so simple I didn’t feel the need to write the example, I’m sure there are tons of them available. Where I put two implementations: the scrollview with PDF pages and the one with PDFView. Recently I wanted to port my Objective-C code to Swift and I decided to share the implementation, so I created this GitHub project I started working on the custom solution back when we didn’t have WKWebViews nor PDF Kit, so my only alternatives were displaying PDFs in a UIWebView or implementing a custom solution. While its relatively easy to do, the PDFKit framework was only introduced in 2017. The third alternative is drawing PDF pages and place them on a UIScrollView. This blog post covered how to create and modify a PDF in Swift using PDFKit. In iOS 11 Apple introduced a new class PDFView, a subclass of UIView that comes with a configuration, so you can have side-by-side pages, vertical and horizontal scrolling and many more options. I said we have 3 ways to show the PDF and we just saw one, so what are the remaining two? The only downside is the scrolling is only vertical, which is fine for most apps but you may want to provide some customisations, for example the ability to scroll horizontally and to have two pages side-by-side in landscape. Just like loading a local page, or a remote one, you can provide the URL of the PDF and the WKWebView will take care of it. There are three possible ways of displaying a PDF. It could be a privacy policy document, an invoice, a flyer the company distributes on paper and via its app, a magazine. Showing PDFs is pretty common for an iOS app.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |