swift: Calling "/usr/bin/defaults" returns no data

I'd like to create a small helper app for new students do read/write User default settings.

Since it was not possible using the UserDefaults class I decided to use the "/usr/bin/defaults". Unfortuntely it seems not to return anything. Debug output shows "Got data: 0 bytes"

Here is a sample code:

import SwiftUI

func readDefaults(domain : String, key :String) -> String {
    let cmdPath = "/usr/bin/defaults"
    //let cmdPath = "/bin/ls"

    let cmd  = Process()
    let pipe = Pipe()
    cmd.standardOutput = pipe
    cmd.standardError  = pipe
    
    cmd.executableURL = URL(fileURLWithPath: cmdPath, isDirectory: false, relativeTo: nil)
    cmd.arguments = ["read", domain, key]
    //cmd.arguments = ["/", "/Library"]

    print("Shell command: \(cmdPath) \(cmd.arguments?.joined(separator: " ") ?? "")")
    var d : Data?
    do {
        try cmd.run()
        d = pipe.fileHandleForReading.readDataToEndOfFile()
        cmd.waitUntilExit()
    } catch let e as NSError {
        return "ERROR \(e.code): \(e.localizedDescription)"
    } catch {
        return "ERROR: call failed!"
    }
    
    // get pipe output and write is to stdout
    guard let d else {
        return "ERROR: Can't get pipe output from command!"
    }
    print("Got data: \(d)")
    if let s = String(data: d, encoding: String.Encoding.utf8) {
        print("Got result: \(s)")
        return s
    } else {
        return "ERROR: No output from pipe."
    }
}


struct ContentView: View {
    let foo = readDefaults(domain: "com.apple.Finder", key: "ShowHardDrivesOnDesktop")
    
    var body: some View {
        VStack {
            Text("ShowHardDrivesOnDesktop: \(foo.description)")
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

This code works well e.g. for "ls" when the comments are changed for cmdPath and cmd.arguments.

What do I miss in order to get it working with defaults?

Strange: In my test application I removed "Apple Sandbox" and now it works here. Using the same settings it does not work in my real app.

I guess there is a wrong Xcode setting for my target. Need to investigate…

Thanks for your post. I have a question about your post.

Since it was not possible using the UserDefaults class

Why is not possible to use the UserDefaults?

https://developer.apple.com/documentation/foundation/userdefaults

Thanks

Albert Pascual
  Worldwide Developer Relations.

swift: Calling "/usr/bin/defaults" returns no data
 
 
Q