How can i check mobile data or wifi is on or off. ios swift

回眸只為那壹抹淺笑 提交于 2019-12-03 03:47:11

Try like below code:

Create object of reachability class like,

    var internetReachability = Reachability()

Now write below code in viewDidLoad(),

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
    self.internetReachability = Reachability.reachabilityForInternetConnection()
    self.internetReachability.startNotifier()
    self.updateInterfaceWithReachability(self.internetReachability)

Now create function to check reachabilty of wifi as well as data pack,

func updateInterfaceWithReachability(reachability: Reachability)
{
    let netStatus : NetworkStatus = reachability.currentReachabilityStatus()
    switch (netStatus.rawValue)
    {
    case NotReachable.rawValue:
        print("offline")
        break
    case ReachableViaWWAN.rawValue:
        print("online")
        break
    case ReachableViaWiFi.rawValue:
        print("online")
        break
    default :
        print("offline")
        break
    }
}

// Rechability update status
func reachabilityChanged(sender : NSNotification!)
{
    let curReach : Reachability = sender.object as! Reachability
    self.updateInterfaceWithReachability(curReach)
}

Hope this will help you.

If you have any super Class in your application then use below code

override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)


        NSNotificationCenter.defaultCenter().addObserver(self, selector: "ShowNetConnectivity", name: SHOW_NO_INTERNET_CONNECTION, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "DisssmissConnectivity", name: DISMISS_INTERNET_CONNECTION, object: nil)


        let reachability = SCNetworkReachabilityCreateWithName(nil, host)!
        SCNetworkReachabilitySetCallback(reachability, { (_, flags, _) in
            print(flags.rawValue)

            if (flags.rawValue == NotReachable.rawValue && flags.rawValue != ReachableViaWiFi.rawValue && flags.rawValue != ReachableViaWWAN.rawValue)
            {
                if(isConnectionAvailable == true)
                {
                    let nc = NSNotificationCenter.defaultCenter()
                    nc.postNotificationName(SHOW_NO_INTERNET_CONNECTION, object: nil)
                }
            }else
            {
                if(isConnectionAvailable == false)
                {
                    let nc = NSNotificationCenter.defaultCenter()
                    nc.postNotificationName(DISMISS_INTERNET_CONNECTION, object: nil)
                }
            }

            }, &context)

        SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes)
    }

    override func viewWillDisappear(animated: Bool)
    {
        super.viewWillDisappear(animated)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: SHOW_NO_INTERNET_CONNECTION, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: DISMISS_INTERNET_CONNECTION, object: nil)
    }

Here's how I do it and it works for me. In my viewDidLoad:

do {
        reachability = try Reachability.reachabilityForInternetConnection()
    } catch {
        print("Unable to create Reachability")
        return
    }

    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(MainViewController.reachabilityChanged(_:)),
                                                     name: ReachabilityChangedNotification,
                                                     object: reachability)

    do {
        try reachability.startNotifier()
    } catch {
        print("This is not working.")
        return
    }

And the reachabilityChanged

func reachabilityChanged(note: NSNotification) {

    let reachability = note.object as! Reachability

    if reachability.isReachable() {
        if reachability.isReachableViaWiFi() {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    } else {
        showNoConnectionAlert()
        print("Not reachable")
    }
}

Using this Reachability

I use this pod repo to achieve not only wifi or mobile data. you can do much more with it and you can use it in ObjC or Swift.

Best regards.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!