Created
July 18, 2020 10:26
-
-
Save lalkrishna/71a8834bda61d01d15abb32839c89ee9 to your computer and use it in GitHub Desktop.
Checking for Update in GitHub project release
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
struct UpdateManager { | |
let updateURL = "https://api.github.com/repos/{user_name}/{repo_title}/releases/latest" | |
static let shared = UpdateManager() | |
private init() { } | |
func checkForUpdates(updateAvailable: @escaping (String?) -> Void) { | |
let session = URLSession(configuration: .default) | |
let updateTask = session.dataTask(with: URL(string: updateURL)!) { (data, response, error) in | |
DispatchQueue.main.async { | |
guard let data = data else { | |
updateAvailable(nil) | |
return | |
} | |
guard let release = try? JSONDecoder().decode(Release.self, from: data) else { | |
updateAvailable(nil) | |
return | |
} | |
guard let infoPlist = Bundle.main.infoDictionary else { | |
updateAvailable(nil) | |
return | |
} | |
guard let installedVersion = infoPlist["CFBundleShortVersionString"] as? String else { | |
updateAvailable(nil) | |
return | |
} | |
guard let installedBuildNumber = infoPlist["CFBundleVersion"] as? String else { | |
updateAvailable(nil) | |
return | |
} | |
let installedVersionToCompare = "\(installedVersion).\(installedBuildNumber)" | |
//Check Version and Build Number | |
let result = (installedVersionToCompare.compare(release.tagName, options: .numeric) == .orderedAscending) | |
if result { | |
updateAvailable(release.htmlURL) | |
} else { | |
updateAvailable(nil) | |
} | |
} | |
} | |
updateTask.resume() | |
} | |
} | |
struct Release: Codable { | |
let id: Int | |
let url: String | |
let name: String | |
let body: String | |
let htmlURL: String | |
let tagName: String | |
enum CodingKeys: String, CodingKey { | |
case id | |
case url | |
case name | |
case body | |
case htmlURL = "html_url" | |
case tagName = "tag_name" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment