iOS
UIWebView
PDF
Swift
Mobile Development

Can we open pdf file using uiwebview on iOS?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

You could historically display PDFs with UIWebView, but modern iOS development should not use it because the API is deprecated and removed from current best practice. Supported alternatives are WKWebView, PDFKit, and QuickLook. Choosing among them depends on how much PDF interaction your product needs.

Why UIWebView Is No Longer the Right Tool

UIWebView is legacy technology with long-term maintenance and compliance risk. Even if old code paths still exist in archived projects, modern iOS apps should migrate.

Reasons to avoid it:

  • deprecated API lifecycle
  • weaker maintainability compared with modern frameworks
  • limited future compatibility

So the practical answer is yes in old history, but no for current production architecture.

Option 1: WKWebView for Lightweight PDF Display

If you just need read-only rendering with minimal customization, WKWebView is simple and effective.

swift
1import UIKit
2import WebKit
3
4final class PdfWebViewController: UIViewController {
5    private let webView = WKWebView(frame: .zero)
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9        webView.frame = view.bounds
10        webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
11        view.addSubview(webView)
12
13        if let url = Bundle.main.url(forResource: "guide", withExtension: "pdf") {
14            webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
15        }
16    }
17}

This works well for in-app help documents and simple previews.

Option 2: PDFKit for Native PDF Features

If you need advanced behavior such as page thumbnails, text selection, annotations, or custom zoom handling, use PDFKit.

swift
1import UIKit
2import PDFKit
3
4final class PdfKitController: UIViewController {
5    private let pdfView = PDFView(frame: .zero)
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9
10        pdfView.frame = view.bounds
11        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
12        pdfView.autoScales = true
13        view.addSubview(pdfView)
14
15        if let url = Bundle.main.url(forResource: "guide", withExtension: "pdf") {
16            pdfView.document = PDFDocument(url: url)
17        }
18    }
19}

For document-centric apps, this is usually the best long-term choice.

Option 3: QuickLook for Fast Preview UX

When you want native preview behavior with low implementation effort, QLPreviewController is useful.

swift
1import QuickLook
2
3final class FilePreviewController: QLPreviewController, QLPreviewControllerDataSource {
4    var fileURL: URL!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        dataSource = self
9    }
10
11    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
12        return 1
13    }
14
15    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
16        return fileURL as QLPreviewItem
17    }
18}

This is strong for attachment previews and document-list flows.

Local and Remote File Handling

PDF rendering quality also depends on file lifecycle handling:

  • bundle documents for static help files
  • download remote files to app sandbox before rendering
  • validate content type and file extension for remote data
  • handle missing or corrupt files with clear UX fallback

For enterprise apps, add retention and cleanup policy for downloaded documents to avoid storage growth and compliance issues.

Performance and Security Considerations

Large PDFs can stress memory on older devices. Test with realistic file sizes and low-memory conditions.

Security considerations:

  • avoid rendering untrusted remote content directly from temporary URLs
  • store sensitive files in protected directories
  • avoid logging raw file contents

If PDFs contain personal data, ensure document lifecycle and deletion behavior aligns with your data policy.

Migration Strategy from Legacy Code

If your app still has old UIWebView paths:

  1. inventory every PDF rendering entry point.
  2. replace simple viewer paths with WKWebView or QuickLook.
  3. replace advanced viewer paths with PDFKit.
  4. run regression tests for navigation, rotation, and file-not-found behavior.

Gradual migration by feature area often works better than one large rewrite.

Common Pitfalls

Keeping legacy UIWebView usage in active production path increases future upgrade risk.

Choosing WKWebView for workflows that actually require PDF-native features leads to custom workaround complexity.

Ignoring remote-file validation can expose app to malformed content and unstable rendering.

Skipping large-file memory tests causes crashes that only appear on older devices.

Summary

  • UIWebView is legacy and should not be used for modern iOS PDF features.
  • Use WKWebView for simple viewing, PDFKit for advanced interaction, and QuickLook for quick previews.
  • Handle local and remote file paths explicitly with robust error handling.
  • Test performance on realistic document sizes and device classes.
  • Plan migration from legacy paths as a controlled, test-driven rollout.

Course illustration
Course illustration

All Rights Reserved.