SwiftUI and local HTML

How to use local HTML project (Hype) with SwiftUI (Xcode 11, Swift 5) project?
Any link to an absolute beginner?

I think you would still need to use a normal swift code inside a UIViewRepresentable view Struct

From an a test I did last year. Still works.

wkwebView SwiftUI UIViewRepresentable file

//
//  WKWebViewController.swift
//  fooBasic
//
//  Created by Mark Hunte on 03/09/2019.
//  Copyright © 2019 Mark Hunte. All rights reserved.
//

import UIKit
import WebKit
import SwiftUI



struct WkwebView: UIViewRepresentable {
   
   
    func makeUIView(context: Context) -> WKWebView {
           let wconfiguration = WKWebViewConfiguration()
                  
                  
                  
                  //-- false = Play video with native device player ; true =  inline
                  wconfiguration.allowsInlineMediaPlayback = false
                  
                  //-- Does not require user inter action for .ie sound auto playback
                  wconfiguration.mediaTypesRequiringUserActionForPlayback = []
                  
                  let webView =  WKWebView(frame: .zero, configuration: wconfiguration)
                  
                  let  theFileName = ("foo" as NSString).lastPathComponent
                  let htmlPath = Bundle.main.path(forResource: theFileName, 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
           
                  webView.scrollView.bounces = false
                  webView.scrollView.isScrollEnabled = false
                  webView.isOpaque = false
                  webView.isHidden = false
           
                  return    webView
                         
                     }
       
       
       func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WkwebView>) {
        
       }
    
     
}

//–SwiftUI view file

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
           Text("Hype Test")
            WkwebView()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
3 Likes

It works and it is a good starting point.
Many thanks.

1 Like