WKWebView does load resources from local document folder

后端 未结 8 602
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 09:38

In my Swift iOS app, I want to download some dynamic HTML pages from a remote server, save them in the document directory, and display those pages from document directory.

8条回答
  •  误落风尘
    2020-11-28 10:09

    This is a simplified version of what I have used to load local files in a project of mine (iOS 10, Swift 3). I have just updated my code (7.5.2017) after testing it out again on iOS 10.3.1 and iPhone 7+ as requested by Raghuram and Fox5150 in the comments.

    I just created a completely new project and this is the folder structure:

    Update 19.04.2018: Added a new feature to download a .zip with HTML, CSS, JS files, unzip it in /Documents/ (Alamofire + Zip) and then load those files into the webView. You can find it in the GitHub sample project as well. Again, feel free to fork & star! :)

    Update 08.02.2018: finally added a GitHub sample project, which also includes a local JavaScript file. Feel free to fork & star! :)

    Version 1 with webView.loadFileURL()

    ViewController.swift

    import UIKit
    import WebKit
    class ViewController: UIViewController, WKNavigationDelegate {
        override func viewDidLoad() {
            super.viewDidLoad()
            let webView = WKWebView()
            let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
            let htmlUrl = URL(fileURLWithPath: htmlPath!, isDirectory: false)
            webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
            webView.navigationDelegate = self
            view = webView
        }
    }
    

    Version 2 with webView.loadHTMLString()

    ViewController.swift

    import UIKit
    import WebKit
    class ViewController: UIViewController, WKNavigationDelegate {
        override func viewDidLoad() {
            super.viewDidLoad()
            let webView = WKWebView()
            let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
            let folderPath = Bundle.main.bundlePath
            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
            }
            webView.navigationDelegate = self
            view = webView
        }
    }
    

    Gotchas to look out for:

    • Make sure that your local html/js/css files are in Project -> Target -> Build Phases -> Copy Bundle Resources
    • Make sure that your html files don't reference relative paths e.g. css/styles.css because iOS will flatten your file structure and styles.css will be on the same level as index.html so write instead

    Given the 2 versions and the gotchas here are my html/css files from the project:

    web/index.html

    
    
        
            
        Offline WebKit
        
      
      
        

    Offline WebKit!

    web/css/styles.css

    #webkit-h1 {
      font-size: 80px;
      color: lightblue;
    }
    

    If somebody wants a GitHub sample project, tell me in the comments section and I'll upload it.

提交回复
热议问题