How to check if my network connected to mobile hotspot as Wifi

I tried using "Reachability" class to check whether my network connected to LTE or Wifi in Swift but i am not able to get the solution.

I have created a Mac OS application and added Reachability class. Now i have to check whether my network is Wifi or Cellular(4G, LTE).

is there a way to find this?

Iam using SystemConfiguration framework and "isWWAN" always returns false.

Replies

Take a look at NWPathMonitor for detect path changes between cellular and Wi-Fi.

You will want to take a look at the pathUpdateHandler to detect changes between cellular and Wi-Fi.


Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

I appreciate your reply Meaton.


Yes, NWPathMonitor delegates whenver the network status changes and it works in iOS.


I have created a Cocoa(macOS) application and using NWPathMonitor, is it possible to get to know when MacBook Air WiFi connected to my cellular network via hotspot enabled.

My Cocoa app should identify whenever the network connected via Wi-Fi or via Celluar (hotspot enabled) network.


let monitor = NWPathMonitor(requiredInterfaceType: .wifi)
        let queue = DispatchQueue.global(qos: .background)
        monitor.start(queue: queue)
        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                print("Connected")
            }
        }


if i change the requiredInterfaceType as ".cellular". am not getting the path.status as .satisfied. It always says interface type as .wifi even if i connected via cellular hotspot.

If are wanting to know whether the Wi-Fi connection is sourced from an access point or a hotspot enabled device, then NWPathMonitor will not provide this information. NWPathMonitor see's correctly sees both connections as Wi-Fi connections.

| is it possible to get to know when MacBook Air WiFi connected to my cellular network

| via hotspot enabled.


let nwPathMonitor = NWPathMonitor()
nwPathMonitor.pathUpdateHandler = { path in
   
    if path.usesInterfaceType(.wifi) {
        // Correctly goes to Wi-Fi via Access Point or Phone enabled hotspot
        os_log("Path is Wi-Fi")
    } else if path.usesInterfaceType(.cellular) {
        os_log("Path is Cellular")
    } else if path.usesInterfaceType(.wiredEthernet) {
        os_log("Path is Wired Ethernet")
    } else if path.usesInterfaceType(.loopback) {
        os_log("Path is Loopback")
    } else if path.usesInterfaceType(.other) {
        os_log("Path is other")
    }
}
nwPathMonitor.start(queue: .main)


What are you looking to do with this information?



Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

I have one usecase to implement in such a way that i can intimate/alert the user in case of network changes (Wifi/Cellular). Let me know if any other solutions are there to achieve this.


Thanks for your suggestions. You saved my time.

For macOS detecting changes between Wired Ethernet, Wi-Fi, and Loopback is what you are looking for with NWPathMonitor.


There is no API for detecting whether the Wi-Fi connection is sourced from a Hotspot connection or from a access point.


Matt Eaton

DTS Engineering, CoreOS

meaton3 at apple.com

Thanks for your time.

I don't want to monitor network type connected changes but I just want to know type of network mobile device is connected at given time. like want to know whether it's wifi or LTE or wired Ethernet.
@sreekanth reddy

Not sure I completely understand your question, but for:

but I just want to know type of network mobile device is connected at given time. like want to know whether it's wifi or LTE or wired Ethernet.

However, for detecting if the path is connected to Wi-Fi, Cellular, or Wired Ethernet, you would use the following:

Code Block swift
let nwPathMonitor = NWPathMonitor()
nwPathMonitor.pathUpdateHandler = { path in
if path.usesInterfaceType(.wifi) {
os_log("Path is Wi-Fi")
} else if path.usesInterfaceType(.cellular) {
os_log("Path is Cellular")
} else if path.usesInterfaceType(.wiredEthernet) {
os_log("Path is Wired Ethernet")
} else if path.usesInterfaceType(.loopback) {
os_log("Path is Loopback")
} else if path.usesInterfaceType(.other) {
os_log("Path is other")
}
}
nwPathMonitor.start(queue: .main)


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
I need to catch the event of connecting and disconnecting a iPhone to an external camera. The connection is via wifi. Reachability library is not up to the task. I also tried the code above, but it doesn't work either.

The problem is that when the iPhone is connected via mobile data (or air mode is turned on) and simultaneously connect to the camera via wifi, none of the methods sends me any event. Tell me how to solve this problem

I need to catch the event of connecting and disconnecting a iPhone to an external camera. The connection is via wifi. Reachability library is not up to the task. I also tried the code above, but it doesn't work either.

The problem is that when the iPhone is connected via mobile data (or air mode is turned on) and simultaneously connect to the camera via wifi, none of the methods sends me any event. Tell me how to solve this problem

I figured it out! I implemented everything using the Reachability library, it was only necessary to specify the ip address of the camera.

Code Block swift
reachability = try? Reachability(hostname: currentCamera.ipAddress)