浏览代码

1.修改iOS Apex Drivers增加网络状态监控。

Pen Li 7 年之前
父节点
当前提交
8491cb17af
共有 2 个文件被更改,包括 148 次插入0 次删除
  1. 21 0
      common/RAReachability.h
  2. 127 0
      common/RAReachability.m

+ 21 - 0
common/RAReachability.h

@@ -0,0 +1,21 @@
+//
+//  RAReachability.h
+//  Apex And Drivers
+//
+//  Created by Jack on 2018/10/23.
+//  Copyright © 2018年 USAI. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface RAReachability : NSObject
+
++ (instancetype)defaultReachability;
+- (void)startNotifier:(void(^)(BOOL reachable))reachableBlk;
+- (void)stopNotifier;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 127 - 0
common/RAReachability.m

@@ -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