在WKWebView swift中下载文档并加载图像(png,jpeg),pdf,doc等

问题描述 投票:0回答:1

我正在使用Alamofire从url下载文档,该文档可以是图像,pdf或doc等。下面的代码用于使用Alamofire下载并保存在文档目录中并加载到webview中。

func saveToDocumentDirectory(BillUrl: String) {
        let manager = Alamofire.Session.default
        let fileName = (BillUrl.components(separatedBy: "/").last) ?? ""
        MBProgressHUD.showAdded(to: self.view, animated: true)

        let destinationPath: DownloadRequest.Destination = { _, _ in
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
            let fileURL = documentsURL.appendingPathComponent(fileName)
            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }
        manager.download(BillUrl, method: .get, parameters: nil, headers: nil, to: destinationPath)
            .downloadProgress { (progress) in
                print("Temporary URL: ")
                print(progress.fractionCompleted)
        }
        .response { response in
            switch response.result {
            case .success(let pathUrl) :
                MBProgressHUD.hide(for: self.view, animated: true)
                print(pathUrl as Any)  //pathUrl = file:///var/mobile/Containers/Data/Application/C651C521-31A7-4512-A393-6A94442472A0/Documents/1_DlatFlsbZ1fJSs-_aIJmlg_1588075057_13.png

                self.displayAlert(path: pathUrl ?? URL(string: "")!)

            case .failure(let error) :
                MBProgressHUD.hide(for: self.view, animated: true)
                print(error)
                self.alert(message: "Something went wrong while proccessing your request", title: "Alert!")
            }
        }
    }

    fileprivate func displayAlert(path: URL) {
        let alertController = UIAlertController(title: "Alert!", message: "file downloaded at \(path)", preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default, handler: { action in  
            let webViewVC = storyBoard.instantiateViewController(withIdentifier: "WebViewVC") as! WebViewVC
            webViewVC.webUrl = path
            self.navigationController?.pushViewController(webViewVC, animated: true)
        })
        alertController.addAction(OKAction)
        self.present(alertController, animated: true, completion: nil)
    }

Webview类的以下代码

class WebViewVC: UIViewController, WKNavigationDelegate {

    var webView: WKWebView!
    var webUrl = URL(string: "")

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isHidden = false
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        webView.allowsBackForwardNavigationGestures = true
        webView.autoresizesSubviews = true
        webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        webView.load(URLRequest(url: webUrl!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 600.0))
    }

    override func viewDidLayoutSubviews() {
        webView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width , height: self.view.frame.size.height)
        view = webView
    }
}

pdf文件已正确加载到webview中,但是图像未正确加载到webview中,并且图像的顶部隐藏在导航栏的后面。enter image description hereenter image description here

我创建了WKWebView的出口并将其添加到self.view.addSubview(webView),但是每次它在webView = WKWebView(frame: .zero, configuration: webConfiguration)崩溃时我不知道下一步该怎么做。先感谢您。

ios swift webkit alamofire wkwebview
1个回答
0
投票

我们可以通过设置框架使其不与WKWebView重叠来创建UINavigationBar

import UIKit
import WebKit

class SecondViewController: UIViewController,UIWebViewDelegate,UIScrollViewDelegate, WKUIDelegate {

   var WKwebView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
               let userContentController: WKUserContentController = WKUserContentController()
               let conf = WKWebViewConfiguration()
               conf.userContentController = userContentController
               WKwebView = WKWebView (frame: CGRect( x: 0, y: 60, width: self.view.frame.width, height: self.view.frame.height - 60 ), configuration: WKWebViewConfiguration())
               WKwebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
               WKwebView.uiDelegate = self
               WKwebView.scrollView.bounces = false
               WKwebView.scrollView.delegate = self;
               view.addSubview(WKwebView)

                var request : URLRequest!
                let LOGINURL = "https://i.stack.imgur.com/RRy76.png";
                let url : URL = URL(string: LOGINURL as String)!
                request = URLRequest (url: url);
                WKwebView.load(request)

    }
}

输出Output without overlapping

© www.soinside.com 2019 - 2024. All rights reserved.