How to display pdf on iOS

后端 未结 12 1111
半阙折子戏
半阙折子戏 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:03

    I advise you using the QuickLook framework while handling PDF files.

    0 讨论(0)
  • 2020-12-12 19:04
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Yourpdf" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    [[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]];
    [webView loadRequest:request];
    [self.view addSubview:webView];
    
    0 讨论(0)
  • 2020-12-12 19:06

    SWIFT 4.x

    UIWebview approach in swift:

        //let url = URL.init(string: "https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf")! // for file in web
        let url = Bundle.main.url(forResource: "filename", withExtension: ".pdf")! // for file in bundle
        let webView = UIWebView.init(frame: view.frame)
        webView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        webView.loadRequest(URLRequest.init(url: url))
        view.addSubview(webView)
    
    0 讨论(0)
  • 2020-12-12 19:07

    There is a new toolkit IOs has introduced. It has many dedicated features related to displaying PDFs. You can use PdfView class in it to display a PDF either from a local file or via a URL.

    PDFKit > PDFView

    0 讨论(0)
  • 2020-12-12 19:08

    You can also use UIDocumentInteractionController !

    let docController = UIDocumentInteractionController(url: URL(fileURLWithPath: path)) documentInteractionController.presentPreview(animated: true)

    0 讨论(0)
  • 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 
        }
    }
    
    0 讨论(0)
提交回复
热议问题