Member-only story
How to check for an active Internet connection on iOS or macOS?
Important: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you’ll freeze up your app.
Swift
1) Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift
2) Test reachability via closures
let reachability = Reachability()!reachability.whenReachable = { reachability in
if reachability.connection == .wifi {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
}reachability.whenUnreachable = { _ in
print("Not reachable")
}do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
Objective-C
1) Add SystemConfiguration
framework to the project but don't worry about including it anywhere
2) Add Tony Million’s version of Reachability.h
and Reachability.m
to the project (found here: https://github.com/tonymillion/Reachability)
3) Update the interface section
#import "Reachability.h"// Add this to the interface in the .m file of your view controller
@interface MyViewController ()
{
Reachability *internetReachableFoo;
}
@end
4) Then implement this method in the .m file of your view controller which you can call
// Checks if we have an internet connection or not
…