Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notifications for Network change(Connect,Disconnect,Change)

In my app I have to alert whenever a network change happens for example when a app is connected or disconnected or changed(wifi to data) network.

But finding it by reaching a url seems expensive.

Is there any public api available in iOS to do this.

like image 702
sathishkumar_kingmaker Avatar asked Oct 05 '15 11:10

sathishkumar_kingmaker


People also ask

Where do I find Alerts on my iPhone?

Find your notifications in Notification Center On the Lock Screen: Swipe up from the middle of the screen. On other screens: Swipe down from the top center. Then you can scroll up to see older notifications, if there are any.

How do I turn off network notifications?

Navigate to Start > Settings> Sounds and Notifications. Select the Notifications tab. Select Wireless Network Detected from the dropdown list. Tap Ring Type and select None from the list.

What is connection notification?

Internet Connection Notification is a program that alerts you when your Internet Connection goes up or down!


2 Answers

I followed this tutorial

https://www.youtube.com/watch?v=wDZmz9IsB-8

credit --> Vasil Nunev

Download this class and import to my project --> Reachability.swift

https://github.com/ashleymills/Reachability.swift

(Reachability.swift) --> Unzip -->Sources/Reachability.swift

This is my view controller

import UIKit

class ViewController: UIViewController {

let reachability =  Reachability()!

override func viewDidLoad() {
    super.viewDidLoad()

    reachability.whenReachable = { _ in
        DispatchQueue.main.async {
            self.view.backgroundColor = UIColor.green
        }
    }

    reachability.whenUnreachable = { _ in
        DispatchQueue.main.async {
            self.view.backgroundColor = UIColor.red
        }
    }

    NotificationCenter.default.addObserver(self, selector: #selector(internetChanged), name: Notification.Name.reachabilityChanged , object: reachability)
    do{
        try reachability.startNotifier()
    } catch {
        print("Could not strat notifier")
    }
}

@objc  func internetChanged(note:Notification)  {
    let reachability  =  note.object as! Reachability
    if reachability.isReachable{
        if reachability.isReachableViaWiFi{
            self.view.backgroundColor = UIColor.green
        }
        else{
            DispatchQueue.main.async {
                self.view.backgroundColor = UIColor.orange
            }
        }
    } else{
        DispatchQueue.main.async {
            self.view.backgroundColor = UIColor.red
        }
    }
}
}
like image 193
user3826696 Avatar answered Oct 19 '22 23:10

user3826696


Look at this official Apple documentation

Using Reachability you can recognize the type of your connection.

There is also a complete example on how detect the changes and the documentation is updated now for ARC.

I hope this can help you

like image 36
Lorenzo Avatar answered Oct 19 '22 23:10

Lorenzo