WKWebView on device cannot load cached HTML

I have a WKWebView that works fine in Simulator, but why only a white blank screen works on the device, this is my code:

override func loadView() { let webConfiguration = WKWebViewConfiguration() webConfiguration.preferences.setValue(true, forKey:"allowFileAccessFromFileURLs") webView = WKWebView(frame: .zero, configuration: webConfiguration) webView.uiDelegate = self view = webView } override func viewDidLoad() { super.viewDidLoad() let path2Esferas = Path.localPath.stringByAppendingPathComponent(path: "\(Path.DIR_IMAGES)\(ImagenDescargaTipo.esfera.rawValue)/\(desarrollo.id)/virtualtour.html") let url = URL(fileURLWithPath: path2Esferas) //NSURL.fileURL(withPath: path2Esferas) let myRequest = URLRequest(url: url) webView.load(myRequest) } 

The HTML in "path2Esferas" is in the cache folder that I download earlier if I put another URL, for example let myURL = URL(string: "https://www.apple.com") , like docs show, it works correctly in the simulator and device. For example: http://proyectoshm.com/esferas/real_de_mina/realdeminas.html

+5
source share
7 answers

After more than 1 year, I found a solution, we need to use loadFileURL to have access to local resources, here is my working code with WKWebView, plus I use loadHTMLString instead of load. vistaweb in my webview by the way

Thanks to this answer: Upload local web files & resources to WKWebView

  do { let local = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] // Loading: Library/Caches/repositorioLocal/esferas/znvUifZhH8mIDr09cX8j/index.html let resourceFolder = "repositorioLocal/esferas/znvUifZhH8mIDr09cX8j" let fileToLoad = "index.html" let urlToFolder = URL(fileURLWithPath: local).appendingPathComponent(resourceFolder) let urlToFile = URL(fileURLWithPath: local).appendingPathComponent(resourceFolder).appendingPathComponent(fileToLoad) let fileContent = try String(contentsOf: urlToFile) let webConfiguration = WKWebViewConfiguration() webConfiguration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs") webConfiguration.allowsInlineMediaPlayback = true vistaweb = WKWebView(frame: self.view.frame, configuration: webConfiguration) self.vistaweb.loadFileURL(urlToFolder, allowingReadAccessTo: urlToFolder) self.vistaweb.loadHTMLString(fileContent, baseURL: urlToFile) self.view.addSubview(vistaweb) } catch let error { print("Error: \(error.localizedDescription)") } 
0
source

INTRO

My solution DOES NOT SOLVE the complete problem.

I leave it here temporarily, it may be useful for understanding (mainly from our comments below) that it does not work.


 import UIKit import WebKit class ViewController: UIViewController { var webView:WKWebView? override func viewDidLoad() { super.viewDidLoad() let webConfiguration = WKWebViewConfiguration() self.webView = WKWebView(frame: self.view.frame, configuration: webConfiguration) // http request with URLRequest.CachePolicy.returnCacheDataElseLoad let request = URLRequest(url: URL(string:"http://proyectoshm.com/esferas/real_de_mina/realdeminas.html")!, cachePolicy: URLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 10) self.webView?.load(request) self.view.addSubview(self.webView!) } } 

I also added to plist:

App Transport Security Settings > Allow Arbitrary Loads in Web Content > YES App Transport Security Settings > Allow Arbitrary Loads > YES

in the end, in order to be able to send your application to the store, you will have problems:

Apple has announced that ATS will be REQUIRED for all applications from January 2017.

https://forums.developer.apple.com/thread/48979

enter image description here

result:

enter image description here

0
source

The problem is this phrase:

 frame: .zero 

Put some frame, even if it is a dummy value:

 frame: CGRect(x:0, y:0, width:100, height:100) 
0
source

If I understand from the comments and your code ( Path.localPath should be linked to your local resources ..), you should do with local files. I remember this should be due to your incorrect method for loading a local resource into WkWebView . In fact, since iOS 9.0 + , as docs explains, you can use the instance method loadFileURL(_:allowingReadAccessTo:)

You should also try using loadHTMLString :

 let path2Esferas = Path.localPath.stringByAppendingPathComponent(path: "\(Path.DIR_IMAGES)\(ImagenDescargaTipo.esfera.rawValue)/\(desarrollo.id)/virtualtour.html") let folderPath = Path.localPath.stringByAppendingPathComponent(path: "\(Path.DIR_IMAGES)\(ImagenDescargaTipo.esfera.rawValue)/\(desarrollo.id)") let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true) do { let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue) webView.loadHTMLString(htmlString as String, baseURL: baseUrl) } catch { // catch error } 

PS Make sure your local html / JavaScript / CSS files are added in Project -> Target -> Build Phases -> Copy Bundle Resources

In any case, remember that you can only load your local resources after the download is complete, so if you have asynchronous loading, make sure you finish it to use this code.

0
source

Instead of breaking into WKWebViewConfiguration with setting the private flag of the API allowFileAccessFromFileURLs try loading the file URL using the special file URL method:

 self.webView?.loadFileURL(url, allowingReadAccessToURL: url.deletingLastPathComponent) 
0
source

You tried to change your line

 let myRequest = URLRequest(url: url) 

to

 let myRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 100) 
0
source

Please check the network connection, if you are a different network with an html file, you do not see anything

0
source

Source: https://habr.com/ru/post/1274122/


All Articles