如何直接从 URL 快速打开 pdf 文件
Posted
技术标签:
【中文标题】如何直接从 URL 快速打开 pdf 文件【英文标题】:How to open pdf file in swift directly from URL 【发布时间】:2019-12-12 07:47:43 【问题描述】:我是 swift 新手,无法从 url 打开 pdf 文件
我的代码是这样的
@IBOutlet var webview: WKWebView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
webview.navigationDelegate = self
let url = URL(string: "http://www.orimi.com/pdf-test.pdf")
self.webview.load(URLRequest(url: url!))
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!)
print("Start loading")
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
print("End loading")
我正在使用此代码,我可以打开 url 链接和图像,但无法打开 pdf 文件
【问题讨论】:
运行上面的代码会发生什么 @user1374 它什么也没显示。 如果 WKWebView WKNavigationDelegate 是否设置了委托 【参考方案1】:如果您使用 >= ios 11,则可以使用 Apples PDFKit
。这里是例子。
import PDFKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
let pdfView = PDFView(frame: view.bounds)
view.addSubview(pdfView)
if let url = URL(string: link_of_pdf), let document = PDFDocument(url: url)
pdfView.document = document
但是如果你需要低于 iOS 11 的支持,你可以使用CGPDFDocument
(但你需要做很多工作),或者找到第三方库。
【讨论】:
如果 URL 已将 pdf 更改为 Web URL 会怎样 你也可以使用网址【参考方案2】:您正在尝试将 http-Url 加载到 iOS 默认禁止的 WebView 中。您可以使用 https-Url 尝试相同的代码或更改 Info.plist 中的传输安全设置以允许任意加载
另一个有用的想法可能是使用 SFSafariViewController 从另一个 url 显示网站或文档。
【讨论】:
【参考方案3】:试试下面的代码告诉我
@IBOutlet weak var webView: WKWebView!
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(true)
self.initialSetup()
private func initialSetup ()
webView.navigationDelegate = self
self.loadhtmlStringImage()
private func loadHTMLStringImage() -> Void
let htmlString = "<p>Identify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the imagesIdentify the arrow-marked structures in the images</p>"
webView.loadHTMLString(htmlString, baseURL: nil)
【讨论】:
【参考方案4】:你为什么使用WKWebView
,因为你可以使用最常用的方法,那就是使用UIDocumentInteractionController
和UIAlertController
检查这部分使用流行的Alamofire
和progressview 的代码(你可以使用任何你喜欢的类似功能的库,当然也可以使用URLSession
)。
注意如果你在你的控制器中使用它,它必须实现委托UIDocumentInteractionControllerDelegate
。
这里是带有下载功能和进度条的完整源代码:
class MyController: UIViewController, UIDocumentInteractionControllerDelegate
var progressView: UIProgressView?
override func viewDidLoad()
super.viewDidLoad()
func downloadCommentFile(id: Int)
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
//MARK: Progress controller
let alertView = UIAlertController(title: "Downloading", message: "Downloading file", preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
// Show UIAlertController it to your users
present(alertView, animated: true, completion:
// Add your progressbar after alert is shown (and measured)
let margin:CGFloat = 8.0
let rect = CGRect(x: margin, y: 72.0, width: alertView.view.frame.width - margin * 2.0 , height: 10.0)
self.progressView = UIProgressView(frame: rect)
self.progressView!.progress = 0
self.progressView!.tintColor = self.view.tintColor
alertView.view.addSubview(self.progressView!)
)
let url = "http://MyUrl/DownloadFile"
let headers = ["Header1": "header 1 value"]
Alamofire.download(url,
method: .post,
parameters: ["id": id],
encoding: JSONEncoding.default,
headers: headers,
to: destination).downloadProgress(closure: (progress) in
//progress closure
self.progressView?.setProgress(Float(progress.fractionCompleted), animated: true)
).response(completionHandler: (DefaultDownloadResponse) in
//here you able to access the DefaultDownloadResponse
//result closure
alertView.dismiss(animated: true, completion:
let viewer = UIDocumentInteractionController(url: DefaultDownloadResponse.destinationURL!)
viewer.delegate = self
viewer.presentPreview(animated: true)
)
)
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController
return self
private func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController!) -> UIView!
return self.view
func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect
return self.view.frame
【讨论】:
【参考方案5】:如果你想在 WKWebView 中加载你的 pdf 文件,你可以使用下面的代码:-
import UIKit
import WebKit
class WebViewController: UIViewController
@IBOutlet weak var webView: WKWebView!
didSet
webView.navigationDelegate = self
override func viewDidLoad()
super.viewDidLoad()
loadWebView()
override func viewWillDisappear(_ animated: Bool)
super.viewWillDisappear(true)
hideLoader()
private func loadWebView()
showLoader()
let request = URLRequest(url: URL(string: "http://www.orimi.com/pdf-test.pdf)!)
DispatchQueue.main.async
self.webView.load(request)
private func showLoader()
DispatchQueue.main.async
//show your loader
private func hideLoader()
DispatchQueue.main.async
//hide your loader
extension WebViewController: WKNavigationDelegate
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)
hideLoader()
showAlert(error.localizedDescription)
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
hideLoader()
【讨论】:
以上是关于如何直接从 URL 快速打开 pdf 文件的主要内容,如果未能解决你的问题,请参考以下文章