I have implemented one successful vpn connection. But When i close and Open app while the VPN is connected, then i can't disconnect the VPN.
public func connectVPN() {
//For no known reason the process of saving/loading the VPN configurations fails.On the 2nd time it works
Log.d(message: "connectVPN")
NSLog("ConnectVPN")
self.vpnManager.loadFromPreferences(completionHandler: self.vpnLoadHandler)
NotificationCenter.default.addObserver(forName: NSNotification.Name.NEVPNStatusDidChange, object: nil , queue: nil) {
notification in
let nevpnconn = notification.object as! NEVPNConnection
let status = nevpnconn.status
self.vpnDelegate?.checkNES(status: status)
}
}
public func disconnectVPN() ->Void {
Log.d(message: "Disconnect VPN Called")
Log.d(message: "Log Disconnect VPN Called")
print("vpnManager:disconnectVPN \(self.vpnManager.connection) ") // object available
self.vpnManager.connection.stopVPNTunnel()
}
I could not find out why it is not disconnecting.
Call stopVPNTunnel() inside loadFromPreferences closure.
NEVPNManager.shared().loadFromPreferences { error in
assert(error == nil, "Failed to load preferences: \(error!.localizedDescription)")
NEVPNManager.shared().connection.stopVPNTunnel()
}
Related
I need an event or like an observer while switching between wifi networks or wifi to cellular and vice versa. Can anybody help me with it? I am uploading a video file and while uploading I am switching between wifi networks or auto-switch between cellular to wifi. So need an event for the same.
It is included in the example on the Reachability github page
//declare this property where it won't go out of scope relative to your listener
let reachability = try! Reachability()
//declare this inside of viewWillAppear
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
And the method
#objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
case .cellular:
print("Reachable via Cellular")
case .unavailable:
print("Network not reachable")
}
}
To check whether the device is connected or not, you can use NWPathMonitor, like this:
import Network
struct ConnectivityMonitor {
static let monitor = NWPathMonitor()
static func setup() {
ConnectivityMonitor.monitor.pathUpdateHandler = { path in
//path.isExpensive tells you whether the user is using cellular data as opposed to wifi. "true" if cellular data.
if path.status == .satisfied {
//Connected
} else {
//Not connected
}
}
let queue = DispatchQueue(label: "Monitor")
ConnectivityMonitor.monitor.start(queue: queue)
}
}
I wanna have UDP connection with a wifi module as a server and an iOS as a client and send/receive data (obviously).First i wanted to use Sockets but then i realized Apple has introduced Network Framework; So i used NWConnection which is a class from Network Framework for my purpose and i was successful at sending the data to the device but unable to receive the response (which i am sure exists as i monitor devices I/O packets via serial port monitor).Here is a test version of the code where i use netcat as server to test the connection:
ViewController Class
import UIKit
import Network
class ViewController: UIViewController
{
var network: UDPNetwork!
override func viewDidLoad()
{
super.viewDidLoad()
}
#IBAction func button(_ sender: Any)
{
self.network = UDPNetwork(host: "192.168.200.4", port:"4210")!
self.network.connect()
}
}
UDPNetwork Class
import Foundation
import Network
class UDPNetwork {
var hostUDP: NWEndpoint.Host
var portUDP: NWEndpoint.Port
private var connection: NWConnection?
private var queue = DispatchQueue(label: "NetworkQuue", qos: .utility)
init?(host: String, port: String) {
guard !host.isEmpty, let portUDP = NWEndpoint.Port(port) else {
return nil
}
self.hostUDP = NWEndpoint.Host(host)
self.portUDP = portUDP
}
func connect()
{
connection = NWConnection(host: hostUDP, port: portUDP, using: .udp)
connection?.stateUpdateHandler =
{
(newState) in switch (newState)
{
case .ready:
//The connection is established and ready to send and recieve data.
print("ready")
self.sendPaket("Hello")
self.receive()
case .setup:
//The connection has been initialized but not started
print("setup")
case .cancelled:
//The connection has been cancelled
print("cancelled")
case .preparing:
//The connection in the process of being established
print("Preparing")
default:
//The connection has disconnected or encountered an error
print("waiting or failed")
}
}
connection?.start(queue: self.queue)
}
func sendPaket(_ packet:String)
{
let packetData = packet.data(using: .utf8)
self.connection?.send(content: packetData, completion: NWConnection.SendCompletion.contentProcessed(({ (error) in
if let err = error {
print("Sending error \(err)")
} else {
print("Sent successfully")
}
})))
}
func receive()
{
self.connection?.receiveMessage(completion:
{
(data, context, isComplete, error) in
print("Got it")
if let err = error {
print("Recieve error: \(err)")
}
if let rcvData = data,
let str = String(data:rcvData, encoding: .utf8) {
print("Received: \(str)")
}
self.receive()
})
}
}
Apple Documentation says:
receiveMessage(completion:)
Schedules a single receive completion handler for a complete message
completion :
A receive completion is invoked exactly once for a call to receive.
My Question is:
How can we call to receive ?
Assuming the receiveMessage(completion:) method is the Call to receive and also after receiving complete message calls the completion itself, What could be the problem if it doesn't get invoked?
Related code and more detail about my use case can be found here:
Swift: Receiving UDP packets from server on serial port monitor but not in ios app
With these assumptions :
We call to receive on the same connection that we send data to
UDP is used method for connection
Server is tested for swapping ports correctly, meaning it responds on the same IP,Port that data is sent to
My Setup:
13" MacBookPro Early 2015 with MacOS Catalina 10.15
Xcode Version 11.0 (11A420a)
Swift 5.1
target iOS 12+
And this is the result from Wire Shark:
I m trying to implement webservice call, when user takes screenshot, and I have successfully implemented it with the bellow method.
//this method get called when screenshot captured
func detectScreenShot() {
let mainQueue = OperationQueue.main
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot,
object: nil,
queue: mainQueue,
using: { notification in
WEBSERVICE_INTERFACE.webServiceWithPostJSONParameters(param: nil, methodName: Constants.URLs.screenshot, headers: Constants.Headers.urlEncoded, showProgress: false, completion: { (response) in
if let response = response{
let response = BaseResponse(JSONString : response)
if let message = response?.message{
LIMITUtils.showAlertMessage(message: message)
}
}
})
})
}
But now I have the scenario, suppose user takes the screenshot by making internet connection off, then no webservice call will happen and user will get the benefits out of it. Now I wanted to have some sort of solution where I can save the webservice call if no connection is available and make the same call when internet connection becomes available. Can anyone please suggest me how I can proceed for this?
Use Reachability for check Internet available or not write bellow code to check internet
//MARK:- check internet connection
func checkInternetStatus() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(reachabilityChanged(_:)), name:ReachabilityChangedNotification, object: nil)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
//Check for Internet Status
do {
self.reachability = try Reachability.reachabilityForInternetConnection()
try self.reachability?.startNotifier()
} catch {
jprint("Errore")
return
}
}
}
func reachabilityChanged(note:NSNotification) {
let Status = reachability?.currentReachabilityStatus //note.object as! Reachability
if Status != .NotReachable {
jprint("isNetworkAvailable = true")
isNetworkAvailable = true
} else {
jprint("isNetworkAvailable = false")
ShowFlinntAlert("No internet", description: "Your internet connection seems to be down")
isNetworkAvailable = false
}
}
Before API call just check
guard isInternetAvailable() else {
return
}
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?
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)
}
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
I am currently using Ashley Mill's Reachability Class. If the application launches with network connectivity then I am able to toggle between connectivity availability without any issues and able to display a network connectivity Alert Controller properly. However if the application is launched when the app starts without internet connection/on airplane mode it abruptly crashes.
override func viewDidLoad()
{
super.viewDidLoad()
setUpReachability (nil)
}
func setUpReachability(hostName: String?)
{
do
{
let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
self.reachability = reachability
try! self.reachability?.startNotifier()
}
catch ReachabilityError.FailedToCreateWithAddress(let address)
{
print("\(address)")
return
} catch {}
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
}
func reachabilityChanged(notification: NSNotification)
{
let reachability = notification.object as! Reachability
if reachability.isReachable()
{
if reachability.isReachableViaWiFi()
{
connected = true
}
else
{
connected = true
}
}
else
{
let alert = UIAlertController( title: "No Network Connection Available", message:"Try Again", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Will Do!" , style: .Default) { _ in } )
presentViewController ( alert, animated: true ) {}
connected = false
}
}
What can be done to allow the iPhone application to launch and display an alert saying there is no network connection rather than abruptly crash?
Error Message:
fatal error: unexpectedly found nil while unwrapping an Optional value
But I would think that reachability changed would catch this in the else statement and pop the error message up?
Shouldn't the else in the reachability.isReachableViaWiFi() if statement be:connected = false ?
The error was that I was in fact trying to download data at the launch of the app instead of first allowing the initialization of the app to finish to then send a request to the server to access information.