Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 445
ScottyBlades Avatar asked Oct 19 '25 15:10

ScottyBlades


2 Answers

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
like image 121
lorem ipsum Avatar answered Oct 22 '25 20:10

lorem ipsum


Alternatively

This is how I separate it for both platforms. Easy to use.

import Foundation

import SwiftUI

#if os(macOS)

public typealias NativeView = NSView
public typealias NativeApplication = NSApplication
public typealias ViewRepresentable = NSViewRepresentable
public typealias ViewControllerRepresentable = NSViewControllerRepresentable

#elseif os(iOS)

public typealias NativeView = UIView
public typealias NativeApplication = UIApplication
public typealias ViewRepresentable = UIViewRepresentable
public typealias ViewControllerRepresentable = UIViewControllerRepresentable

#endif

Example Usage

struct ExampleView: ViewRepresentable {
    // ...
}
like image 40
mgyky Avatar answered Oct 22 '25 19:10

mgyky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!