Search code examples
swiftuiwatchkitwatchwatchconnectivity

cannot find uiviewcontrollerrepresentable in scope


I have a WatchKit extension to my app and I need to modify the data in the content view with updates from the app on the phone. So I am using the code below... but the compiler errors out saying it can find UViewControllerRepresentable in scope

struct ContentView: View, UViewControllerRepresentable
  {
  
  
  var body: some View {
    VStack
      {
          ...
      }
  }

Solution

  • UIViewControllerRepresentable is not available for watch.

    https://developer.apple.com/documentation/swiftui/uiviewcontrollerrepresentable

    There is no need because WatchKit/WatchOS doesn't have UIViewControllers https://developer.apple.com/documentation/uikit/uiviewcontroller

    SwiftUI Views are available but you can't use UIKit's UIViews/UIViewControllers

    Whenever you have to redesign a View for a watchOS compatible version you can name it the same as the iOS target's version and give it the same init.

    If you keep the target memberships selected appropriately the compiler will chose the current target's version.