I coding a VPN tool, using the NetworkExtension
framework. I can connect IPSec through NEVPNManager.sharedManager
, and can grab the notification when VPN connect status changed. But when I kill the app, and reopen it, the NEVPNManager.Connect.Status
always Zero, than means can't display the correct connect state. How to solve it?
Asked
Active
Viewed 4,178 times
10

Ravi Dhorajiya
- 1,531
- 3
- 21
- 26

William Sterling
- 167
- 1
- 9
-
After you kill your app do you still see the [VPN] indicator in the status bar? – hungri-yeti Aug 21 '16 at 01:33
-
yes, the VPN still connecting. – William Sterling Aug 21 '16 at 03:28
2 Answers
9
William Sterling comment does make sense, & it works for me,
Before adding observer for NEVPNStatusDidChange
load preferences for VPN Manager object like bellow,
override func viewDidLoad() {
super.viewDidLoad()
self.vpnManager.loadFromPreferences { (error) in
if error != nil {
print(error.debugDescription)
}
else{
print("No error from loading VPN viewDidLoad")
}
}
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.VPNStatusDidChange(_:)), name: NSNotification.Name.NEVPNStatusDidChange, object: nil)
}

Mukesh Lokare
- 2,159
- 26
- 38
6
Try this:
func viewDidLoad() {
// Register to be notified of changes in the status. These notifications only work when app is in foreground.
notificationObserver = NSNotificationCenter.defaultCenter().addObserverForName(NEVPNStatusDidChangeNotification, object: nil , queue: nil) {
notification in
print("received NEVPNStatusDidChangeNotification")
let nevpnconn = notification.object as! NEVPNConnection
let status = nevpnconn.status
self.checkNEStatus(status)
}
}
func checkNEStatus( status:NEVPNStatus ) {
switch status {
case NEVPNStatus.Invalid:
print("NEVPNConnection: Invalid")
case NEVPNStatus.Disconnected:
print("NEVPNConnection: Disconnected")
case NEVPNStatus.Connecting:
print("NEVPNConnection: Connecting")
case NEVPNStatus.Connected:
print("NEVPNConnection: Connected")
case NEVPNStatus.Reasserting:
print("NEVPNConnection: Reasserting")
case NEVPNStatus.Disconnecting:
print("NEVPNConnection: Disconnecting")
}
}
The above code should generate the following messages when running the app with VPN already connected:
checkNEStatus: NEVPNConnection: Invalid
viewDidLoad: received NEVPNStatusDidChangeNotification
checkNEStatus: NEVPNConnection: Connected

hungri-yeti
- 584
- 1
- 5
- 6
-
I have do this, In my viewController, the code is`[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VPNStatusChanged:) name:NEVPNStatusDidChangeNotification object:nil]; // the status always NEVPNStatusInvalid ... NSLog(@"vpn status is %ld", [NEVPNManager sharedManager].connection.status);` and I can observe the state. but if kill the app, and reopen, the state always invalid. – William Sterling Aug 21 '16 at 04:57
-
3got it ! must load the vpn config before the notification, and then the NEVPNStatusDidChangeNotification worked ! thanks a lot. – William Sterling Aug 21 '16 at 05:16
-
1Are you retaining the NSObjectProtocol returned from addObserver? My previous snippet doesn't show it but I have: `var notificationObserver:NSObjectProtocol?` as a class var in the view controller – hungri-yeti Aug 21 '16 at 05:22
-
-
I'm experiencing wrong sorting on notification ... the last notification doesn't reflect the current VPN status. Is it possible? – Lubbo Oct 01 '19 at 06:51