Detecting Network Connectivity Changes using Reachability, NSNotification and Network Link Conditioner in Swift
You must create a Reachability object before you can receive notifications from it. Also, be sure to call the startNotifier()
method on the Reachability object you create. This would be an example of how to do so inside of your application delegate:
class AppDelegate: UIResponder, UIApplicationDelegate
{
private var reachability:Reachability!;
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
{
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);
self.reachability = Reachability.reachabilityForInternetConnection();
self.reachability.startNotifier();
}
@objc func checkForReachability(notification:NSNotification)
{
// Remove the next two lines of code. You cannot instantiate the object
// you want to receive notifications from inside of the notification
// handler that is meant for the notifications it emits.
//var networkReachability = Reachability.reachabilityForInternetConnection()
//networkReachability.startNotifier()
let networkReachability = notification.object as Reachability;
var remoteHostStatus = networkReachability.currentReachabilityStatus()
if (remoteHostStatus.value == NotReachable.value)
{
println("Not Reachable")
}
else if (remoteHostStatus.value == ReachableViaWiFi.value)
{
println("Reachable via Wifi")
}
else
{
println("Reachable")
}
}
}
I recommend you take a look at the documentation for NSNotificationCenter and NSNotification. That way you'll be more familiar with how to work with notifications next time something like this comes up.
Swift 3
NotificationCenter.default.addObserver(self, selector:Selector(("checkForReachability:")), name: NSNotification.Name.reachabilityChanged, object: nil)
let reachability: Reachability = Reachability.forInternetConnection()
reachability.startNotifier()
How to get notifications for Network change(Connect,Disconnect,Change)
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
How to detect change in network with Reachability?
Another possible solution is to add a NS Notification in "application didfinishlaunching":
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil];
and in checkForReachability method do this:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
//Do something
}
else if (remoteHostStatus == ReachableViaWiFi) {
// Do something
}
else{
// Else do something else
}
Wifi-Network Changed Notification
If you are using this Reachability files then its easy.
Add an Observer inside the viewWillAppear
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityDidChange:) name:kReachabilityChangedNotification object:nil];
And implement the observer method
- (void)reachabilityDidChange:(NSNotification *)notification {
Reachability *reachability = (Reachability *)[notification object];
if ([reachability isReachable] && [reachability isReachableViaWiFi]) {
NSLog(@"Reachable via Wifi");
}
}
Updated
Add the following code inside the Network change completion block of your Reachability handler. By Sending the SCNetworkReachabilityFlag
as a parameter to the method.
-(BOOL)isReachableViaWiFi :(SCNetworkReachabilityFlags)flags {
// Check we're reachable
if((flags & kSCNetworkReachabilityFlagsReachable))
{
// Check we're NOT on WWAN
if((flags & kSCNetworkReachabilityFlagsIsWWAN))
{
return NO;
}
return YES;
}
return NO;
}
Internet connection status swift reachability
Declare this in AppDelegate / Vc
let reachability = Reachability()!
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
print("could not start reachability notifier")
}
//
when status is changed this method is called
@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 .none:
print("Network not reachable")
}
}
//
to avoid crashes don't forget to un register
reachability.stopNotifier()
NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
Related Topics
Setneedsdisplayinrect: Causes the Whole View to Be Updated
Is It Just the iPhone Simulator That Is Restricted to Intel Only MAC'S
Uitableview Scrolls to Top When Reloading Cells with Changing Cell Heights
Core Data Store Included in App Bundle
How to Port "Method_Getimplementation" and "Method_Setimplementation" to Monotouch
View Controller Responds to App Delegate Notifications in iOS 12 But Not in iOS 13
How to Draw Inside the Black Edges in iOS Sdk with Opengl Es
How to Set Mdm Payload's "Identity" in Ipcu
How to Add Watermark on a Exist Image
Xctest Build Errors for Test Target Xcode 5:
Background User Location When App Is Terminated/Suspended
Uiscrollview Not Scrolling Regardless of Large Contentsize
Change the Color of a Link in an Nsmutableattributedstring
Swift 3: How to Add Watermark on Video? Avvideocompositioncoreanimationtool iOS 10 Issue