macos swift helper app not working when launched automatically

Viewed 20

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 is changed to "connected"
(it's also meant to send the request every time you're connected to internet, but it only fires once)

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() // this is supposed to fire every time the app is connected to the internet
            }
        }
        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
    }
}
1 Answers

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, instead of the code being outside of the function so that it only loads once.

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()
    }
}

Related