-
-
Save haikusw/a71dfd9793ad1fd5c00c88676c5721a2 to your computer and use it in GitHub Desktop.
How to hookup Sparkle in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@main | |
struct PastApp: App { // https://zeezide.de/en/products/past/ | |
var body: some Scene { | |
DocumentGroup(viewing: PastDocument.self) { file in | |
ContentView(document: file.$document) | |
} | |
.commands { | |
TextFormattingCommands() | |
AboutCommands() | |
SparkleCommands() // Just drop it in here | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SparkleCommands.swift | |
// Past for iChat | |
// | |
// Created by Helge Heß on 08.04.21. | |
// | |
import SwiftUI | |
#if SPARKLE && canImport(Sparkle) | |
import Sparkle | |
/** | |
* Update menu, if Sparkle is used. | |
*/ | |
struct SparkleCommands: Commands { | |
init() { | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { | |
SparkleCommands.setupUpdater() | |
} | |
} | |
var body: some Commands { | |
CommandGroup(after: .appInfo) { | |
Button("Check for Updates …") { | |
SparkleCommands.updater?.checkForUpdates() | |
} | |
} | |
} | |
fileprivate static var updater : SPUUpdater? | |
private final class SparkleDelegate: | |
NSObject, SPUUpdaterDelegate, SPUStandardUserDriverDelegate {} | |
private static let delegate = SparkleDelegate() | |
private static func setupUpdater() { | |
guard SparkleCommands.updater == nil else { return } | |
let updateDriver = SPUStandardUserDriver(hostBundle: .main, | |
delegate: delegate) | |
let updater = SPUUpdater(hostBundle: .main, applicationBundle: .main, | |
userDriver: updateDriver, delegate: delegate) | |
updater.automaticallyChecksForUpdates = true | |
updater.updateCheckInterval = 24 * 60 * 60 // every day | |
SparkleCommands.updater = updater | |
do { | |
try updater.start() | |
updater.checkForUpdatesInBackground() | |
} | |
catch { | |
print("ERROR: failed to start Sparkle updater:", error) | |
} | |
} | |
} | |
#else // No Sparkle | |
/** | |
* Sparkle is inactive, this will yield no commands. | |
*/ | |
struct SparkleCommands: Commands { | |
let body = EmptyCommands() | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment