|
@@ -0,0 +1,127 @@
|
|
|
|
|
+//
|
|
|
|
|
+// RAReachability.m
|
|
|
|
|
+// Apex And Drivers
|
|
|
|
|
+//
|
|
|
|
|
+// Created by Jack on 2018/10/23.
|
|
|
|
|
+// Copyright © 2018年 USAI. All rights reserved.
|
|
|
|
|
+//
|
|
|
|
|
+
|
|
|
|
|
+#import "RAReachability.h"
|
|
|
|
|
+#import "Reachability.h"
|
|
|
|
|
+
|
|
|
|
|
+#define host @"https://ra.apexshipping.com"
|
|
|
|
|
+
|
|
|
|
|
+@interface RAReachability ()
|
|
|
|
|
+
|
|
|
|
|
+@property (nonatomic,copy) void(^ReachabilityBlk)(BOOL);
|
|
|
|
|
+
|
|
|
|
|
+@end
|
|
|
|
|
+
|
|
|
|
|
+@implementation RAReachability {
|
|
|
|
|
+ Reachability *_hostReachability; ///< 服务器可达
|
|
|
|
|
+ Reachability *_connectionReachability; ///< 本机网络状态
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
++ (instancetype)defaultReachability {
|
|
|
|
|
+ static RAReachability *reachability;
|
|
|
|
|
+ static dispatch_once_t token;
|
|
|
|
|
+ dispatch_once(&token, ^{
|
|
|
|
|
+ reachability = [[RAReachability alloc] _init];
|
|
|
|
|
+ });
|
|
|
|
|
+ return reachability;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (instancetype)_init {
|
|
|
|
|
+ if (self = [super init]) {
|
|
|
|
|
+ self->_hostReachability = [Reachability reachabilityWithHostName:host];
|
|
|
|
|
+ self->_connectionReachability = [Reachability reachabilityForInternetConnection];
|
|
|
|
|
+
|
|
|
|
|
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityConnectionChanged:) name:kReachabilityChangedNotification object:nil];
|
|
|
|
|
+ }
|
|
|
|
|
+ return self;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)dealloc {
|
|
|
|
|
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)startNotifier:(void(^)(BOOL reachable))reachableBlk {
|
|
|
|
|
+
|
|
|
|
|
+ self.ReachabilityBlk = reachableBlk;
|
|
|
|
|
+
|
|
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
|
|
+
|
|
|
|
|
+ [self->_hostReachability startNotifier];
|
|
|
|
|
+ [self->_connectionReachability startNotifier];
|
|
|
|
|
+
|
|
|
|
|
+ [[NSRunLoop currentRunLoop] run];
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)stopNotifier {
|
|
|
|
|
+ self.ReachabilityBlk = nil;
|
|
|
|
|
+ [self->_hostReachability stopNotifier];
|
|
|
|
|
+ [self->_connectionReachability stopNotifier];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)reachabilityConnectionChanged:(NSNotification *)notification {
|
|
|
|
|
+
|
|
|
|
|
+ Reachability *reachability = [notification object];
|
|
|
|
|
+ if ([reachability isMemberOfClass:[Reachability class]]) {
|
|
|
|
|
+
|
|
|
|
|
+ NetworkStatus status = reachability.currentReachabilityStatus;
|
|
|
|
|
+
|
|
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
+
|
|
|
|
|
+ if (reachability == self->_hostReachability) {
|
|
|
|
|
+
|
|
|
|
|
+ switch (status) {
|
|
|
|
|
+ case NotReachable: {
|
|
|
|
|
+ if (self.ReachabilityBlk) {
|
|
|
|
|
+ self.ReachabilityBlk(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case ReachableViaWiFi: {
|
|
|
|
|
+ if (self.ReachabilityBlk) {
|
|
|
|
|
+ self.ReachabilityBlk(true);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case ReachableViaWWAN: {
|
|
|
|
|
+ if (self.ReachabilityBlk) {
|
|
|
|
|
+ self.ReachabilityBlk(true);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (reachability == self->_connectionReachability) {
|
|
|
|
|
+
|
|
|
|
|
+ switch (status) {
|
|
|
|
|
+ case NotReachable: {
|
|
|
|
|
+ if (self.ReachabilityBlk) {
|
|
|
|
|
+ self.ReachabilityBlk(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default: {
|
|
|
|
|
+ if (self.ReachabilityBlk) {
|
|
|
|
|
+ self.ReachabilityBlk(true);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+@end
|