Post

Replies

Boosts

Views

Created

Does UIScreen work with mac catalyst?
In the documentation, UIScreen supports Mac Catalyst https://developer.apple.com/documentation/uikit/uiscreen/ but when I actually do UIScreen.screens with mac catalyst, I only get the main screen and cannot detect the external display. import SwiftUI struct ContentView: View {       @EnvironmentObject var externalDisplayContent: ExternalDisplayContent       var body: some View {     VStack {       Button("Display"){         print(UIScreen.screens)       }     }   }     } struct ContentView_Previews: PreviewProvider {       static var previews: some View {     ContentView()   }     }
1
0
1.2k
Mar ’22
How do I use NotificationCenter in mac catalyst to detect and connect an external display?
I can successfully connect and show ExternalView() on the external display on IOS target. However, mac catalyst is unable to detect the display connection in NotificationCenter. How do I use NotificationCenter in mac catalyst to detect and connect an external display? import Combine import SwiftUI @main struct ScreensApp: App {   @ObservedObject var externalDisplayContent = ExternalDisplayContent()   @State var additionalWindows: [UIWindow] = []   private var screenDidConnectPublisher: AnyPublisher<UIScreen, Never> {     NotificationCenter.default       .publisher(for: UIScreen.didConnectNotification)       .compactMap { $0.object as? UIScreen }       .receive(on: RunLoop.main)       .eraseToAnyPublisher()   }   private var screenDidDisconnectPublisher: AnyPublisher<UIScreen, Never> {     NotificationCenter.default       .publisher(for: UIScreen.didDisconnectNotification)       .compactMap { $0.object as? UIScreen }       .receive(on: RunLoop.main)       .eraseToAnyPublisher()   }   var body: some Scene {     WindowGroup {       ContentView()         .environmentObject(externalDisplayContent)         .onReceive(           screenDidConnectPublisher,           perform: screenDidConnect         )         .onReceive(           screenDidDisconnectPublisher,           perform: screenDidDisconnect         )     }   }   private func screenDidConnect(_ screen: UIScreen) {     print("connected")     let window = UIWindow(frame: screen.bounds)     window.windowScene = UIApplication.shared.connectedScenes       .first { ($0 as? UIWindowScene)?.screen == screen }       as? UIWindowScene     let view = ExternalView()       .environmentObject(externalDisplayContent)     let controller = UIHostingController(rootView: view)     window.rootViewController = controller     window.isHidden = false     additionalWindows.append(window)     externalDisplayContent.isShowingOnExternalDisplay = true   }   private func screenDidDisconnect(_ screen: UIScreen) {     print("disconnected")     additionalWindows.removeAll { $0.screen == screen }     externalDisplayContent.isShowingOnExternalDisplay = false   } }
2
0
910
Mar ’22