Open PDF File Using Swift

Open PDF file using swift

If you simply want to view a PDF file you can load it into a UIWebView.

let url : NSURL! = NSURL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf")
webView.loadRequest(NSURLRequest(URL: url))

Swift 4.1 :

let url: URL! = URL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf")
webView.loadRequest(URLRequest(url: url))

If you'd like to achieve more, a good framework is PSPDFKit.

How to open pdf file in swift directly from URL

You are trying to load a http-Url into a WebView which is prohibited by default in iOS. You can try the same code with an https-Url or change your Transport Security Settings in the Info.plist to allow Arbitrary loads

Another useful idea might be to use SFSafariViewController to show a website or document from another url.

How to open a locally saved PDF file with Swift

NSWorkspace can do that.

let url = URL(fileURLWithPath: "/Volumes/NAS/Advertising Department/16_SCRIPTS/*Instructions/2_Running Scripts/1_InDesign/Layout Ad Script Instructions.pdf")
NSWorkspace.shared.open(url)

How do I view a PDF file saved in my document directory using Swift?

You can use PDFKit.amazing tool for PDF viewing https://developer.apple.com/documentation/pdfkit

import PDFKit

class TextPreviewController: BaseController {

@IBOutlet weak private var pdfView: PDFView!

override func viewDidLoad() {
super.viewDidLoad()
setupView()
}

private func setupView() {
let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("Owl.jpg")
guard let path = myurl,
let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path))else { return }
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
}

}

you just need to put you link on PDF and it will be open.

how to view pdf files in collectionview?

You can try to implement the PDFKit framework in your app. This framework has been available since IOS 11.

Create an extra class in you project called "PDFView" and assign the CollectionViewCell (in which you are trying to display the PDF) to this class. Then you can setup the loading of the PDF file in your newly created "PDFView" class.

For more information over the implementation please see this question.

Cannot open PDF using xcode for my iphone app

Ok, the issue was I was using ".open" when creating the document picker instead of ".import". That did the trick.



Related Topics



Leave a reply



Submit