Post not yet marked as solved
Can you explain more about what sort of product you’re creating?
OK, the app im developing will only run in the macs of the company i work at. The idea is for this to be a daemon that runs all the time and periodically sends data about the system to an api (some of this data i can only access as root). This app also comes with a Network Extension that uses the Content Filter apis to serve as a firewall.
Like I said earlier, one of the Timers i have repeats every 3 min and sends data to an api, based on this data the api tells my daemon what to do, wether it should download an update or update the current firewall rules used by the Net Ext. I was talking to @meatton about this and he suggested that I should merge my daemon and extension and perform all this repeating tasks inside the extension. I am against this idea for now because extensions cant be restarted using a simple kill command so for troubleshooting it can be sort of dangerous.
I understand that using an app as a daemon is not ideal but I don't really think that has anything to do with the problems I am facing. Also, even if I absolutely had to separate the daemon from this App i would just end up with a blank helper application that has nothing in it, no UI, no code, etc.
Lastly, given that the app is an enterprise solution it is ditributed via MDM.
Post not yet marked as solved
Yes it is installed in /Library/LaunchDaemons.
BTW, what I said earlier about NSWorkspace notifications does not seem to be a reliable solution to my problem.
Post not yet marked as solved
You can’t have an app that is a launchd daemon.
Oh i didn't know that. It works perfectly fine though because the app has no UI and is very simple. I found out that I can get notified about when the system sleeps/wakes with this code and this seems that have helped me solve this issue.
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(recieveSleepNotification), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(recieveWakeNotification), name: NSWorkspace.didWakeNotification, object: nil)
If so, where is that installed and what does that look like?
?xml version="1.0" encoding="UTF-8"?
!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"
plist version="1.0"
dict
keyLabel/key
stringcom.company.myapp/string
keyProgram/key
string/Applications/myapp.app/Contents/MacOS/myapp/string
keyKeepAlive/key
true/
keyRunAtLoad/key
true/
/dict
/plist
Im not using Mac Catalyst I just build the app in Xcode.
After opening a TSI I decided to refactor my code and avoid using XPC given that it was not the best option for what I wanted to do. I wanted to send data from the Extension to the App and then have the App send that data to an api but it is best to have the Extension make the requests so it doesn't depend on the App.
Post not yet marked as solved
Ok i have some questions now. Given that my app is a Launch Daemon with no UI at all does that mean it is always running in the background? If it isnt how can i detect when the app sleeps or gets back up, should I use this methods applicationDidBecomeActive , applicationWillResignActive or are they not appropriate for my use case?
Post not yet marked as solved
Thanks a lot for the quick response. Does this still apply for MacOS apps or is it only for IOS?
I don't believe applicationwillenterbackground is avaliable on MacOS. How could i stop the Timers when the system sleeps and restart them when it gets back up?
The mentioned error dissapears when adding this to the NE entitlements
keycom.apple.security.temporary-exception.files.absolute-path.read-write/key
array
string/private/var/db/mds//string
string/usr/libexec//string
/array
I think what is described in this post - https://developer.apple.com/forums/thread/126437 is exactly what im looking for. Unfortunately i can't figure out how to implement it so i will open a TSI.
Is it possible that I need to reverse things and set up the xpc listener on the app and connect from the extension?
Does anyone have an example code I could see to help me figure this out?
Turns out i can get the path with SecCodeCopyPath, when I manage to get the command line arguments of the process I will share how I did it.
Or did you mean that you are creating a LaunchDaemon that works along side your Network System Extension?
Yeah that is what i meant, sorry.
Basically what the extension does is collect data about the outbound flow generated by unsigned apps and when it stores a certain amount it sends them to the main app which then sends them to an api. Thats all the communication that happens between the app and the extension.
Post not yet marked as solved
Im gonna be openning a TSI
Post not yet marked as solved
Ok last one.
I have set the timer to repeat every 10 seconds for the sake of testing. I have noticed in Instruments using Time Profiler that every time i make a request, more and more threads keep appearing and they dont go away so i think i may be on to something.
Post not yet marked as solved
I finally got DispatchSource Timer to work but the leak still continues. Another weird thing is that even though Instruments shows that there is a leak the memory usage of the app stays the same for hours so maybe the leak is not harmful ?
I will investigate if the fact that my post method is static has anything to do with the leak.