UIDocumentInteractionController veya QuickLook kullanarak yapabilirsin.
web'de bulunan bir pdf dosyalarının indirilmesi ve görüntülenebilmesi için basit bir örnek hazırladım.
Önemli: Örneğin çalışabilmesi için "info.plist" içerisine aşağıdaki parametreyi eklemelisin.
App Transport Security Settings
Allow Arbitrary Loads = YES
//
// ViewController.swift
// pdfDisplayer
//
// Created by Yasin TURKOGLU on 13.04.2018.
// Copyright © 2018 Yasin TURKOGLU. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {
let actionButton:UIButton = {
let button = UIButton(type: .custom)
button.backgroundColor = .blue
button.setTitle("Download and display PDF", for: .normal)
button.addTarget(self, action: #selector(displayPDFAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let UIDocumentInteractionControllerPDFPresenter:UIDocumentInteractionController = {
let interactionController = UIDocumentInteractionController()
return interactionController
}()
override func viewDidLoad() {
super.viewDidLoad()
self.UIDocumentInteractionControllerPDFPresenter.delegate = self
self.view.addSubview(self.actionButton)
NSLayoutConstraint.activate([
self.actionButton.heightAnchor.constraint(equalToConstant: 50.0),
self.actionButton.widthAnchor.constraint(equalToConstant: 250.0),
self.actionButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
self.actionButton.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
self.view.layoutIfNeeded()
}
@objc func displayPDFAction() {
print("Downloading and displaying process has been started")
UIApplication.shared.isNetworkActivityIndicatorVisible = true
downloadAndDisplayPDFFromURL(string: "https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf") { (error) in
if let receivedError = error {
print("An error occurred:\(receivedError.localizedDescription)")
} else {
print("Successfully downloaded and presented")
}
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
func downloadAndDisplayPDFFromURL(string: String, _ completed: @escaping(Error?)->Void) {
DispatchQueue.global(qos: .background).async {
if let pdfURL = URL(string: string) {
do {
let pdfData = try Data(contentsOf: pdfURL)
let documentsDirectoryURL = try FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("Sample.pdf")
try pdfData.write(to: documentsDirectoryURL, options: .atomic)
DispatchQueue.main.async {
self.UIDocumentInteractionControllerPDFPresenter.url = documentsDirectoryURL
self.UIDocumentInteractionControllerPDFPresenter.presentPreview(animated: true)
completed(nil)
}
} catch {
completed(error)
}
}
}
}
//MARK: UIDocumentInteractionControllerDelegate methods
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}