Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift iOS- What to do when there is a wifi connection but no internet connection?

I'm using Alamofire in my iOS app. I use bool values in viewWillAppear and in AppDelegate with NSNotifications to check if there is an internet connection. If there is no wifi connection a pop up appears to inform the user. If there is a wifi connection the pop up disappears and everything works fine again. I've had no problems as long as wifi is clearly not working.

I was at a meetup and someone explained to me that the way it works is it it looks for a wifi connection and not an internet connection. For e.g.. if I have a wifi router and it's plugged in but the router isn't connected to the internet Alamofire will view this as a successful connection because it actually is connecting to wifi although it doesn't know the wifi can't connect to the internet.

I was just in a situation where I connected to an open network, my app initially responded as if I were actually connected to the internet (no pop up) but I couldn't get connect to anything. The wifi signal was on full. In terminal I ran a ping and it turns out the connection was dead. My app couldn't tell the difference.

enter image description here enter image description here

How do I make a pop up appear in a sitaution like this?

Also what is .case unknown for?

Alamofire.Swift:

import Foundation
import Alamofire

open class NetworkManager {

    open static var sharedManager: NetworkReachabilityManager = {

        let reachabilityManager = NetworkReachabilityManager()

        reachabilityManager?.listener = { (status) in

            switch status {

            case .notReachable:
                print("The network is not reachable")
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "unsuccessful"), object: nil)

            case .unknown : //???????
                print("It is unknown wether the network is reachable")
                //I'm not sure whether to put a Notification for successful or unsuccessful???

            case .reachable(.ethernetOrWiFi):
                print("The network is reachable over the WiFi connection")
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "successful"), object: nil)

            case .reachable(.wwan):
                print("The network is reachable over the WWAN connection")
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "successful"), object: nil)
            }
        }

        reachabilityManager?.startListening()
        return reachabilityManager!
    }()
}

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        NetworkManager.sharedManager.startListening()

SomeVC:

override func viewWillAppear() {
        super.viewWillAppear()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(successful), name: "successful", object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(unsuccessful), name: "unsuccessful", object: nil)

       if NetworkManager.sharedManager.isReachable == true{
            self.successful()
       }else{
            self.unsuccessful()
       }

       if NetworkManager.sharedManager.isReachableOnWWAN == true{ 
            self.successful()
       }else{
            self.unsuccessful()
       }

       if NetworkManager.sharedManager.isReachableOnEthernetOrWiFi == true{ 
            self.successful()
       }else{
            self.unsuccessful()
       }
}

func successful(){
    //dismiss pop up
}

func unsuccessful(){
    //show pop up
}

deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self, name: "successful", object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: "unsuccessful", object: nil)
}
}
like image 453
Lance Samaria Avatar asked Mar 28 '17 10:03

Lance Samaria


People also ask

Why does it say wireless connection but no internet connection?

A single device has a WiFi connection without internet If your PC or laptop is the only device on your network that claims to have a connection but no actual internet access, you likely have an incorrect configuration problem, faulty drivers, WiFi adapter, DNS problems, or an issue with your IP address.

Why does my WiFi say no internet connection iOS?

Still can't connect? Reset your Network Settings. If you're using iOS or iPadOS 15 or later, tap Settings > General > Transfer or Reset [Device] > Reset > Reset Network Settings. If you're using iOS or iPadOS 14 or earlier, tap Settings > General > Reset > Reset Network Settings.

Why internet is not working in iOS?

The most common reasons why your iPad or iPhone won't connect to Wi-Fi include a poor connection signal, an incorrect Wi-Fi password, your Wi-Fi or mobile data settings, restricted network settings, or even a bulky phone case. A weak Wi-Fi connection can also mean you need to increase your internet speed.


1 Answers

You can init NetworkReachabilityManagerwith host, for example, google host, because default is 0.0.0.0

let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")

When you start listening reachability manager doing ping to host. If network is available you can cache SSID and ping again when SSID changed.

For case .unknown better put a Notification for unsuccessful.

Example get SSID (it doesn't work in Simulator):

func fetchSSIDInfo() ->  String? {  
        if let interfaces = CNCopySupportedInterfaces() {  
            for i in 0..<CFArrayGetCount(interfaces){  
                let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)  
                let rec = unsafeBitCast(interfaceName, to: AnyObject.self)  
                let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)  

                if let unsafeInterfaceData = unsafeInterfaceData as? Dictionary<AnyHashable, Any> {  
                    return unsafeInterfaceData["SSID"] as? String  
                }  
            }  
        }  
        return nil  
    }
like image 87
Dialogue Avatar answered Sep 20 '22 15:09

Dialogue