如何在WKWebView Swift4中加载excel / pdf服务器网址

时间:2018-08-28 08:53:10

标签: ios swift wkwebview

我想在WKWebView中加载Excel工作表URL / PDF URL(文档URL)。当我尝试从浏览器打开该URL时,它将直接下载该文档。因此,我感到困惑是1)是否必须直接将URL加载到WKWebView还是2)首先下载这些文件,然后在WKWebView中加载该本地路径。

如果我选择第二个选项,那么每次都会下载我的文档吗? 以下是我尝试过的代码,但未显示任何内容

import UIKit
import WebKit

class OpenReportsFullScreenVC: UIViewController,WKUIDelegate {

    var webView : WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //first way :- webView.load(URLRequest(url: URL(string: "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls")!))
        // second way
        let filePathURLData =  "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls"
        let fileURL = URL(fileURLWithPath: filePathURLData )
        webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)

        }

    override func loadView() {
        //initialise webview
        let webViewConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
        webView.uiDelegate = self
        view = webView
    }
}

2 个答案:

答案 0 :(得分:2)

文件路径不能像示例中那样包含空白字符。

答案 1 :(得分:2)

尝试使用此代码

import UIKit

import WebKit

class ViewController: UIViewController , **WKNavigationDelegate**{

    var webView : WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()            
        let myBlog = "your URl"  
        let url = NSURL(string: myBlog)!
        let request = NSURLRequest(url: url as URL)

        webView = WKWebView(frame: self.view.frame)
        webView.navigationDelegate = self
        webView.load(request as URLRequest)
        self.view.addSubview(webView)
        self.view.sendSubview(toBack: webView)

    }
}