mhdhejazi / Dynamic

Call hidden/private API in style! The Swift way.
https://medium.com/@mhdhejazi/calling-ios-and-macos-hidden-api-in-style-1a924f244ad1
Apache License 2.0
695 stars 36 forks source link

Big Sur breaks code #7

Open polymerchm opened 3 years ago

polymerchm commented 3 years ago

The following code (use to position and size the main window after launch) now crashes

delay(0.1) { let ns = self.window?.nsWindow Dynamic(ns!).setFrame(CGRect(200,200,1200,800), display: true) let frame = Dynamic(ns!).frame.asCGRect! let size = frame.size Dynamic(ns!).setAspectRatio(CGSize(1.0, size.height/size.width))

}

with the following error

2020-11-17 21:42:22.832850-0600 ChordCalc Mac[11229:361126] Metal API Validation Enabled WARNING: Trying to access an unrecognized member: UINSWindowProxy.setFrame:display:

mhdhejazi commented 3 years ago

Yeah, hostWindowForUIWindow() now returns UINSWindowProxy on macOS 11. We need to call UINSWindowProxy.attachedWindow to get the actual window:

extension UIWindow {
    var nsWindow: NSObject? {
        var nsWindow = Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self)
        if #available(macOS 11, *) {
            nsWindow = nsWindow.attachedWindow
        }
        return nsWindow.asObject
    }
}

Let me know if it works for you.

gabors commented 3 years ago

Adding the attachedWindow works on BigSur, but if #available(macOS 11, *) also resolves to true on Catalina.

I ended up with this for now:


extension UIWindow
{
    public var nsWindow: Dynamic
    {
        if ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 11, minorVersion: 0, patchVersion: 0))
        {
            return Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self).attachedWindow
        }
        else
        {
            return Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self)
        }
    }
}