22 May 2024 Leave a comment Tech-Help
Ensuring your iOS or macOS application has an active internet connection is crucial for many functionalities. There are several methods to achieve this, each with its own pros and cons. Below, we outline a few effective ways to check for an internet connection using both Swift and Objective-C.
Using the Reachability Framework
The Reachability framework is a popular choice for checking internet connectivity. It provides a straightforward way to determine if the device is connected via WiFi or cellular data.
Swift Implementation
To get started with Swift, you can use the Reachability.swift library. Here’s a basic implementation:
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 Implementation
For Objective-C, you can use the Reachability class provided by Tony Million. First, add the SystemConfiguration framework to your project and then include the Reachability.h and Reachability.m files from here.
#import "Reachability.h"
@interface MyViewController ()
{
Reachability *internetReachableFoo;
}
@end
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
});
};
[internetReachableFoo startNotifier];
}
Using NWPathMonitor for iOS 12+ and macOS 10.14+
If you are developing for iOS 12 or newer, or macOS 10.14 (Mojave) or newer, you can use NWPathMonitor for a more modern approach:
import Network
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.status != .satisfied {
// Not connected
} else if path.usesInterfaceType(.cellular) {
// Cellular 3/4/5g connection
} else if path.usesInterfaceType(.wifi) {
// Wi-Fi connection
} else if path.usesInterfaceType(.wiredEthernet) {
// Ethernet connection
}
}
monitor.start(queue: DispatchQueue.global(qos: .background))
For more information, you can visit the Apple Developer documentation.
Leveraging Repeato for Automated Testing
Automating your tests to ensure connectivity can be a time-consuming task. This is where Repeato, a No-code test automation tool for iOS and Android, comes into play. With Repeato, you can create, run, and maintain automated tests for your apps efficiently.
Repeato uses computer vision and AI to simplify the test automation process, allowing developers to focus on creating great products instead of maintaining tests. Moreover, it enables non-technical colleagues or QA teams to handle test automation, enhancing productivity and collaboration.
For more information on how Repeato can help streamline your development and testing process, visit our documentation and explore our pricing plans.