Reputation: 11
I'm trying to setup an auto-update feature using Sparkle in my Mac app using SwiftUI. I keep getting an Unable to check for Updates error. Updae Error Image
I wasn't able to figure out how to fix it based on the documentation and I've not tried to set anything like this up before.
I'm not sure if I messed something up in my swift code or if my .xml file has an error. Or maybe something else. I've attached some of my code below.
Swift file:
final class CheckForUpdatesViewModel: ObservableObject {
@Published var canCheckForUpdates = false
init(updater: SPUUpdater) {
updater.publisher(for: \.canCheckForUpdates)
.assign(to: &$canCheckForUpdates)
}
}
struct CheckForUpdatesView: View {
@ObservedObject private var checkForUpdatesViewModel: CheckForUpdatesViewModel
private let updater: SPUUpdater
init(updater: SPUUpdater) {
self.updater = updater
// Create our view model for our CheckForUpdatesView
self.checkForUpdatesViewModel = CheckForUpdatesViewModel(updater: updater)
}
var body: some View {
Button("Check for Updates…", action: updater.checkForUpdates)
.disabled(!checkForUpdatesViewModel.canCheckForUpdates)
}
}
xml file:
<rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>MacLibrary Update</title>
<link>http://sparkle-project.org/files/sparkletestcast.xml</link>
<description>Most recent changes with links to updates.</description>
<language>en</language>
<item>
<title>Version 2.0</title>
<link>https://themaclibrary.com</link>
<sparkle:version>2.0</sparkle:version>
<description>
<![CDATA[ <ul> <li>Added support for auto-update</li> <li>Search apps now available</li> </ul> ]]>
</description>
<pubDate>Sun, 12 Mar 2023 15:20:11 +0000</pubDate>
<enclosure url="https://github.com/doorhinge-apps/The-Mac-Library/releases/download/v2.0-beta.3/MacLibrary.app.zip" length="3090000" type="application/octet-stream" sparkle:edSignature="0BE06q4Xll6v8BrvQIRTNnd700piitM/P5eteZJ1P1B+TDiU2x981+CDwY25tY8YrSwMt7ZGyoXQd92tbESGnX3c3ZmrKo+3OhtjxGTi14AqokyJrrcWkPSXdQisUjRx=="/>
</item>
</channel>
</rss>
Thanks!
Upvotes: 1
Views: 473
Reputation: 1
func isUpdateAvailable() throws -> Bool {
guard let info = Bundle.main.infoDictionary,
let currentVersion = info["CFBundleShortVersionString"] as? String,
let identifier = info["CFBundleIdentifier"] as? String,
let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(identifier)") else {
throw VersionError.invalidBundleInfo
}
let data = try Data(contentsOf: url)
guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else {
throw VersionError.invalidResponse
}
if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String {
return version != currentVersion
}
throw VersionError.invalidResponse
}
enum VersionError: Error {
case invalidResponse, invalidBundleInfo
}
DispatchQueue.global().async {
do {
let update = try self.isUpdateAvailable()
DispatchQueue.main.async {
// show alert
}
} catch {
print(error)
}
}
try using this method works better , no need to use sparkle pod
Upvotes: 0