Files
sparkle-project.github.io/documentation/programmatic-setup/index.md
T
Mayur Pawashe d80c1f8b8d Add minor documentation changes (#80)
* Fix programatic/programmatic typo
* Refer to programmatic setup in upgrading section instead of recommending SPUUpdater
* Update sparkle-cli usage
* Update .gitignore to ignore Jekyll cache files
2021-08-21 11:55:20 -07:00

2.0 KiB

layout, id, title
layout id title
documentation documentation Setting up Sparkle programmatically

Create an updater programmatically

Here is an example of creating an updater in Sparkle 2 (beta) with Cocoa programmatically. It hooks up a menu item's target/action for checking for updates.

import Cocoa
import Sparkle

@NSApplicationMain
@objc class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet var checkForUpdatesMenuItem: NSMenuItem! // Hooked up in Interface Builder

    let updaterController = SPUStandardUpdaterController(startingUpdater: false, updaterDelegate: nil, userDriverDelegate: nil)

    func applicationDidFinishLaunching(_ notification: Notification) {
        checkForUpdatesMenuItem.target = updaterController
        checkForUpdatesMenuItem.action = #selector(SPUStandardUpdaterController.checkForUpdates(_:))

        // SPUStandardUpdaterController was instantiated by not starting the updater automatically
        updaterController.startUpdater()
    }
}

In Sparkle 2 you can also choose to instantiate and use SPUUpdater directly instead of the SPUStandardUpdaterController wrapper if you need more control over your user interface or what bundle to update.

If you use another UI toolkit, these are the relevant APIs in Sparkle 2 for checking for updates and handling menu item validation:

  • -[SPUStandardUpdaterController startUpdater] or -[SPUUpdater startUpdater:] for starting the updater (unless it is specified to be automatically started)
  • -[SPUStandardUpdaterController checkForUpdates:] or -[SPUUpdater checkForUpdates] for checking for updates
  • -[SPUUpdater canCheckForUpdates] for menu item validation

If you are using Sparkle 1, you will need to use these APIs:

  • +[SUUpdater sharedUpdater] for creating and starting the updater automatically
  • -[SUUpdater checkForUpdates:] for checking for updates
  • -[SUUpdater validateMenuItem:] for menu item validation

Check Sparkle's APIs for more information.