Skip to content

Commit

Permalink
Fix VPN state when app re-launch
Browse files Browse the repository at this point in the history
  • Loading branch information
ruqli committed Jan 5, 2017
1 parent 7d556af commit f66ba5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions PacketTunnel/PacketTunnelProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ - (void)setupWormhole {
[weakSelf.wormhole passMessageObject:@"ok" identifier:@"tunnelStatus"];
}];
[self.wormhole listenForMessageWithIdentifier:@"stopTunnel" listener:^(id _Nullable messageObject) {
NSLog(@"received message: stopTunnel");
[weakSelf stop];
}];
[self.wormhole listenForMessageWithIdentifier:@"getTunnelConnectionRecords" listener:^(id _Nullable messageObject) {
Expand Down
15 changes: 12 additions & 3 deletions Potatso/HomeVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class HomeVC: FormViewController, UINavigationControllerDelegate, HomePresenterP
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
self.status = .Off
self.status = Manager.sharedManager.vpnStatus
print ("HomeVC.init: ", self.status.rawValue)
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
presenter.bindToVC(self)
presenter.delegate = self
Expand All @@ -52,7 +53,7 @@ class HomeVC: FormViewController, UINavigationControllerDelegate, HomePresenterP
self.navigationItem.titleView = titleButton
// Post an empty message so we could attach to packet tunnel process
Manager.sharedManager.postMessage()
handleRefreshUI(nil)
// handleRefreshUI(nil)
updateForm()
navigationItem.leftBarButtonItem = UIBarButtonItem(image: "List".templateImage, style: .Plain, target: presenter, action: #selector(HomePresenter.chooseConfigGroups))
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(addProxy(_:)))
Expand Down Expand Up @@ -182,16 +183,24 @@ class HomeVC: FormViewController, UINavigationControllerDelegate, HomePresenterP

func generateProxySection() -> Section {
let proxySection = Section("Connect".localized())
var reloading = true

proxySection <<< SwitchRow("connection") {
reloading = true
$0.title = status.hintDescription
$0.value = status.onOrConnectiong
reloading = false
}.onChange({ [unowned self] (row) in
if reloading {
return
}
self.handleConnectButtonPressed()
})
.cellUpdate ({ cell, row in
reloading = true
row.title = self.status.hintDescription
row.value = self.status.onOrConnectiong
reloading = false
})
<<< TextRow(kFormDNS) {
$0.title = "DNS".localized()
Expand All @@ -213,7 +222,7 @@ class HomeVC: FormViewController, UINavigationControllerDelegate, HomePresenterP
var count = 0
if ruleSet.ruleCount > 0 {
count = ruleSet.ruleCount
}else {
} else {
count = ruleSet.rules.count
}
if count > 1 {
Expand Down
10 changes: 8 additions & 2 deletions PotatsoLibrary/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public enum ManagerError: ErrorType {
case VPNStartFail
}

public enum VPNStatus {
public enum VPNStatus : Int {
case Off
case Connecting
case On
Expand Down Expand Up @@ -54,9 +54,14 @@ public class Manager {
loadProviderManager { (manager) -> Void in
if let manager = manager {
self.updateVPNStatus(manager)
if self.vpnStatus == .On {
self.observerAdded = true
NSNotificationCenter.defaultCenter().addObserverForName(NEVPNStatusDidChangeNotification, object: manager.connection, queue: NSOperationQueue.mainQueue(), usingBlock: { [unowned self] (notification) -> Void in
self.updateVPNStatus(manager)
})
}
}
}
addVPNStatusObserver()
}

func addVPNStatusObserver() {
Expand All @@ -78,6 +83,7 @@ public class Manager {
}

func updateVPNStatus(manager: NEVPNManager) {
print("updateVPNStatus:", manager.connection.status.rawValue)
switch manager.connection.status {
case .Connected:
self.vpnStatus = .On
Expand Down

0 comments on commit f66ba5f

Please sign in to comment.