Skip to content

olvrlrnz/BackgroundService

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BackgroundService

A GUI-less implementation of NSApplication

You can use a BackgroundService if you have an app bundle that uses the bundle format but does not have any graphical user interface.

Even though it would be possible to just remove unnecessary components, the system might still set up facilities that do not work in all situations, like linking to the AppKit framework, which is not daemon safe, as outlined in Technical Note 2083.

In those cases it might be beneficial to replace the usual application lifecycle with a BackgroundService.

How to use:

  • Create a new target of type macOS Application

  • Remove AppDelegate.swift and MainMenu.xib

  • Create a class that implements the BackgroundServicable protocol

        class MyBackgroundService: BackgroundServicable {
            init(arguments: [String]) {
                self.arguments = arguments
            }
        }
  • Add the class to the bundle's Info.plist and declare it UI-less

        <key>NSPrincipalClass</key>
        <string>$(MODULE_NAME).MyBackgroundService</string>
        <key>LSUIElement</key>
        <true/>
  • Remove the NSMainNibFile key from the Info.plist

  • Implement methods from BackgroundServiceDelegate you need

        extension MyBackgroundService: BackgroundServiceDelegate {
            func backgroundServiceDidFinishLaunching() {
                print("background service finished launching")
            }
    
            func backgroundServiceWillTerminate() {
                print("background service will terminate")
            }
    
            func backgroundServiceDidReceiveSignal(_ signal: Int32)
                print("background service received signal \(signal)")
            }
        }

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages