Hacker News new | past | comments | ask | show | jobs | submit login
Create a Screensaver for macOS with Swift (betterprogramming.pub)
3 points by codetrotter on Sept 18, 2021 | hide | past | favorite | 3 comments



According to a discussion on the Apple developer forums, it's not a good idea to create a screensaver using Swift. However, the code from the OP blog post Works For Me™, and I enjoy writing Swift and I don't like Objective-C, so I decided to do like they do in the OP blog post and use Swift anyways.

The discussion on the Apple developer forums is about three years old, so I'm not sure if it still applies. But beware of it if you decide to write a screensaver in Swift too:

> Don’t do this. The current screen saver architecture uses an NSBundle-style plug-in. It’s not safe to create such plug-ins in Swift unless you control all the plug-ins and the app loading those plug-ins, and that’s clearly not the case here. The problem is one of ABI compatibility. If the app loading the plug-ins loads two different plug-ins, each of which is linked to its own Swift runtime, you’ll have two Swift runtimes running in the same process and that will be Bad™.

> Note that this does not apply to modern plug-ins, which are all app extensions. An app extension can be safely written in Swift because it runs in its own process.

> If you’d like to write a screen saver in Swift, I encourage you to file an enhancement request for the system to add support for app extension-based screen savers. Please post your bug number, just for the record.

https://developer.apple.com/forums/thread/89482?answerId=268...

A little further down, someone also said:

> Just to let everybody know: Some code angel at Apple fixed the specific issue in one of the latest dot releases of macOS High Sierra! Yay!

> [...]

> The issues described by Quinn still exist in general, but at least with only one (used) Swift plugin/saver within the System Preferences it works again.

https://developer.apple.com/forums/thread/89482?answerId=305...


Also, another thing worth mentioning is that the miniature preview of the screensaver from the OP blog post will show a black screen and nothing else. But it runs perfectly fine other than that. Don't know what to make of that, or how to get it to show the miniature preview.


Commenting again to add that it looks like the reason I wasn't seeing anything in the thumbnail may have been because I kept the "Desktop & Screen Saver" window with the thumbnail open while working on it and installing new versions of it. So the missing thumbnail may have been an artifact of the thumbnail showing an earlier version of the code or something, even though it was correctly showing the latest version when running the full screen version. It may also have been related to the Info.plist data of the project. Not sure. But if anyone else experiences the same thing, try to navigate back to the main "System Preferences" view and into the "Desktop & Screen Saver" view again and the thumbnail might update. But if you still run into an all-black thumbnail preview then more investigation will be necessary.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: