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
I advise you using the QuickLook framework while handling PDF files.
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];
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)
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
You can also use UIDocumentInteractionController !
let docController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
documentInteractionController.presentPreview(animated: true)
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
}
}