UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Unable to loadHTMLString

Forums > SwiftUI

Hi,

When I try to load HTMLString, I get the error "Referencing static method 'buildBlock' on 'Optional' requires that 'WKNavigation' conform to 'View'" How can I sort out this?

import SwiftUI
import WebKit

struct ViewPostView: View {
    @State var comments: [Comments] = []
    @State var newComment: String = ""

    let post: Posts
    var webView: WKWebView!

    var body: some View {

        VStack{

            webView.loadHTMLString(post.post_content, baseURL: nil)

            List(comments){comment in
                VStack(alignment: .trailing, spacing: 10){
                    HStack(spacing: 40){
                        Text(comment.author_name)
                        Text(comment.comment_date)
                    }

                    Text(comment.comment_content)
                }
            }
            .frame(alignment: .trailing)
            .onAppear {
                PostViewManager().getComments(intPostID: 1296){(comments) in
                    self.comments = comments
                }
            }

            TextField("Write your Comment", text: $newComment)
            Button(action: {
                if (self.newComment == "") {return}
                PostViewManager().makeNewComment(intPostID: self.post.id, strComment: self.newComment)
            }) {
                Text(/*@START_MENU_TOKEN@*/"Button"/*@END_MENU_TOKEN@*/)
            }
        }

        //.navigationBarTitle("Post Title")

    }
}

struct ViewPostView_Previews: PreviewProvider {
    static var previews: some View {
        ViewPostView(post: Posts(id: 1, title: "Placeholder", city_id: 1, city_name: "Placeholder", category_id: 1, category_name: "Placeholder", since: "Placeholder", author_id: "Placeholder", author_name: "Placeholder", post_content: "<h3>Content</h3>", featured_image: "logo"))
    }
}

2      

WKWebView is used with UIKit; you cannot use it in SwiftUI code. The VStack is expecting SwiftUi Views inside it and since WKWebView is not one, it errors.

You would need to make a SwiftUI View struct that wraps a WKWebView using UIViewRepresentable in order to use it in SwiftUI.

3      

This will load a WebView

import SwiftUI
import WebKit

struct WebView: UIViewRepresentable {
    var website: String

    func makeUIView(context: UIViewRepresentableContext<WebView>) -> WKWebView {
        let webView = WKWebView()
        if let url = URL(string: "https://" + website) {
            webView.load(URLRequest(url: url))
            webView.allowsBackForwardNavigationGestures = false
        }

        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WebView>) {

    }
}

then in ContentView

struct ContentView: View {
    let website = "hackingwithswift.com"

    var body: some View {
        WebView(website: website)
    }
}

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

hope that some help

3      

@NigelGee Thank you very much for the example. Is there a way to make the height of the webView to match the content height?

@roosterboy Thank you for the clarification.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.