0

Now I am working on mac osx app development. I need to check if os is up to date or outdated programmatically. I can see it via Software Update of System Preferences. There are a lot of solutions to get current mac os version. But I am not interested in version. Only interested in if os is the latest one or old one.

enter image description here

But I want to know it in my app programmatically. swift code or objective-c code is fine.

JS Guru
  • 343
  • 1
  • 3
  • 11
  • 2
    In the shell run `softwareupdate --list` and parse the result. – vadian Jan 28 '20 at 11:30
  • Yes, it is. But you are able to read data asynchronously from the shell with `NSTask`. – vadian Jan 28 '20 at 16:37
  • @vadian softwareupdate --list is helpful for me As you know, this shell command is async $ softwareupdate --list Software Update Tool Finding available software No new software available. what I want to get as result is the messages under "Finding available software". import SwiftShell do { let command = runAsync("softwareupdate", "--list") try command.finish() let result = command.stdout.read() print(result) } catch { } then the result is below Software Update Tool Finding available software I can not get "No new software available" – JS Guru Jan 28 '20 at 16:43
  • @vadian Could you provide me the code snippet getting full results of "softwareupdate --list"? – JS Guru Jan 28 '20 at 16:46
  • Please look at the [Ray Wenderlich Tutorial](https://www.raywenderlich.com/1197-nstask-tutorial-for-os-x) – vadian Jan 28 '20 at 17:03
  • @vadian I did it by myself. I will share my code as answer. – JS Guru Jan 28 '20 at 19:26

1 Answers1

1

Below is simple code snippet but I spent a lot of time to get it done. I hope someone does not waste time like me.

import SwiftShell

func getIsOSXUpdate() -> Bool {
    // softwareupdate --list|tee
    let command = runAsync("softwareupdate", "--list")
    do {
        try command.finish()
    } catch {
        NSLog("could not collect available updates.")
        return false
    }

    let result = command.stdout.read()
    let error = command.stderror.read()

    if error.contains("No new software available") {
        return true
    }
    return false
}
JS Guru
  • 343
  • 1
  • 3
  • 11