'Get computer informations from Swift [closed]

With Swift I'd like to get a string with the indicated informations about a computer running my application, like:

My mac informations popup

  1. OS version
  2. Machine type
  3. Processor and memory (installed and free)

Googled all around but seems a clear answer doesn't exist.



Solution 1:[1]

You should only ask one question at a time but I'll try to summarize:

  1. To get the operating system version

    ProcessInfo.processInfo.operatingSystemVersionString

  2. To get the machine type will not be so easy. You can get only the Model Identifier AFAIK MacPro5,1. You would need to use the terminal command

    sysctl hw.model

  3. Processor I think you will need the terminal command as well.

    sysctl -n machdep.cpu.brand_string
    sysctl -n machdep.cpu.core_count

  4. To get the physical memory. I am not sure if you can get the free memory as well.

    ProcessInfo.processInfo.physicalMemory


To use the terminal command in Swift you can do something like:

extension DataProtocol {
    var string: String? { String(bytes: self, encoding: .utf8)  }
}

extension Process {
    static func stringFromTerminal(command: String) -> String {
        let task = Process()
        let pipe = Pipe()
        task.standardOutput = pipe
        task.launchPath = "/bin/bash"
        task.arguments = ["-c", "sysctl -n " + command]
        task.launch()
        return pipe.fileHandleForReading.availableData.string ?? ""
    }
    static let processor = stringFromTerminal(command: "machdep.cpu.brand_string")
    static let cores = stringFromTerminal(command: "machdep.cpu.core_count")
    static let threads = stringFromTerminal(command: "machdep.cpu.thread_count")
    static let vendor = stringFromTerminal(command: "machdep.cpu.vendor")
    static let family = stringFromTerminal(command: "machdep.cpu.family")
}

Usage:

Process.processor

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1