TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Nov 2017 |
Maintained by Alexey Belkevich.
SCNetworkReachability class is the wrapper on C-structures and C-functions of SCNetworkReachability API in SystemConfiguration.framework
NSNotificationCenter
. Use callback blocks to determine reachability status or observe changesAdd pod 'SCNetworkReachability'
to Podfile
Check current reachability status of some host. This callback will run only once immediately after reachability status will be determined. Note. Callback block will run on main queue!
SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initWithHost:@"https://github.com"];
[reachability reachabilityStatus:^(SCNetworkStatus status)
{
switch (status)
{
case SCNetworkStatusReachableViaWiFi:
NSLog(@"Reachable via WiFi");
break;
case SCNetworkStatusReachableViaCellular:
NSLog(@"Reachable via Cellular");
break;
case SCNetworkStatusNotReachable:
NSLog(@"Not Reachable");
break;
}
}];
And if you don't want to store instance of SCNetworkReachability
you can do the same with class method.
[SCNetworkReachability host:@"github.com" reachabilityStatus:^(SCNetworkStatus status)
{
switch (status)
{
case SCNetworkStatusReachableViaWiFi:
NSLog(@"Reachable via WiFi");
break;
case SCNetworkStatusReachableViaCellular:
NSLog(@"Reachable via Cellular");
break;
case SCNetworkStatusNotReachable:
NSLog(@"Not Reachable");
break;
}
}];
Note. There is only one difference from iOS that you have only one status SCNetworkStatusReachable
instead of SCNetworkStatusReachableViaWiFi
and SCNetworkStatusReachableViaCellular
.
Observe reachability changes of some host. This callback will run each time reachability status will be changed.
SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initWithHost:@"example.com"];
[reachability observeReachability:^(SCNetworkStatus status)
{
switch (status)
{
case SCNetworkStatusReachableViaWiFi:
NSLog(@"Reachable via WiFi");
break;
case SCNetworkStatusReachableViaCellular:
NSLog(@"Reachable via Cellular");
break;
case SCNetworkStatusNotReachable:
NSLog(@"Not Reachable");
break;
}
}];
Sometimes you need to check reachability not on main queue. And it's easy with Multithreading
subspec.
Add pod 'SCNetworkReachability/Multithreading'
to Podfile.
Multithreading
subspec allows to set custom dispatch queue for callbacks. So you can choose any of this methods.
- (void)observeReachabilityOnDispatchQueue:(dispatch_queue_t)queue
callback:(void (^)(SCNetworkStatus))block;
- (void)reachabilityStatusOnDispatchQueue:(dispatch_queue_t)queue
callback:(void (^)(SCNetworkStatus))block;
+ (void)hostReachabilityStatus:(NSString *)host onDispatchQueue:(dispatch_queue_t)queue
callback:(void (^)(SCNetworkStatus))block;
Another useful subspec is Shared
. It creates shared instance of SCNetworkReachability
class.
Add pod 'SCNetworkReachability/Shared'
to Podfile.
SCNetworkReachability *reachability = SCNetworkReachability.shared;
I've leaved two reachability initialisation methods to make it compatible with Apple's reachabililty. I've never used them but it can be useful for someone.
Add pod 'SCNetworkReachability/Compatibility'
to Podfile.
Determine WiFi reachability on device. Available only on iOS.
SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initForLocalWiFi];
Reachability with IP-address struct
struct sockaddr_in address;
// Does anybody knows how to define this structure? =)
address = ...
SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initWithHostAddress:&address];
Follow updates on twitter @okolodev