Download, Store, and View PDF in Swift

I’ve been working on an App that requires me to download PDF files, store it, and to view it. To accomplish the task, there are three main topics:

  • DownloadTask
  • File Management
  • PDFView

DownloadTask

To download the file from an URL, we need to use downloadTask . Since we also need to tack where the location had the task have placed our files, the observer, which is ViewController in my example, needs to conform to URLSessionDownloadDelegate .

To see where the downloaded file is located, simply inspect the location printed in the Xcode console.

After I pressed download, the file downloaded persisted less than a second, and then it was killed by the system. This behavior behaves the same on both simulator and physical machine.

File Management

Each App created in iOS has its own sandbox. Inside the sandbox, there are three main portions that iOS developers should know: Bundle Container, Data Container, and iCloud Container. Here I am going to only specify on Data Container to stay focus on our task — download PDF.

reference: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW4

Data container is the space where the your code can manipulate the files downloaded from the Internet after the is compiled. Below am I listing the important features:

  • Files inside Library and tmp will be cleaned up by iOS automatically.
  • iTunes will backup all the files in Data Container except Caches, tmp and files specifying .isExcludedFromBackup = true . During App review, if Apple finds out that unneeded files being backed up in iTines, it’s likely the App will be rejected.
  • Documents is the location to store downloaded files.

Hence, the next step is to copy the file from tmp to Documents. I’ve done this by:

  1. extracting the original pdf name,
  2. creating a url in Documents,
  3. deleting file with the same name to avoid Copy Error: “CFNetworkDownload_mdrFNb.tmp” couldn’t be copied to “Documents” because an item with the same name already exists. , and
  4. copying it to Documents.

PDFView

Now we’ve successfully placed the downloaded PDF into the proper place for users to access. It’s time to view it with PDFView from PDFKit, which is a convenient framework provided by Apple since the release of iOS 11.

Even though many tutorials of PDFKit uses storyboard to create PDFView by assigning the property to a UIView, it does not support in Xibs. Thus here I’m going to instantiate it programmatically.

Wa-Lah!!! Now we’ve fetched the opened the PDF successfully 😀. I know the size doesn’t fit, and I’ll be focusing on PDFKit in the next article. Enjoy!