How to display pdf on iOS

后端 未结 12 1149
半阙折子戏
半阙折子戏 2020-12-12 18:30

I want to know if there is any way for a offline build for xcode iOS such that we can display pdf file from local file.

The method I\'m using now is vi

12条回答
  •  醉话见心
    2020-12-12 19:09

    we are iOS coders, no JS/Web programmer, so stay in swift and Apple APIs. The faster, cleaner, way is to use Quicklook.

    I quote calimarkus.

    Anyway code is very simple:

    //  Copyright © 2019 ing.conti. All rights reserved.
    //
    
    import UIKit
    import QuickLook
    
    
    class ViewController: UIViewController, QLPreviewControllerDataSource {
    
        var previewController:QLPreviewController?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            self.previewController = QLPreviewController()
            previewController!.dataSource = self
            present(previewController!, animated: true)
    
        }
    
        //QL delegate:
    
        func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
            return 1
        }
    
    
        func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    
            guard let url = Bundle.main.url(forResource: "SAMPLE", withExtension: "pdf") else {
                fatalError("Could not load pdf")
            }
    
            return url as QLPreviewItem 
        }
    }
    

提交回复
热议问题