Search code examples
swiftxcodeswiftuinswindowvisionos

AppKit and NSWindow are inaccessable in VisionOS project


XCode project, Version 15.2 VisionOS

I am trying to obtain the width and height of a VisionOS window that has been resized by the user. The best class I found to help me with this is NSWindowDelegate (child of NSWindow class).

To use NSWindowDelegate, I must import AppKit. But I receive the message "No such module 'AppKit'".

Does VisionOS not have AppKit? Is there another import required for the use of NSWindow classes on VisionOS? If not, how can one obtain the dimensions of an already existing window in VisionOS?

Thank you for any input.

Here is my code:

import AppKit //"No such module 'AppKit'"


class CustomWindowManager: NSWindowController, NSWindowDelegate { //"Cannot find type 'NSWindowController' or 'NSWindowDelegate' in scope"

       // Resize Methods

       func windowWillResize(sender: NSWindow,toSize frameSize: NSSize) -> NSSize { //"Cannot find type 'NSSize' or 'NSWindow' in scope"

            // my code goes here
       }

       func windowDidResize(notification: NSNotification) {

           // my code goes here
       }
}

Solution

  • No, AppKit is explicitly macOS. See https://developer.apple.com/documentation/appkit It says: macOS 10.0+, Mac Catalyst 13.0+. visionOS is not mentioned, hence it's not supported.

    To observe changes in window size using SwiftUI only, you could use this approach: Observe window resize event with SwiftUI