How to edit hosts file through macOS app?

Hi all,

I have created a macOS app, and would like to be able to edit(add a couple of lines) the /etc/hosts file.
Normally I do this by writing this in my terminal
Code Block
Sudo nano /etc/hosts

and then my password when it asks for it. After doing this I can freely edit the file.
So my question is, how can I do all of this through my macOS app?

I have tried the code bellow, but I keep getting this in my Xcode terminal
Code Block
zsh:1: permission denied: /private/etc/hosts


Code Block
let executableURL = URL(fileURLWithPath: "/bin/zsh")
do {
       try Process.run(executableURL,
                     arguments: ["-c", "echo myPassword | sudo -S echo 127.0.0.1 www.apple.com >> /private/etc/hosts"],
                    terminationHandler: { _ in self.isRunning = false })
     } catch {
            print(error)
      }


How to edit hosts file through macOS app?
 
 
Q