swiftuiuiviewrepresentable

Cannot find type 'UIViewRepresentable' in scope


import SwiftUI
#if canImport(WebKit)
import WebKit
#endif

struct WebView: UIViewRepresentable {

    var url: URL

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

I created my app as a multiplatform app. I added this view. However I'm getting a couple errors that I can't figure out.

Cannot find type 'UIViewRepresentable' in scope Cannot find type 'Context' in scope

I cleared my build folder. My minimum deployment targets are as follows:

enter image description here

I want this to run for the following schemes:enter image description here enter image description here


Solution

  • UIViewRepresentable
    

    Is for iOS and MacCatalyst

    NSViewRepresentable
    

    Is for macOS

    Make sure that you are checking the target, you are likely building for macOS.

    #if os(iOS)
         UIViewRepresentable code
    
    #endif