Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load excel / pdf server url in WKWebView Swift4

I wanted to load the excel sheet url/pdf url (document url) in WKWebView. As I am trying to open that url from browser then it directly downloads that document. So I am confused whether 1] I have to directly load the url into WKWebView or 2] first download those files and then load that local path in WKWebView.

If I go with the 2nd option then will my document download every time? Following is the code which I have tried but it shows nothing

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
    }
}

1 Answers

Try with this code

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)

    }
}
like image 120
Prajakta Avatar answered Jan 26 '26 11:01

Prajakta