get Firewall status from system preferences via Cocoa app

I am using the below code snippet to get the system preferences firewall status wheter it is ON/OFF using system .plist file.


func getFirewallStatus() -> Bool {
        let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .systemDomainMask, true)
        var path = paths[0]
        path = "\(path)/\("Preferences/com.apple.alf.plist")"
        path = path.replacingOccurrences(of: "/System", with: "")
        let _dictionary = NSDictionary(contentsOfFile: path)
        // firewall status
        let status = (_dictionary["globalstate"] as? NSNumber)?.intValue ?? 0
        if status == 0 {
            return false
        }
        return true
    }


Sandbox disable(Xcode): In this case, I am able to get the firewall status (On/Off).

Sandbox enable(Xcode): app is crashing when accessing the contents of file.


let _dictionary = NSDictionary(contentsOfFile: path)


Please let me know the reasons, why app is crashing in case of sandbox enable mode?


My intention is to get the status of firewall and not to update any system preferences config.


Thanks in advance!

The technique you’re using is not supported regardless of the state of the App Sandbox. Unless otherwise documented, Apple preferences files are not considered API. Their location and content can change at any time. You should not ship products that depend on them.

My intention is to get the status of firewall and not to update any system preferences config.

Unfortunately I don’t think that there’s any supported API for this. I encourage you to file an enhancement request for that. Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you and I filed an enhancment request for my query, here is the bug number: FB7614453

get Firewall status from system preferences via Cocoa app
 
 
Q