helper app doesn't seem to be doing anything on login

I'm making an app that's meant to automatically log into school internet, and I'm trying to make it so that you save the credentials in the main app, and the helper app gets registered to launch at login, which works. However, when the helper app opens from logging in, it doesn't send a web request to log into the internet, but when I open the helper app manually, it does. Keep in mind that it's meant to send a request whenever the internet status changed to "connected"

import Cocoa
import Network

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    let defaults = UserDefaults.init(suiteName: "replaced userdefaults group name thing")

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let usernamestored = defaults!.string(forKey: "username")!
        let passwordstored = String(decoding: kread(service: "detnsw-autologin", account: usernamestored)!, as: UTF8.self) // kread() refers to a function i have in another file for reading values from keychain

        let url = URL(string:"the login page url")
        guard let requestUrl = url else { fatalError() }
        var request = URLRequest(url: requestUrl)
        request.httpMethod = "POST"
        let poststring = "csrfmiddlewaretoken=&username=\(usernamestored)&password=\(passwordstored)"
        request.httpBody = poststring.data(using: String.Encoding.utf8)

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if let error = error {
                print("Error took place \(error)")
                return
            }
            if let data = data, let _ = String(data: data, encoding: .utf8) {
                //print("Response data string:\n \(dataString)")
                //print(response.statusCode)
            }
        }

        let monitor = NWPathMonitor()
        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                task.resume()
            }
        }
        let queue = DispatchQueue(label: "Monitor")
        monitor.start(queue: queue)
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
        return true
    }
}
Answered by circularsprojects in 723417022

so I've fixed the issue by putting all of the code that loads the username and password into the function that runs when the internet is connected, so the final code looks something like this:

monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                let usernamestored = defaults!.string(forKey: "username")!
                ...
                let task = URLSession.shared.dataTask(with: request) { (data, response, error) in {...}
                task.resume()
            }
        }
Accepted Answer

so I've fixed the issue by putting all of the code that loads the username and password into the function that runs when the internet is connected, so the final code looks something like this:

monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                let usernamestored = defaults!.string(forKey: "username")!
                ...
                let task = URLSession.shared.dataTask(with: request) { (data, response, error) in {...}
                task.resume()
            }
        }
helper app doesn't seem to be doing anything on login
 
 
Q