我是新手,无法从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文件
答案 0 :(得分:1)
您正在尝试将http-Url加载到WebView中,这在iOS中是默认禁止的。您可以使用https-Url尝试相同的代码,也可以在Info.plist中更改传输安全设置以允许任意加载
另一个有用的想法可能是使用SFSafariViewController从另一个URL显示网站或文档。
答案 1 :(得分:0)
尝试下面的代码,让我知道
@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)
}
答案 2 :(得分:0)
如果您使用> = iOS 11,则可以使用苹果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
(但是您需要为此工作很多),或者找到第三方lib。
答案 3 :(得分:0)
为什么使用WKWebView
是因为可以使用最常见的方法,即使用UIDocumentInteractionController
和UIAlertController
检查这部分使用带有进度视图的流行Alamofire
的代码部分(您可以使用任何喜欢的库来实现类似功能,当然也可以使用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
}
}
答案 4 :(得分:0)
如果要在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()
}
}