Only a small part of AppKit API is available to Mac Catalyst, but since Mac Catalyst apps are also macOS apps, they do have full access to AppKit API in runtime. It’s just not visible to them because Apple marked most of the AppKit classes as unavailable to Catalyst apps.

In this article, we’ll see how we can access AppKit API from Mac Catalyst apps in two different ways and in pure Swift.

A. Using Dynamic

Image for post

Image source: Author

Dynamic is a library that allows us to access Objective-C code from Swift in a simple and clean way. I wrote this library because I wanted a simple way to access hidden and private API from Swift without needing to create a separate plugin or use message-sending tricks that make the code uglier and harder to understand.

1. Add Dynamic as a Swift package dependency

Image for post

2. Access AppKit API

That’s it! Now, you can access all of AppKit API using a very simple and clean syntax that is very similar to what you’d do if the API was public.

For example, this is how we enter fullscreen from a macOS app vs. Catalyst with Dynamic:

// macOS App
	NSApplication.shared
	        .windows.first?
	        .toggleFullScreen(nil)

	// Mac Catalyst (with Dynamic)
	Dynamic.NSApplication.sharedApplication
	        .windows.firstObject
	        .toggleFullScreen(nil)

#programming #swift #xcode #ios #macos

How to Access the AppKit API From Mac Catalyst Apps
9.50 GEEK