0

I am trying to build a SwiftUI tvOS app. As you can see here, I am trying to create a SwiftUI View using a UIViewControllerRepresentable, specifically for the DDDevicePickerViewController.

However, I noticed that there is no DDDevicePickerViewControllerDelegate while I was trying to implement it, which is needed according to Paul Hudson's tutorial. How can I use the DevicePickerView in SwiftUI?

I tried to use this code to create it, so when I use it, I just get a black screen with no errors in the logs:

import Foundation
import SwiftUI
import DeviceDiscoveryUI

public struct DDevicePickerView: UIViewControllerRepresentable {
    let viewController: DDDevicePickerViewController

    public init() {
        // Create the view controller for the device picker.
        let devicePicker = DDDevicePickerViewController(browseDescriptor: .applicationService(name: "TicTacToe"),
                                                              parameters: applicationServiceParameters())
        self.viewController = devicePicker!
    }

    public func makeUIViewController(context: Context) -> DDDevicePickerViewController {
        let gkVC = viewController
        return gkVC
    }

    public func updateUIViewController(_ uiViewController: DDDevicePickerViewController, context: Context) {
        return
    }
}
10
  • Also related to this question: stackoverflow.com/questions/74647385/… Commented Dec 15, 2022 at 13:27
  • 1
    Having a delegate is not necessary for a UIViewRepresentable. That tutorial only shows a delegate because the image picker has it. You should update your question to show what you have tried and how it wasn't working if you ran into actual problems. Commented Dec 15, 2022 at 13:32
  • @DávidPásztor thanks for the feedback, updated with code, and result of code Commented Dec 15, 2022 at 13:37
  • 1
    That UIViewControllerRepresentable implementation is not correct Commented Dec 15, 2022 at 21:35
  • @malhal what's wrong with it? Can you please share a resource I can follow? Commented Dec 15, 2022 at 22:55

1 Answer 1

1

SwiftUI already has a wrapper DevicePicker

But if you want to wrap it yourself, start with something like this and then you just have to figure out how to get the async endpoint result. It is quite unusual to have view controllers be async like this.

import Foundation
import SwiftUI
import DeviceDiscoveryUI

public struct DevicePicker: UIViewControllerRepresentable {
    @Binding var isPresented: Bool

    public func makeUIViewController(context: Context) -> UIViewController {
        UIViewController()
    }

    public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        if isPresented {
             if uiViewController.presentedViewController != nil {
                 return
             }
             
             let = picker DDDevicePickerViewController(browseDescriptor: .applicationService(name: "TicTacToe"), parameters: applicationServiceParameters())
              uiViewController.present(picker, animated: !context.transaction.disablesAnimations)
        }
        else {
            uiViewController.presentedViewController?.dismiss()
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

! Thanks a lot. I was getting a runtime non crash error (stackoverflow.com/questions/26022756/…) at uiViewController.present so I had to wrap it in DispatchQueue.main.async {...}. Either way the app gets stuck on a black screen after the present -,- So that did do anything either It might be that its waiting for the let endpoint = try await picker.endpoint to do some work? There's no documentation anywhere of this being done in swiftUI but I might have to give up
Yeh working around this async thing is going to be a challenge. I found one example on Github but it's for UIKit github.com/TheFirstPrototype/AppleTV-NetworkDiscovery-Swift/…
yeah that’s my code lol 😂 It’s from the WWDC example code which uses UIKit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.