4

I am trying to create a VPN app that notifies the user when the VPN is turned off manually in the Settings app. More generally, I want to be able to react when the network settings are changed. I have seen a lot of comments on StackOverflow regarding Reachability, Network, etc. but I can't find out whether I can check these things in the background. Would there be a way to do it by using "fetch" or "remote-notification". I have an app on my phone that gives me a notification if I turn off the VPN, so I know there's a way to do it, but I can't figure out how.

aequinox
  • 125
  • 1
  • 17
  • check this answers out. https://stackoverflow.com/a/65211525 https://stackoverflow.com/a/62777999 https://stackoverflow.com/a/64316672 This is maybe not the solution for all the problems you mentioned, but maybe it helps you. – DoTryCatch May 27 '21 at 12:54
  • @HackMac Thank you. Do any of those allow network monitoring in the background? I already am able to detect network changes when the app is open. – aequinox May 27 '21 at 16:03
  • Im not sure, never used them for this type of task, but I would just assign them to the dispatch queue background with a timer calling the function very min or something like that (I know not the best way but it should work). – DoTryCatch May 28 '21 at 22:27
  • @HackMac And that will continue working after the app is closed and in the background? – aequinox May 29 '21 at 03:05
  • For running tasks after the app terminated see: https://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated - or many create an agent app that does this task. – DoTryCatch May 29 '21 at 12:35

2 Answers2

1

According to This Apple Developer Discussion:

The answer might be No. This Apple developer discussion might be answered by Apple staff.

There’s no mechanism for running code in the background on network changes. Most folks who need to do this sort of thing use the VPN On Demand architecture. VPN On Demand has an API, but that API directly maps to configuration profile properties and configuration profiles aren’t considered an API (meaning they’re supported by Apple Support, not by Developer Technical Support).

Cyan
  • 181
  • 11
  • 1
    Thank you. I also saw that, but like I said, I have an App Store approved app that has this functionality, so I believe there is some way to do this. And there is no profile used for that VPN app. – aequinox May 29 '21 at 03:06
1

This will work to monitor your internet status every 3 seconds:

import Cocoa
import Darwin
import Network

//DispatchQueue.global(qos: .userInitiated).async {
let monitor = NWPathMonitor()
let queue = DispatchQueue(label: "Monitor")
monitor.start(queue: queue)
var count = 10


while count >= 0 {
    
monitor.pathUpdateHandler = { path in
    
    if path.status == .satisfied {
        print("There is internet")
        
        if path.usesInterfaceType(.wifi) { print("wifi") }
        else if path.usesInterfaceType(.cellular) { print("wifi") }
        else if path.usesInterfaceType(.wiredEthernet) { print("wiredEthernet") }
        else if path.usesInterfaceType(.loopback) { print("loopback") }
        else if path.usesInterfaceType(.other) { print("other") }

        
    } else {
        print("No internet")
    }
    
}
   sleep(3)
   count = count - 1
}

monitor.cancel()
//}

This code returns every three seconds the internet status and the connection type. if you want this to run for ever, change the while count >= 0 to while true with DispatchQueue.global(qos: .userInitiated).async { or DispatchQueue.global(qos: .background).async { you can move the task to the background.
To continue run the code in background (when app is not present) follow along https://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated .

Note: The Appstore will sometimes reject your app when using the reachability framework.

DoTryCatch
  • 1,052
  • 6
  • 17
  • Thank you. So there’s no way to do something in the background without the user being able to disable it? – aequinox May 29 '21 at 20:45
  • Do you mean you want users cannot disable it? If so, as I know, even if you can do, Appstore might reject your app. – Cyan May 30 '21 at 00:05