├── LICENSE ├── ReadMe.md ├── SGNetObserver.podspec ├── SGNetObserver ├── Reachability.h ├── Reachability.m ├── SGNetObserver.h ├── SGNetObserver.m ├── SimplePing.h ├── SimplePing.m ├── SimplePinger.h └── SimplePinger.m ├── SGNetObserverDemo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── apple.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── apple.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── SGNetObserverDemo.xcscheme │ └── xcschememanagement.plist └── SGNetObserverDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 iOSSinger 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | #iOS完美的网络状态判断工具 2 | 大多数App都严重依赖于网络,一款用户体验良好的的app是必须要考虑网络状态变化的.iOSSinger下一般使用Reachability这个类来检测网络的变化. 3 | ####Reachability 4 | 这个是苹果开发文档里面的一个类,官方写的,用来判断网络的变化,包括无网络,wifi,和蜂窝三种情况.官方地址:[**点我查看**]().Reachability类实际上是苹果公司对SCNetworkReachability API的封装,使用方法比较简单,这里不再介绍.说说它的优缺点: 5 | 6 | * 优点 7 | * 使用简单,只有一个类,官方还有Demo,容易上手 8 | * 灵敏度高,基本网络一有变化,基本马上就能判断出来 9 | * 能够判断有网状态的切换比如2G/3G/4G切换 10 | * 缺点 11 | * 不能判断路由器本身是否能联网 12 | * 能否连接到指定服务器,比如国内访问墙外的服务器 13 | * 有网,但是信号很差,网速很慢,跟没网一样.这时候应该认为无网. 14 | 15 | ####解决方案 16 | 事实上Reachability已经很好了,但是实际上客户端到达服务器需要很多道"关卡",例如路由器,电信服务器,防火墙等.其实说白了就是解决一个问题:***`客户端是否能够成功访问服务器`***.这里介绍另外一个官方的类:SimplePing[**点我查看**](https://developer.apple.com/library/content/samplecode/SimplePing/Introduction/Intro.html#//apple_ref/doc/uid/DTS10000716-Intro-DontLinkElementID_2). 17 | 18 | ####SimplePing 19 | SimplePing也是官方文档的一个类,目的是ping服务器,可以判断客户端是否可以连接到指定服务器.ping 类似于心跳包功能,隔一段时间就ping下服务器,看是否畅通无阻.因此ping不可能做到及时判断网络变化,会有一定的延迟.可能大家已经猜到了我的思路,没错,把他们两个合在一起.下面说说我的思路: 20 | 21 | * 首先利用Reachability判断设备是否联网,至于能不能连接到服务器用ping来检查 22 | * 如果Reachability判断为有网,并且ping也判断为有网,那么表示真的有网,否则就是没网. 23 | * ping 虽然能够判断客户端到服务器是否畅通,但是由于网络抖动或者网络很弱等原因,可能出现ping失败的情况,解决方案就是加上失败次数限制,超过限制就认为断网了. 24 | * 2/3/4G切换的时候,Reachability虽然检测到了网络变化,但是类型还是蜂窝移动,不能给出具体的网络类型.这里可以通过获取状态栏上的属性来判断. 25 | 26 | ``` 27 | - (SGNetworkStatus)netWorkDetailStatus{ 28 | UIApplication *app = [UIApplication sharedApplication]; 29 | UIView *statusBar = [app valueForKeyPath:@"statusBar"]; 30 | UIView *foregroundView = [statusBar valueForKeyPath:@"foregroundView"]; 31 | 32 | UIView *networkView = nil; 33 | 34 | for (UIView *childView in foregroundView.subviews) { 35 | if ([childView isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) { 36 | networkView = childView; 37 | } 38 | } 39 | 40 | SGNetworkStatus status = SGNetworkStatusNone; 41 | 42 | if (networkView) { 43 | int netType = [[networkView valueForKeyPath:@"dataNetworkType"]intValue]; 44 | switch (netType) { 45 | case 0: 46 | status = SGNetworkStatusNone; 47 | break; 48 | case 1://实际上是2G 49 | status = SGNetworkStatusUkonow; 50 | break; 51 | case 2: 52 | status = SGNetworkStatus3G; 53 | break; 54 | case 3: 55 | status = SGNetworkStatus4G; 56 | break; 57 | case 5: 58 | status = SGNetworkStatusWifi; 59 | break; 60 | default: 61 | status = SGNetworkStatusUkonow; 62 | break; 63 | } 64 | } 65 | return status; 66 | } 67 | 68 | ``` 69 | 可喜的是,即使隐藏了状态栏,判断依然有效! 70 | 71 | ####其他细节 72 | * 默认采用host为`www.baidu.com`,别喷我,不是给百度打广告,而是因为百度真的只适合判断有没有网,因为响应真的很快.当然也可以用自己的服务器地址,这样更加真实,万一你家的服务器很渣或者突然crash了也能够完美判断. 73 | * 判断具体网络类型的时候,实际上用kvc获取了控件的私有属性,根据网上的反应,没有因此被拒的情况,因此不用担心.如果因为这个原因被拒,请联系我第一时间修改. 74 | * 支持全局通知和代理的方式.默认全局发送通知,如果设置了'delegate'这个属性,那么只有代理会收到通知,不在发送全局通知.如果想两种方式并存,可以新建一个'SGNetObserver'对象. 75 | 76 | PS:貌似苹果官方的原话是'调用了系统的私有api会被拒'. 77 | 78 | * 支持模拟器,支持IPv4,IPv6 79 | 80 | 详细代码在这里:github地址:,支持cocoapods,欢迎使用! 81 | 82 | 最后,如果有什么不对,欢迎大家留言指正. -------------------------------------------------------------------------------- /SGNetObserver.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |spec| 2 | 3 | spec.name = 'SGNetObserver' 4 | 5 | spec.version = '1.0.0' 6 | 7 | spec.ios.deployment_target = '8.0' 8 | 9 | spec.license = 'MIT' 10 | 11 | spec.homepage = 'https://github.com/iOSSinger' 12 | 13 | spec.author = { "iOSSinger" => "747616044@qq.com" } 14 | 15 | spec.summary = 'iOS完美的网络状态判断工具' 16 | 17 | spec.source = { :git => 'https://github.com/iOSSinger/SGNetObserver.git', :tag => spec.version } 18 | 19 | spec.source_files = "SGNetObserver/**/{*.h,*.m}" 20 | 21 | spec.frameworks = 'SystemConfiguration' 22 | 23 | spec.requires_arc = true 24 | 25 | end 26 | -------------------------------------------------------------------------------- /SGNetObserver/Reachability.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Basic demonstration of how to use the SystemConfiguration Reachablity APIs. 7 | */ 8 | 9 | #import 10 | #import 11 | #import 12 | 13 | typedef enum : NSInteger { 14 | NotReachable = 0, 15 | ReachableViaWiFi, 16 | ReachableViaWWAN 17 | } NetworkStatus; 18 | 19 | #pragma mark IPv6 Support 20 | //Reachability fully support IPv6. For full details, see ReadMe.md. 21 | 22 | @interface Reachability : NSObject 23 | /** 24 | * 回调 25 | */ 26 | @property (nonatomic,copy) void(^networkStatusDidChanged)(); 27 | 28 | /** 29 | * hostname 30 | */ 31 | @property (nonatomic,copy,readonly)NSString *hostName; 32 | 33 | /*! 34 | * Use to check the reachability of a given host name. 35 | */ 36 | + (instancetype)reachabilityWithHostName:(NSString *)hostName; 37 | 38 | /*! 39 | * Use to check the reachability of a given IP address. 40 | */ 41 | + (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress; 42 | 43 | /*! 44 | * Checks whether the default route is available. Should be used by applications that do not connect to a particular host. 45 | */ 46 | + (instancetype)reachabilityForInternetConnection; 47 | 48 | 49 | #pragma mark reachabilityForLocalWiFi 50 | //reachabilityForLocalWiFi has been removed from the sample. See ReadMe.md for more information. 51 | //+ (instancetype)reachabilityForLocalWiFi; 52 | 53 | /*! 54 | * Start listening for reachability notifications on the current run loop. 55 | */ 56 | - (BOOL)startNotifier; 57 | - (void)stopNotifier; 58 | 59 | - (NetworkStatus)currentReachabilityStatus; 60 | 61 | /*! 62 | * WWAN may be available, but not active until a connection has been established. WiFi may require a connection for VPN on Demand. 63 | */ 64 | - (BOOL)connectionRequired; 65 | 66 | @end 67 | 68 | 69 | -------------------------------------------------------------------------------- /SGNetObserver/Reachability.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Basic demonstration of how to use the SystemConfiguration Reachablity APIs. 7 | */ 8 | 9 | #import 10 | #import 11 | #import 12 | #import 13 | #import 14 | 15 | #import 16 | 17 | #import "Reachability.h" 18 | 19 | #pragma mark IPv6 Support 20 | //Reachability fully support IPv6. For full details, see ReadMe.md. 21 | 22 | 23 | #pragma mark - Supporting functions 24 | 25 | #define kShouldPrintReachabilityFlags 1 26 | 27 | static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment) 28 | { 29 | #if kShouldPrintReachabilityFlags 30 | 31 | NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n", 32 | (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', 33 | (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', 34 | 35 | (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', 36 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', 37 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', 38 | (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', 39 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', 40 | (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', 41 | (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-', 42 | comment 43 | ); 44 | #endif 45 | } 46 | 47 | 48 | static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 49 | { 50 | #pragma unused (target, flags) 51 | NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback"); 52 | NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback"); 53 | 54 | Reachability *hostReachability = (__bridge Reachability *)info; 55 | 56 | if (hostReachability.networkStatusDidChanged) { 57 | hostReachability.networkStatusDidChanged(); 58 | } 59 | } 60 | 61 | 62 | #pragma mark - Reachability implementation 63 | @interface Reachability() 64 | 65 | 66 | 67 | @end 68 | 69 | @implementation Reachability 70 | { 71 | SCNetworkReachabilityRef _reachabilityRef; 72 | } 73 | 74 | + (instancetype)reachabilityWithHostName:(NSString *)hostName 75 | { 76 | Reachability* returnValue = NULL; 77 | SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); 78 | if (reachability != NULL) 79 | { 80 | returnValue= [[self alloc] init]; 81 | if (returnValue != NULL) 82 | { 83 | returnValue->_reachabilityRef = reachability; 84 | } 85 | else { 86 | CFRelease(reachability); 87 | } 88 | } 89 | returnValue->_hostName = hostName; 90 | return returnValue; 91 | } 92 | 93 | 94 | + (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress 95 | { 96 | SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress); 97 | 98 | Reachability* returnValue = NULL; 99 | 100 | if (reachability != NULL) 101 | { 102 | returnValue = [[self alloc] init]; 103 | if (returnValue != NULL) 104 | { 105 | returnValue->_reachabilityRef = reachability; 106 | } 107 | else { 108 | CFRelease(reachability); 109 | } 110 | } 111 | return returnValue; 112 | } 113 | 114 | 115 | + (instancetype)reachabilityForInternetConnection 116 | { 117 | struct sockaddr_in zeroAddress; 118 | bzero(&zeroAddress, sizeof(zeroAddress)); 119 | zeroAddress.sin_len = sizeof(zeroAddress); 120 | zeroAddress.sin_family = AF_INET; 121 | 122 | return [self reachabilityWithAddress: (const struct sockaddr *) &zeroAddress]; 123 | } 124 | 125 | #pragma mark reachabilityForLocalWiFi 126 | //reachabilityForLocalWiFi has been removed from the sample. See ReadMe.md for more information. 127 | //+ (instancetype)reachabilityForLocalWiFi 128 | 129 | 130 | 131 | #pragma mark - Start and stop notifier 132 | 133 | - (BOOL)startNotifier 134 | { 135 | BOOL returnValue = NO; 136 | SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 137 | 138 | if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context)) 139 | { 140 | if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) 141 | { 142 | returnValue = YES; 143 | } 144 | } 145 | 146 | return returnValue; 147 | } 148 | 149 | 150 | - (void)stopNotifier 151 | { 152 | if (_reachabilityRef != NULL) 153 | { 154 | SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 155 | } 156 | } 157 | 158 | 159 | - (void)dealloc 160 | { 161 | [self stopNotifier]; 162 | if (_reachabilityRef != NULL) 163 | { 164 | CFRelease(_reachabilityRef); 165 | } 166 | } 167 | 168 | 169 | #pragma mark - Network Flag Handling 170 | 171 | - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags 172 | { 173 | PrintReachabilityFlags(flags, "networkStatusForFlags"); 174 | if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 175 | { 176 | // The target host is not reachable. 177 | return NotReachable; 178 | } 179 | 180 | NetworkStatus returnValue = NotReachable; 181 | 182 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 183 | { 184 | /* 185 | If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi... 186 | */ 187 | returnValue = ReachableViaWiFi; 188 | } 189 | 190 | if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 191 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 192 | { 193 | /* 194 | ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs... 195 | */ 196 | 197 | if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 198 | { 199 | /* 200 | ... and no [user] intervention is needed... 201 | */ 202 | returnValue = ReachableViaWiFi; 203 | } 204 | } 205 | 206 | if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 207 | { 208 | /* 209 | ... but WWAN connections are OK if the calling application is using the CFNetwork APIs. 210 | */ 211 | returnValue = ReachableViaWWAN; 212 | } 213 | 214 | return returnValue; 215 | } 216 | 217 | 218 | - (BOOL)connectionRequired 219 | { 220 | NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef"); 221 | SCNetworkReachabilityFlags flags; 222 | 223 | if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 224 | { 225 | return (flags & kSCNetworkReachabilityFlagsConnectionRequired); 226 | } 227 | 228 | return NO; 229 | } 230 | 231 | 232 | - (NetworkStatus)currentReachabilityStatus 233 | { 234 | NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef"); 235 | NetworkStatus returnValue = NotReachable; 236 | SCNetworkReachabilityFlags flags; 237 | 238 | if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 239 | { 240 | returnValue = [self networkStatusForFlags:flags]; 241 | } 242 | 243 | return returnValue; 244 | } 245 | 246 | 247 | @end 248 | -------------------------------------------------------------------------------- /SGNetObserver/SGNetObserver.h: -------------------------------------------------------------------------------- 1 | // 2 | // SGNetObserver.h 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | * 网络状态变化的全局通知 13 | * info-keys: 14 | * @"status" 网络状态,SGNetworkStatus类型 15 | * @"host" host 地址 16 | */ 17 | extern NSString *SGReachabilityChangedNotification; 18 | 19 | 20 | typedef NS_ENUM(NSUInteger, SGNetworkStatus) { 21 | SGNetworkStatusNone, 22 | SGNetworkStatus3G, 23 | SGNetworkStatus4G, 24 | SGNetworkStatusWifi, 25 | SGNetworkStatusUkonow 26 | }; 27 | 28 | @protocol SGNetworkStatusDelegate 29 | 30 | - (void)observer:(id)obsever host:(NSString *)host networkStatusDidChanged:(SGNetworkStatus)ststus; 31 | 32 | @end 33 | 34 | @interface SGNetObserver : NSObject 35 | /** 36 | * 当前网络状态 37 | */ 38 | @property (nonatomic,assign) SGNetworkStatus networkStatus; 39 | 40 | /** 41 | * delegate,如果设定,只走代理,不发全局通知.否则只发全局通知 42 | */ 43 | @property (nonatomic,weak) id delegate; 44 | 45 | /** 46 | * 是否支持IPv4,默认全部支持 47 | */ 48 | @property (nonatomic,assign) BOOL supportIPv4; 49 | 50 | /** 51 | * 是否支持IPv6 52 | */ 53 | @property (nonatomic,assign) BOOL supportIPv6; 54 | 55 | /** 56 | * 有很小概率ping失败(实际没有断网),设定多少次ping失败认为是断网,默认2次 57 | */ 58 | @property (nonatomic,assign) NSUInteger failureTimes; 59 | 60 | /** 61 | * ping 的频率,默认1s 62 | */ 63 | @property (nonatomic,assign) NSTimeInterval interval; 64 | 65 | /** 66 | * 默认www.baidu.com 67 | */ 68 | + (instancetype)defultObsever; 69 | 70 | /** 71 | * 自定义地址 72 | */ 73 | + (instancetype)observerWithHost:(NSString *)host; 74 | 75 | /** 76 | * 开始监控 77 | */ 78 | - (void)startNotifier; 79 | 80 | /** 81 | * 停止监控 82 | */ 83 | - (void)stopNotifier; 84 | @end 85 | -------------------------------------------------------------------------------- /SGNetObserver/SGNetObserver.m: -------------------------------------------------------------------------------- 1 | // 2 | // SGNetObserver.m 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import "SGNetObserver.h" 10 | #import "Reachability.h" 11 | #import "SimplePinger.h" 12 | 13 | NSString *SGReachabilityChangedNotification = @"SGNetworkReachabilityChangedNotification"; 14 | 15 | @interface SGNetObserver() 16 | 17 | @property (nonatomic,copy) NSString *host; 18 | 19 | @property (nonatomic,strong) Reachability *hostReachability; 20 | 21 | @property (nonatomic,strong) SimplePinger *pinger; 22 | @end 23 | 24 | @implementation SGNetObserver 25 | #pragma mark - 初始化 26 | + (instancetype)defultObsever{ 27 | SGNetObserver *obsever = [[self alloc] init]; 28 | obsever.host = @"www.baidu.com"; 29 | return obsever; 30 | } 31 | 32 | + (instancetype)observerWithHost:(NSString *)host{ 33 | SGNetObserver *obsever = [[self alloc] init]; 34 | obsever.host = host; 35 | return obsever; 36 | } 37 | 38 | - (instancetype)init{ 39 | if (self = [super init]) { 40 | _networkStatus = -1; 41 | _failureTimes = 2; 42 | _interval = 1.0; 43 | } 44 | return self; 45 | } 46 | 47 | - (void)dealloc{ 48 | [self.hostReachability stopNotifier]; 49 | [self.pinger stopNotifier]; 50 | } 51 | #pragma mark - function 52 | 53 | - (void)startNotifier{ 54 | [self.hostReachability startNotifier]; 55 | [self.pinger startNotifier]; 56 | } 57 | 58 | - (void)stopNotifier{ 59 | [self.hostReachability stopNotifier]; 60 | [self.pinger stopNotifier]; 61 | } 62 | 63 | #pragma mark - delegate 64 | - (void)networkStatusDidChanged{ 65 | 66 | //获取两种方法得到的联网状态,并转为BOOL值 67 | BOOL status1 = [self.hostReachability currentReachabilityStatus]; 68 | 69 | BOOL status2 = self.pinger.reachable; 70 | 71 | //综合判断网络,判断原则:Reachability -> pinger 72 | if (status1 && status2) {//有网 73 | self.networkStatus = self.netWorkDetailStatus; 74 | }else{//无网 75 | self.networkStatus = SGNetworkStatusNone; 76 | } 77 | } 78 | 79 | #pragma mark - setter 80 | - (void)setNetworkStatus:(SGNetworkStatus)networkStatus{ 81 | if (_networkStatus != networkStatus) { 82 | _networkStatus = networkStatus; 83 | 84 | NSLog(@"网络状态-----%@",self.networkDict[@(networkStatus)]); 85 | 86 | //有代理 87 | if(self.delegate){//调用代理 88 | if ([self.delegate respondsToSelector:@selector(observer:host:networkStatusDidChanged:)]) { 89 | [self.delegate observer:self host:self.host networkStatusDidChanged:networkStatus]; 90 | } 91 | }else{//发送全局通知 92 | NSDictionary *info = @{@"status" : @(networkStatus), 93 | @"host" : self.host }; 94 | [[NSNotificationCenter defaultCenter] postNotificationName:SGReachabilityChangedNotification object:nil userInfo:info]; 95 | } 96 | } 97 | 98 | } 99 | #pragma mark - getter 100 | 101 | - (Reachability *)hostReachability{ 102 | if (_hostReachability == nil) { 103 | _hostReachability = [Reachability reachabilityWithHostName:self.host]; 104 | 105 | __weak typeof(self) weakSelf = self; 106 | [_hostReachability setNetworkStatusDidChanged:^{ 107 | [weakSelf networkStatusDidChanged]; 108 | }]; 109 | } 110 | return _hostReachability; 111 | } 112 | 113 | - (SimplePinger *)pinger{ 114 | if (_pinger == nil) { 115 | _pinger = [SimplePinger simplePingerWithHostName:self.host]; 116 | _pinger.supportIPv4 = self.supportIPv4; 117 | _pinger.supportIPv6 = self.supportIPv6; 118 | _pinger.interval = self.interval; 119 | _pinger.failureTimes = self.failureTimes; 120 | 121 | __weak typeof(self) weakSelf = self; 122 | [_pinger setNetworkStatusDidChanged:^{ 123 | [weakSelf networkStatusDidChanged]; 124 | }]; 125 | } 126 | return _pinger; 127 | } 128 | #pragma mark - tools 129 | - (SGNetworkStatus)netWorkDetailStatus{ 130 | UIApplication *app = [UIApplication sharedApplication]; 131 | UIView *statusBar = [app valueForKeyPath:@"statusBar"]; 132 | UIView *foregroundView = [statusBar valueForKeyPath:@"foregroundView"]; 133 | 134 | UIView *networkView = nil; 135 | 136 | for (UIView *childView in foregroundView.subviews) { 137 | if ([childView isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) { 138 | networkView = childView; 139 | } 140 | } 141 | 142 | SGNetworkStatus status = SGNetworkStatusNone; 143 | 144 | if (networkView) { 145 | int netType = [[networkView valueForKeyPath:@"dataNetworkType"]intValue]; 146 | switch (netType) { 147 | case 0: 148 | status = SGNetworkStatusNone; 149 | break; 150 | case 1://实际上是2G 151 | status = SGNetworkStatusUkonow; 152 | break; 153 | case 2: 154 | status = SGNetworkStatus3G; 155 | break; 156 | case 3: 157 | status = SGNetworkStatus4G; 158 | break; 159 | case 5: 160 | status = SGNetworkStatusWifi; 161 | break; 162 | default: 163 | status = SGNetworkStatusUkonow; 164 | break; 165 | } 166 | } 167 | return status; 168 | } 169 | 170 | - (NSDictionary *)networkDict{ 171 | return @{ 172 | @(SGNetworkStatusNone) : @"无网络", 173 | @(SGNetworkStatusUkonow) : @"未知网络", 174 | @(SGNetworkStatus3G) : @"3G网络", 175 | @(SGNetworkStatus4G) : @"4G网络", 176 | @(SGNetworkStatusWifi) : @"WIFI网络", 177 | }; 178 | } 179 | @end 180 | -------------------------------------------------------------------------------- /SGNetObserver/SimplePing.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | An object wrapper around the low-level BSD Sockets ping function. 7 | */ 8 | 9 | @import Foundation; 10 | 11 | #include // for __Check_Compile_Time 12 | 13 | NS_ASSUME_NONNULL_BEGIN 14 | 15 | @protocol SimplePingDelegate; 16 | 17 | /*! Controls the IP address version used by SimplePing instances. 18 | */ 19 | 20 | typedef NS_ENUM(NSInteger, SimplePingAddressStyle) { 21 | SimplePingAddressStyleAny, ///< Use the first IPv4 or IPv6 address found; the default. 22 | SimplePingAddressStyleICMPv4, ///< Use the first IPv4 address found. 23 | SimplePingAddressStyleICMPv6 ///< Use the first IPv6 address found. 24 | }; 25 | 26 | /*! An object wrapper around the low-level BSD Sockets ping function. 27 | * \details To use the class create an instance, set the delegate and call `-start` 28 | * to start the instance on the current run loop. If things go well you'll soon get the 29 | * `-simplePing:didStartWithAddress:` delegate callback. From there you can can call 30 | * `-sendPingWithData:` to send a ping and you'll receive the 31 | * `-simplePing:didReceivePingResponsePacket:sequenceNumber:` and 32 | * `-simplePing:didReceiveUnexpectedPacket:` delegate callbacks as ICMP packets arrive. 33 | * 34 | * The class can be used from any thread but the use of any single instance must be 35 | * confined to a specific thread and that thread must run its run loop. 36 | */ 37 | 38 | @interface SimplePing : NSObject 39 | 40 | - (instancetype)init NS_UNAVAILABLE; 41 | 42 | /*! Initialise the object to ping the specified host. 43 | * \param hostName The DNS name of the host to ping; an IPv4 or IPv6 address in string form will 44 | * work here. 45 | * \returns The initialised object. 46 | */ 47 | 48 | - (instancetype)initWithHostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER; 49 | 50 | /*! A copy of the value passed to `-initWithHostName:`. 51 | */ 52 | 53 | @property (nonatomic, copy, readonly) NSString * hostName; 54 | 55 | /*! The delegate for this object. 56 | * \details Delegate callbacks are schedule in the default run loop mode of the run loop of the 57 | * thread that calls `-start`. 58 | */ 59 | 60 | @property (nonatomic, weak, readwrite, nullable) id delegate; 61 | 62 | /*! Controls the IP address version used by the object. 63 | * \details You should set this value before starting the object. 64 | */ 65 | 66 | @property (nonatomic, assign, readwrite) SimplePingAddressStyle addressStyle; 67 | 68 | /*! The address being pinged. 69 | * \details The contents of the NSData is a (struct sockaddr) of some form. The 70 | * value is nil while the object is stopped and remains nil on start until 71 | * `-simplePing:didStartWithAddress:` is called. 72 | */ 73 | 74 | @property (nonatomic, copy, readonly, nullable) NSData * hostAddress; 75 | 76 | /*! The address family for `hostAddress`, or `AF_UNSPEC` if that's nil. 77 | */ 78 | 79 | @property (nonatomic, assign, readonly) sa_family_t hostAddressFamily; 80 | 81 | /*! The identifier used by pings by this object. 82 | * \details When you create an instance of this object it generates a random identifier 83 | * that it uses to identify its own pings. 84 | */ 85 | 86 | @property (nonatomic, assign, readonly) uint16_t identifier; 87 | 88 | /*! The next sequence number to be used by this object. 89 | * \details This value starts at zero and increments each time you send a ping (safely 90 | * wrapping back to zero if necessary). The sequence number is included in the ping, 91 | * allowing you to match up requests and responses, and thus calculate ping times and 92 | * so on. 93 | */ 94 | 95 | @property (nonatomic, assign, readonly) uint16_t nextSequenceNumber; 96 | 97 | /*! Starts the object. 98 | * \details You should set up the delegate and any ping parameters before calling this. 99 | * 100 | * If things go well you'll soon get the `-simplePing:didStartWithAddress:` delegate 101 | * callback, at which point you can start sending pings (via `-sendPingWithData:`) and 102 | * will start receiving ICMP packets (either ping responses, via the 103 | * `-simplePing:didReceivePingResponsePacket:sequenceNumber:` delegate callback, or 104 | * unsolicited ICMP packets, via the `-simplePing:didReceiveUnexpectedPacket:` delegate 105 | * callback). 106 | * 107 | * If the object fails to start, typically because `hostName` doesn't resolve, you'll get 108 | * the `-simplePing:didFailWithError:` delegate callback. 109 | * 110 | * It is not correct to start an already started object. 111 | */ 112 | 113 | - (void)start; 114 | 115 | /*! Sends a ping packet containing the specified data. 116 | * \details Sends an actual ping. 117 | * 118 | * The object must be started when you call this method and, on starting the object, you must 119 | * wait for the `-simplePing:didStartWithAddress:` delegate callback before calling it. 120 | * \param data Some data to include in the ping packet, after the ICMP header, or nil if you 121 | * want the packet to include a standard 56 byte payload (resulting in a standard 64 byte 122 | * ping). 123 | */ 124 | 125 | - (void)sendPingWithData:(nullable NSData *)data; 126 | 127 | /*! Stops the object. 128 | * \details You should call this when you're done pinging. 129 | * 130 | * It's safe to call this on an object that's stopped. 131 | */ 132 | 133 | - (void)stop; 134 | 135 | @end 136 | 137 | /*! A delegate protocol for the SimplePing class. 138 | */ 139 | 140 | @protocol SimplePingDelegate 141 | 142 | @optional 143 | 144 | /*! A SimplePing delegate callback, called once the object has started up. 145 | * \details This is called shortly after you start the object to tell you that the 146 | * object has successfully started. On receiving this callback, you can call 147 | * `-sendPingWithData:` to send pings. 148 | * 149 | * If the object didn't start, `-simplePing:didFailWithError:` is called instead. 150 | * \param pinger The object issuing the callback. 151 | * \param address The address that's being pinged; at the time this delegate callback 152 | * is made, this will have the same value as the `hostAddress` property. 153 | */ 154 | 155 | - (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address; 156 | 157 | /*! A SimplePing delegate callback, called if the object fails to start up. 158 | * \details This is called shortly after you start the object to tell you that the 159 | * object has failed to start. The most likely cause of failure is a problem 160 | * resolving `hostName`. 161 | * 162 | * By the time this callback is called, the object has stopped (that is, you don't 163 | * need to call `-stop` yourself). 164 | * \param pinger The object issuing the callback. 165 | * \param error Describes the failure. 166 | */ 167 | 168 | - (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error; 169 | 170 | /*! A SimplePing delegate callback, called when the object has successfully sent a ping packet. 171 | * \details Each call to `-sendPingWithData:` will result in either a 172 | * `-simplePing:didSendPacket:sequenceNumber:` delegate callback or a 173 | * `-simplePing:didFailToSendPacket:sequenceNumber:error:` delegate callback (unless you 174 | * stop the object before you get the callback). These callbacks are currently delivered 175 | * synchronously from within `-sendPingWithData:`, but this synchronous behaviour is not 176 | * considered API. 177 | * \param pinger The object issuing the callback. 178 | * \param packet The packet that was sent; this includes the ICMP header (`ICMPHeader`) and the 179 | * data you passed to `-sendPingWithData:` but does not include any IP-level headers. 180 | * \param sequenceNumber The ICMP sequence number of that packet. 181 | */ 182 | 183 | - (void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; 184 | 185 | /*! A SimplePing delegate callback, called when the object fails to send a ping packet. 186 | * \details Each call to `-sendPingWithData:` will result in either a 187 | * `-simplePing:didSendPacket:sequenceNumber:` delegate callback or a 188 | * `-simplePing:didFailToSendPacket:sequenceNumber:error:` delegate callback (unless you 189 | * stop the object before you get the callback). These callbacks are currently delivered 190 | * synchronously from within `-sendPingWithData:`, but this synchronous behaviour is not 191 | * considered API. 192 | * \param pinger The object issuing the callback. 193 | * \param packet The packet that was not sent; see `-simplePing:didSendPacket:sequenceNumber:` 194 | * for details. 195 | * \param sequenceNumber The ICMP sequence number of that packet. 196 | * \param error Describes the failure. 197 | */ 198 | 199 | - (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error; 200 | 201 | /*! A SimplePing delegate callback, called when the object receives a ping response. 202 | * \details If the object receives an ping response that matches a ping request that it 203 | * sent, it informs the delegate via this callback. Matching is primarily done based on 204 | * the ICMP identifier, although other criteria are used as well. 205 | * \param pinger The object issuing the callback. 206 | * \param packet The packet received; this includes the ICMP header (`ICMPHeader`) and any data that 207 | * follows that in the ICMP message but does not include any IP-level headers. 208 | * \param sequenceNumber The ICMP sequence number of that packet. 209 | */ 210 | 211 | - (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; 212 | 213 | /*! A SimplePing delegate callback, called when the object receives an unmatched ICMP message. 214 | * \details If the object receives an ICMP message that does not match a ping request that it 215 | * sent, it informs the delegate via this callback. The nature of ICMP handling in a 216 | * BSD kernel makes this a common event because, when an ICMP message arrives, it is 217 | * delivered to all ICMP sockets. 218 | * 219 | * IMPORTANT: This callback is especially common when using IPv6 because IPv6 uses ICMP 220 | * for important network management functions. For example, IPv6 routers periodically 221 | * send out Router Advertisement (RA) packets via Neighbor Discovery Protocol (NDP), which 222 | * is implemented on top of ICMP. 223 | * 224 | * For more on matching, see the discussion associated with 225 | * `-simplePing:didReceivePingResponsePacket:sequenceNumber:`. 226 | * \param pinger The object issuing the callback. 227 | * \param packet The packet received; this includes the ICMP header (`ICMPHeader`) and any data that 228 | * follows that in the ICMP message but does not include any IP-level headers. 229 | */ 230 | 231 | - (void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet; 232 | 233 | @end 234 | 235 | #pragma mark * ICMP On-The-Wire Format 236 | 237 | /*! Describes the on-the-wire header format for an ICMP ping. 238 | * \details This defines the header structure of ping packets on the wire. Both IPv4 and 239 | * IPv6 use the same basic structure. 240 | * 241 | * This is declared in the header because clients of SimplePing might want to use 242 | * it parse received ping packets. 243 | */ 244 | 245 | struct ICMPHeader { 246 | uint8_t type; 247 | uint8_t code; 248 | uint16_t checksum; 249 | uint16_t identifier; 250 | uint16_t sequenceNumber; 251 | // data... 252 | }; 253 | typedef struct ICMPHeader ICMPHeader; 254 | 255 | __Check_Compile_Time(sizeof(ICMPHeader) == 8); 256 | __Check_Compile_Time(offsetof(ICMPHeader, type) == 0); 257 | __Check_Compile_Time(offsetof(ICMPHeader, code) == 1); 258 | __Check_Compile_Time(offsetof(ICMPHeader, checksum) == 2); 259 | __Check_Compile_Time(offsetof(ICMPHeader, identifier) == 4); 260 | __Check_Compile_Time(offsetof(ICMPHeader, sequenceNumber) == 6); 261 | 262 | enum { 263 | ICMPv4TypeEchoRequest = 8, ///< The ICMP `type` for a ping request; in this case `code` is always 0. 264 | ICMPv4TypeEchoReply = 0 ///< The ICMP `type` for a ping response; in this case `code` is always 0. 265 | }; 266 | 267 | enum { 268 | ICMPv6TypeEchoRequest = 128, ///< The ICMP `type` for a ping request; in this case `code` is always 0. 269 | ICMPv6TypeEchoReply = 129 ///< The ICMP `type` for a ping response; in this case `code` is always 0. 270 | }; 271 | 272 | NS_ASSUME_NONNULL_END 273 | -------------------------------------------------------------------------------- /SGNetObserver/SimplePing.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | An object wrapper around the low-level BSD Sockets ping function. 7 | */ 8 | 9 | #import "SimplePing.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #pragma mark * IPv4 and ICMPv4 On-The-Wire Format 16 | 17 | /*! Describes the on-the-wire header format for an IPv4 packet. 18 | * \details This defines the header structure of IPv4 packets on the wire. We need 19 | * this in order to skip this header in the IPv4 case, where the kernel passes 20 | * it to us for no obvious reason. 21 | */ 22 | 23 | struct IPv4Header { 24 | uint8_t versionAndHeaderLength; 25 | uint8_t differentiatedServices; 26 | uint16_t totalLength; 27 | uint16_t identification; 28 | uint16_t flagsAndFragmentOffset; 29 | uint8_t timeToLive; 30 | uint8_t protocol; 31 | uint16_t headerChecksum; 32 | uint8_t sourceAddress[4]; 33 | uint8_t destinationAddress[4]; 34 | // options... 35 | // data... 36 | }; 37 | typedef struct IPv4Header IPv4Header; 38 | 39 | __Check_Compile_Time(sizeof(IPv4Header) == 20); 40 | __Check_Compile_Time(offsetof(IPv4Header, versionAndHeaderLength) == 0); 41 | __Check_Compile_Time(offsetof(IPv4Header, differentiatedServices) == 1); 42 | __Check_Compile_Time(offsetof(IPv4Header, totalLength) == 2); 43 | __Check_Compile_Time(offsetof(IPv4Header, identification) == 4); 44 | __Check_Compile_Time(offsetof(IPv4Header, flagsAndFragmentOffset) == 6); 45 | __Check_Compile_Time(offsetof(IPv4Header, timeToLive) == 8); 46 | __Check_Compile_Time(offsetof(IPv4Header, protocol) == 9); 47 | __Check_Compile_Time(offsetof(IPv4Header, headerChecksum) == 10); 48 | __Check_Compile_Time(offsetof(IPv4Header, sourceAddress) == 12); 49 | __Check_Compile_Time(offsetof(IPv4Header, destinationAddress) == 16); 50 | 51 | /*! Calculates an IP checksum. 52 | * \details This is the standard BSD checksum code, modified to use modern types. 53 | * \param buffer A pointer to the data to checksum. 54 | * \param bufferLen The length of that data. 55 | * \returns The checksum value, in network byte order. 56 | */ 57 | 58 | static uint16_t in_cksum(const void *buffer, size_t bufferLen) { 59 | // 60 | size_t bytesLeft; 61 | int32_t sum; 62 | const uint16_t * cursor; 63 | union { 64 | uint16_t us; 65 | uint8_t uc[2]; 66 | } last; 67 | uint16_t answer; 68 | 69 | bytesLeft = bufferLen; 70 | sum = 0; 71 | cursor = buffer; 72 | 73 | /* 74 | * Our algorithm is simple, using a 32 bit accumulator (sum), we add 75 | * sequential 16 bit words to it, and at the end, fold back all the 76 | * carry bits from the top 16 bits into the lower 16 bits. 77 | */ 78 | while (bytesLeft > 1) { 79 | sum += *cursor; 80 | cursor += 1; 81 | bytesLeft -= 2; 82 | } 83 | 84 | /* mop up an odd byte, if necessary */ 85 | if (bytesLeft == 1) { 86 | last.uc[0] = * (const uint8_t *) cursor; 87 | last.uc[1] = 0; 88 | sum += last.us; 89 | } 90 | 91 | /* add back carry outs from top 16 bits to low 16 bits */ 92 | sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 93 | sum += (sum >> 16); /* add carry */ 94 | answer = (uint16_t) ~sum; /* truncate to 16 bits */ 95 | 96 | return answer; 97 | } 98 | 99 | #pragma mark * SimplePing 100 | 101 | @interface SimplePing () 102 | 103 | // read/write versions of public properties 104 | 105 | @property (nonatomic, copy, readwrite, nullable) NSData * hostAddress; 106 | @property (nonatomic, assign, readwrite ) uint16_t nextSequenceNumber; 107 | 108 | // private properties 109 | 110 | /*! True if nextSequenceNumber has wrapped from 65535 to 0. 111 | */ 112 | 113 | @property (nonatomic, assign, readwrite) BOOL nextSequenceNumberHasWrapped; 114 | 115 | /*! A host object for name-to-address resolution. 116 | */ 117 | 118 | @property (nonatomic, strong, readwrite, nullable) CFHostRef host __attribute__ ((NSObject)); 119 | 120 | /*! A socket object for ICMP send and receive. 121 | */ 122 | 123 | @property (nonatomic, strong, readwrite, nullable) CFSocketRef socket __attribute__ ((NSObject)); 124 | 125 | @end 126 | 127 | @implementation SimplePing 128 | 129 | - (instancetype)initWithHostName:(NSString *)hostName { 130 | NSParameterAssert(hostName != nil); 131 | self = [super init]; 132 | if (self != nil) { 133 | self->_hostName = [hostName copy]; 134 | self->_identifier = (uint16_t) arc4random(); 135 | } 136 | return self; 137 | } 138 | 139 | - (void)dealloc { 140 | [self stop]; 141 | // Double check that -stop took care of _host and _socket. 142 | assert(self->_host == NULL); 143 | assert(self->_socket == NULL); 144 | } 145 | 146 | - (sa_family_t)hostAddressFamily { 147 | sa_family_t result; 148 | 149 | result = AF_UNSPEC; 150 | if ( (self.hostAddress != nil) && (self.hostAddress.length >= sizeof(struct sockaddr)) ) { 151 | result = ((const struct sockaddr *) self.hostAddress.bytes)->sa_family; 152 | } 153 | return result; 154 | } 155 | 156 | /*! Shuts down the pinger object and tell the delegate about the error. 157 | * \param error Describes the failure. 158 | */ 159 | 160 | - (void)didFailWithError:(NSError *)error { 161 | id strongDelegate; 162 | 163 | assert(error != nil); 164 | 165 | // We retain ourselves temporarily because it's common for the delegate method 166 | // to release its last reference to us, which causes -dealloc to be called here. 167 | // If we then reference self on the return path, things go badly. I don't think 168 | // that happens currently, but I've got into the habit of doing this as a 169 | // defensive measure. 170 | 171 | CFAutorelease( CFBridgingRetain( self )); 172 | 173 | [self stop]; 174 | strongDelegate = self.delegate; 175 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didFailWithError:)] ) { 176 | [strongDelegate simplePing:self didFailWithError:error]; 177 | } 178 | } 179 | 180 | /*! Shuts down the pinger object and tell the delegate about the error. 181 | * \details This converts the CFStreamError to an NSError and then call through to 182 | * -didFailWithError: to do the real work. 183 | * \param streamError Describes the failure. 184 | */ 185 | 186 | - (void)didFailWithHostStreamError:(CFStreamError)streamError { 187 | NSDictionary * userInfo; 188 | NSError * error; 189 | 190 | if (streamError.domain == kCFStreamErrorDomainNetDB) { 191 | userInfo = @{(id) kCFGetAddrInfoFailureKey: @(streamError.error)}; 192 | } else { 193 | userInfo = nil; 194 | } 195 | error = [NSError errorWithDomain:(NSString *) kCFErrorDomainCFNetwork code:kCFHostErrorUnknown userInfo:userInfo]; 196 | 197 | [self didFailWithError:error]; 198 | } 199 | 200 | /*! Builds a ping packet from the supplied parameters. 201 | * \param type The packet type, which is different for IPv4 and IPv6. 202 | * \param payload Data to place after the ICMP header. 203 | * \param requiresChecksum Determines whether a checksum is calculated (IPv4) or not (IPv6). 204 | * \returns A ping packet suitable to be passed to the kernel. 205 | */ 206 | 207 | - (NSData *)pingPacketWithType:(uint8_t)type payload:(NSData *)payload requiresChecksum:(BOOL)requiresChecksum { 208 | NSMutableData * packet; 209 | ICMPHeader * icmpPtr; 210 | 211 | packet = [NSMutableData dataWithLength:sizeof(*icmpPtr) + payload.length]; 212 | assert(packet != nil); 213 | 214 | icmpPtr = packet.mutableBytes; 215 | icmpPtr->type = type; 216 | icmpPtr->code = 0; 217 | icmpPtr->checksum = 0; 218 | icmpPtr->identifier = OSSwapHostToBigInt16(self.identifier); 219 | icmpPtr->sequenceNumber = OSSwapHostToBigInt16(self.nextSequenceNumber); 220 | memcpy(&icmpPtr[1], [payload bytes], [payload length]); 221 | 222 | if (requiresChecksum) { 223 | // The IP checksum routine returns a 16-bit number that's already in correct byte order 224 | // (due to wacky 1's complement maths), so we just put it into the packet as a 16-bit unit. 225 | 226 | icmpPtr->checksum = in_cksum(packet.bytes, packet.length); 227 | } 228 | 229 | return packet; 230 | } 231 | 232 | - (void)sendPingWithData:(NSData *)data { 233 | int err; 234 | NSData * payload; 235 | NSData * packet; 236 | ssize_t bytesSent; 237 | id strongDelegate; 238 | 239 | // data may be nil 240 | NSParameterAssert(self.hostAddress != nil); // gotta wait for -simplePing:didStartWithAddress: 241 | 242 | // Construct the ping packet. 243 | 244 | payload = data; 245 | if (payload == nil) { 246 | payload = [[NSString stringWithFormat:@"%28zd bottles of beer on the wall", (ssize_t) 99 - (size_t) (self.nextSequenceNumber % 100) ] dataUsingEncoding:NSASCIIStringEncoding]; 247 | assert(payload != nil); 248 | 249 | // Our dummy payload is sized so that the resulting ICMP packet, including the ICMPHeader, is 250 | // 64-bytes, which makes it easier to recognise our packets on the wire. 251 | 252 | assert([payload length] == 56); 253 | } 254 | 255 | switch (self.hostAddressFamily) { 256 | case AF_INET: { 257 | packet = [self pingPacketWithType:ICMPv4TypeEchoRequest payload:payload requiresChecksum:YES]; 258 | } break; 259 | case AF_INET6: { 260 | packet = [self pingPacketWithType:ICMPv6TypeEchoRequest payload:payload requiresChecksum:NO]; 261 | } break; 262 | default: { 263 | assert(NO); 264 | } break; 265 | } 266 | assert(packet != nil); 267 | 268 | // Send the packet. 269 | 270 | if (self.socket == NULL) { 271 | bytesSent = -1; 272 | err = EBADF; 273 | } else { 274 | bytesSent = sendto( 275 | CFSocketGetNative(self.socket), 276 | packet.bytes, 277 | packet.length, 278 | 0, 279 | self.hostAddress.bytes, 280 | (socklen_t) self.hostAddress.length 281 | ); 282 | err = 0; 283 | if (bytesSent < 0) { 284 | err = errno; 285 | } 286 | } 287 | 288 | // Handle the results of the send. 289 | 290 | strongDelegate = self.delegate; 291 | if ( (bytesSent > 0) && (((NSUInteger) bytesSent) == packet.length) ) { 292 | 293 | // Complete success. Tell the client. 294 | 295 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didSendPacket:sequenceNumber:)] ) { 296 | [strongDelegate simplePing:self didSendPacket:packet sequenceNumber:self.nextSequenceNumber]; 297 | } 298 | } else { 299 | NSError * error; 300 | 301 | // Some sort of failure. Tell the client. 302 | 303 | if (err == 0) { 304 | err = ENOBUFS; // This is not a hugely descriptor error, alas. 305 | } 306 | error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]; 307 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didFailToSendPacket:sequenceNumber:error:)] ) { 308 | [strongDelegate simplePing:self didFailToSendPacket:packet sequenceNumber:self.nextSequenceNumber error:error]; 309 | } 310 | } 311 | 312 | self.nextSequenceNumber += 1; 313 | if (self.nextSequenceNumber == 0) { 314 | self.nextSequenceNumberHasWrapped = YES; 315 | } 316 | } 317 | 318 | /*! Calculates the offset of the ICMP header within an IPv4 packet. 319 | * \details In the IPv4 case the kernel returns us a buffer that includes the 320 | * IPv4 header. We're not interested in that, so we have to skip over it. 321 | * This code does a rough check of the IPv4 header and, if it looks OK, 322 | * returns the offset of the ICMP header. 323 | * \param packet The IPv4 packet, as returned to us by the kernel. 324 | * \returns The offset of the ICMP header, or NSNotFound. 325 | */ 326 | 327 | + (NSUInteger)icmpHeaderOffsetInIPv4Packet:(NSData *)packet { 328 | // Returns the offset of the ICMPv4Header within an IP packet. 329 | NSUInteger result; 330 | const struct IPv4Header * ipPtr; 331 | size_t ipHeaderLength; 332 | 333 | result = NSNotFound; 334 | if (packet.length >= (sizeof(IPv4Header) + sizeof(ICMPHeader))) { 335 | ipPtr = (const IPv4Header *) packet.bytes; 336 | if ( ((ipPtr->versionAndHeaderLength & 0xF0) == 0x40) && // IPv4 337 | ( ipPtr->protocol == IPPROTO_ICMP ) ) { 338 | ipHeaderLength = (ipPtr->versionAndHeaderLength & 0x0F) * sizeof(uint32_t); 339 | if (packet.length >= (ipHeaderLength + sizeof(ICMPHeader))) { 340 | result = ipHeaderLength; 341 | } 342 | } 343 | } 344 | return result; 345 | } 346 | 347 | /*! Checks whether the specified sequence number is one we sent. 348 | * \param sequenceNumber The incoming sequence number. 349 | * \returns YES if the sequence number looks like one we sent. 350 | */ 351 | 352 | - (BOOL)validateSequenceNumber:(uint16_t)sequenceNumber { 353 | if (self.nextSequenceNumberHasWrapped) { 354 | // If the sequence numbers have wrapped that we can't reliably check 355 | // whether this is a sequence number we sent. Rather, we check to see 356 | // whether the sequence number is within the last 120 sequence numbers 357 | // we sent. Note that the uint16_t subtraction here does the right 358 | // thing regardless of the wrapping. 359 | // 360 | // Why 120? Well, if we send one ping per second, 120 is 2 minutes, which 361 | // is the standard "max time a packet can bounce around the Internet" value. 362 | return ((uint16_t) (self.nextSequenceNumber - sequenceNumber)) < (uint16_t) 120; 363 | } else { 364 | return sequenceNumber < self.nextSequenceNumber; 365 | } 366 | } 367 | 368 | /*! Checks whether an incoming IPv4 packet looks like a ping response. 369 | * \details This routine modifies this `packet` data! It does this for two reasons: 370 | * 371 | * * It needs to zero out the `checksum` field of the ICMPHeader in order to do 372 | * its checksum calculation. 373 | * 374 | * * It removes the IPv4 header from the front of the packet. 375 | * \param packet The IPv4 packet, as returned to us by the kernel. 376 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 377 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 378 | */ 379 | 380 | - (BOOL)validatePing4ResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 381 | BOOL result; 382 | NSUInteger icmpHeaderOffset; 383 | ICMPHeader * icmpPtr; 384 | uint16_t receivedChecksum; 385 | uint16_t calculatedChecksum; 386 | 387 | result = NO; 388 | 389 | icmpHeaderOffset = [[self class] icmpHeaderOffsetInIPv4Packet:packet]; 390 | if (icmpHeaderOffset != NSNotFound) { 391 | icmpPtr = (struct ICMPHeader *) (((uint8_t *) packet.mutableBytes) + icmpHeaderOffset); 392 | 393 | receivedChecksum = icmpPtr->checksum; 394 | icmpPtr->checksum = 0; 395 | calculatedChecksum = in_cksum(icmpPtr, packet.length - icmpHeaderOffset); 396 | icmpPtr->checksum = receivedChecksum; 397 | 398 | if (receivedChecksum == calculatedChecksum) { 399 | if ( (icmpPtr->type == ICMPv4TypeEchoReply) && (icmpPtr->code == 0) ) { 400 | if ( OSSwapBigToHostInt16(icmpPtr->identifier) == self.identifier ) { 401 | uint16_t sequenceNumber; 402 | 403 | sequenceNumber = OSSwapBigToHostInt16(icmpPtr->sequenceNumber); 404 | if ([self validateSequenceNumber:sequenceNumber]) { 405 | 406 | // Remove the IPv4 header off the front of the data we received, leaving us with 407 | // just the ICMP header and the ping payload. 408 | [packet replaceBytesInRange:NSMakeRange(0, icmpHeaderOffset) withBytes:NULL length:0]; 409 | 410 | *sequenceNumberPtr = sequenceNumber; 411 | result = YES; 412 | } 413 | } 414 | } 415 | } 416 | } 417 | 418 | return result; 419 | } 420 | 421 | /*! Checks whether an incoming IPv6 packet looks like a ping response. 422 | * \param packet The IPv6 packet, as returned to us by the kernel; note that this routine 423 | * could modify this data but does not need to in the IPv6 case. 424 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 425 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 426 | */ 427 | 428 | - (BOOL)validatePing6ResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 429 | BOOL result; 430 | const ICMPHeader * icmpPtr; 431 | 432 | result = NO; 433 | 434 | if (packet.length >= sizeof(*icmpPtr)) { 435 | icmpPtr = packet.bytes; 436 | 437 | // In the IPv6 case we don't check the checksum because that's hard (we need to 438 | // cook up an IPv6 pseudo header and we don't have the ingredients) and unnecessary 439 | // (the kernel has already done this check). 440 | 441 | if ( (icmpPtr->type == ICMPv6TypeEchoReply) && (icmpPtr->code == 0) ) { 442 | if ( OSSwapBigToHostInt16(icmpPtr->identifier) == self.identifier ) { 443 | uint16_t sequenceNumber; 444 | 445 | sequenceNumber = OSSwapBigToHostInt16(icmpPtr->sequenceNumber); 446 | if ([self validateSequenceNumber:sequenceNumber]) { 447 | *sequenceNumberPtr = sequenceNumber; 448 | result = YES; 449 | } 450 | } 451 | } 452 | } 453 | return result; 454 | } 455 | 456 | /*! Checks whether an incoming packet looks like a ping response. 457 | * \param packet The packet, as returned to us by the kernel; note that may end up modifying 458 | * this data. 459 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 460 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 461 | */ 462 | 463 | - (BOOL)validatePingResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 464 | BOOL result; 465 | 466 | switch (self.hostAddressFamily) { 467 | case AF_INET: { 468 | result = [self validatePing4ResponsePacket:packet sequenceNumber:sequenceNumberPtr]; 469 | } break; 470 | case AF_INET6: { 471 | result = [self validatePing6ResponsePacket:packet sequenceNumber:sequenceNumberPtr]; 472 | } break; 473 | default: { 474 | assert(NO); 475 | result = NO; 476 | } break; 477 | } 478 | return result; 479 | } 480 | 481 | /*! Reads data from the ICMP socket. 482 | * \details Called by the socket handling code (SocketReadCallback) to process an ICMP 483 | * message waiting on the socket. 484 | */ 485 | 486 | - (void)readData { 487 | int err; 488 | struct sockaddr_storage addr; 489 | socklen_t addrLen; 490 | ssize_t bytesRead; 491 | void * buffer; 492 | enum { kBufferSize = 65535 }; 493 | 494 | // 65535 is the maximum IP packet size, which seems like a reasonable bound 495 | // here (plus it's what uses). 496 | 497 | buffer = malloc(kBufferSize); 498 | assert(buffer != NULL); 499 | 500 | // Actually read the data. We use recvfrom(), and thus get back the source address, 501 | // but we don't actually do anything with it. It would be trivial to pass it to 502 | // the delegate but we don't need it in this example. 503 | 504 | addrLen = sizeof(addr); 505 | bytesRead = recvfrom(CFSocketGetNative(self.socket), buffer, kBufferSize, 0, (struct sockaddr *) &addr, &addrLen); 506 | err = 0; 507 | if (bytesRead < 0) { 508 | err = errno; 509 | } 510 | 511 | // Process the data we read. 512 | 513 | if (bytesRead > 0) { 514 | NSMutableData * packet; 515 | id strongDelegate; 516 | uint16_t sequenceNumber; 517 | 518 | packet = [NSMutableData dataWithBytes:buffer length:(NSUInteger) bytesRead]; 519 | assert(packet != nil); 520 | 521 | // We got some data, pass it up to our client. 522 | 523 | strongDelegate = self.delegate; 524 | if ( [self validatePingResponsePacket:packet sequenceNumber:&sequenceNumber] ) { 525 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didReceivePingResponsePacket:sequenceNumber:)] ) { 526 | [strongDelegate simplePing:self didReceivePingResponsePacket:packet sequenceNumber:sequenceNumber]; 527 | } 528 | } else { 529 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didReceiveUnexpectedPacket:)] ) { 530 | [strongDelegate simplePing:self didReceiveUnexpectedPacket:packet]; 531 | } 532 | } 533 | } else { 534 | 535 | // We failed to read the data, so shut everything down. 536 | 537 | if (err == 0) { 538 | err = EPIPE; 539 | } 540 | [self didFailWithError:[NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]]; 541 | } 542 | 543 | free(buffer); 544 | 545 | // Note that we don't loop back trying to read more data. Rather, we just 546 | // let CFSocket call us again. 547 | } 548 | 549 | /*! The callback for our CFSocket object. 550 | * \details This simply routes the call to our `-readData` method. 551 | * \param s See the documentation for CFSocketCallBack. 552 | * \param type See the documentation for CFSocketCallBack. 553 | * \param address See the documentation for CFSocketCallBack. 554 | * \param data See the documentation for CFSocketCallBack. 555 | * \param info See the documentation for CFSocketCallBack; this is actually a pointer to the 556 | * 'owning' object. 557 | */ 558 | 559 | static void SocketReadCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { 560 | // This C routine is called by CFSocket when there's data waiting on our 561 | // ICMP socket. It just redirects the call to Objective-C code. 562 | SimplePing * obj; 563 | 564 | obj = (__bridge SimplePing *) info; 565 | assert([obj isKindOfClass:[SimplePing class]]); 566 | 567 | #pragma unused(s) 568 | assert(s == obj.socket); 569 | #pragma unused(type) 570 | assert(type == kCFSocketReadCallBack); 571 | #pragma unused(address) 572 | assert(address == nil); 573 | #pragma unused(data) 574 | assert(data == nil); 575 | 576 | [obj readData]; 577 | } 578 | 579 | /*! Starts the send and receive infrastructure. 580 | * \details This is called once we've successfully resolved `hostName` in to 581 | * `hostAddress`. It's responsible for setting up the socket for sending and 582 | * receiving pings. 583 | */ 584 | 585 | - (void)startWithHostAddress { 586 | int err; 587 | int fd; 588 | 589 | assert(self.hostAddress != nil); 590 | 591 | // Open the socket. 592 | 593 | fd = -1; 594 | err = 0; 595 | switch (self.hostAddressFamily) { 596 | case AF_INET: { 597 | fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); 598 | if (fd < 0) { 599 | err = errno; 600 | } 601 | } break; 602 | case AF_INET6: { 603 | fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6); 604 | if (fd < 0) { 605 | err = errno; 606 | } 607 | } break; 608 | default: { 609 | err = EPROTONOSUPPORT; 610 | } break; 611 | } 612 | 613 | if (err != 0) { 614 | [self didFailWithError:[NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]]; 615 | } else { 616 | CFSocketContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 617 | CFRunLoopSourceRef rls; 618 | id strongDelegate; 619 | 620 | // Wrap it in a CFSocket and schedule it on the runloop. 621 | 622 | self.socket = (CFSocketRef) CFAutorelease( CFSocketCreateWithNative(NULL, fd, kCFSocketReadCallBack, SocketReadCallback, &context) ); 623 | assert(self.socket != NULL); 624 | 625 | // The socket will now take care of cleaning up our file descriptor. 626 | 627 | assert( CFSocketGetSocketFlags(self.socket) & kCFSocketCloseOnInvalidate ); 628 | fd = -1; 629 | 630 | rls = CFSocketCreateRunLoopSource(NULL, self.socket, 0); 631 | assert(rls != NULL); 632 | 633 | CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode); 634 | 635 | CFRelease(rls); 636 | 637 | strongDelegate = self.delegate; 638 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(simplePing:didStartWithAddress:)] ) { 639 | [strongDelegate simplePing:self didStartWithAddress:self.hostAddress]; 640 | } 641 | } 642 | assert(fd == -1); 643 | } 644 | 645 | /*! Processes the results of our name-to-address resolution. 646 | * \details Called by our CFHost resolution callback (HostResolveCallback) when host 647 | * resolution is complete. We just latch the first appropriate address and kick 648 | * off the send and receive infrastructure. 649 | */ 650 | 651 | - (void)hostResolutionDone { 652 | Boolean resolved; 653 | NSArray * addresses; 654 | 655 | // Find the first appropriate address. 656 | 657 | addresses = (__bridge NSArray *) CFHostGetAddressing(self.host, &resolved); 658 | if ( resolved && (addresses != nil) ) { 659 | resolved = false; 660 | for (NSData * address in addresses) { 661 | const struct sockaddr * addrPtr; 662 | 663 | addrPtr = (const struct sockaddr *) address.bytes; 664 | if ( address.length >= sizeof(struct sockaddr) ) { 665 | switch (addrPtr->sa_family) { 666 | case AF_INET: { 667 | if (self.addressStyle != SimplePingAddressStyleICMPv6) { 668 | self.hostAddress = address; 669 | resolved = true; 670 | } 671 | } break; 672 | case AF_INET6: { 673 | if (self.addressStyle != SimplePingAddressStyleICMPv4) { 674 | self.hostAddress = address; 675 | resolved = true; 676 | } 677 | } break; 678 | } 679 | } 680 | if (resolved) { 681 | break; 682 | } 683 | } 684 | } 685 | 686 | // We're done resolving, so shut that down. 687 | 688 | [self stopHostResolution]; 689 | 690 | // If all is OK, start the send and receive infrastructure, otherwise stop. 691 | 692 | if (resolved) { 693 | [self startWithHostAddress]; 694 | } else { 695 | [self didFailWithError:[NSError errorWithDomain:(NSString *)kCFErrorDomainCFNetwork code:kCFHostErrorHostNotFound userInfo:nil]]; 696 | } 697 | } 698 | 699 | /*! The callback for our CFHost object. 700 | * \details This simply routes the call to our `-hostResolutionDone` or 701 | * `-didFailWithHostStreamError:` methods. 702 | * \param theHost See the documentation for CFHostClientCallBack. 703 | * \param typeInfo See the documentation for CFHostClientCallBack. 704 | * \param error See the documentation for CFHostClientCallBack. 705 | * \param info See the documentation for CFHostClientCallBack; this is actually a pointer to 706 | * the 'owning' object. 707 | */ 708 | 709 | static void HostResolveCallback(CFHostRef theHost, CFHostInfoType typeInfo, const CFStreamError *error, void *info) { 710 | // This C routine is called by CFHost when the host resolution is complete. 711 | // It just redirects the call to the appropriate Objective-C method. 712 | SimplePing * obj; 713 | 714 | obj = (__bridge SimplePing *) info; 715 | assert([obj isKindOfClass:[SimplePing class]]); 716 | 717 | #pragma unused(theHost) 718 | assert(theHost == obj.host); 719 | #pragma unused(typeInfo) 720 | assert(typeInfo == kCFHostAddresses); 721 | 722 | if ( (error != NULL) && (error->domain != 0) ) { 723 | [obj didFailWithHostStreamError:*error]; 724 | } else { 725 | [obj hostResolutionDone]; 726 | } 727 | } 728 | 729 | - (void)start { 730 | Boolean success; 731 | CFHostClientContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 732 | CFStreamError streamError; 733 | 734 | assert(self.host == NULL); 735 | assert(self.hostAddress == nil); 736 | 737 | self.host = (CFHostRef) CFAutorelease( CFHostCreateWithName(NULL, (__bridge CFStringRef) self.hostName) ); 738 | assert(self.host != NULL); 739 | 740 | CFHostSetClient(self.host, HostResolveCallback, &context); 741 | 742 | CFHostScheduleWithRunLoop(self.host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 743 | 744 | success = CFHostStartInfoResolution(self.host, kCFHostAddresses, &streamError); 745 | if ( ! success ) { 746 | [self didFailWithHostStreamError:streamError]; 747 | } 748 | } 749 | 750 | /*! Stops the name-to-address resolution infrastructure. 751 | */ 752 | 753 | - (void)stopHostResolution { 754 | // Shut down the CFHost. 755 | if (self.host != NULL) { 756 | CFHostSetClient(self.host, NULL, NULL); 757 | CFHostUnscheduleFromRunLoop(self.host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 758 | self.host = NULL; 759 | } 760 | } 761 | 762 | /*! Stops the send and receive infrastructure. 763 | */ 764 | 765 | - (void)stopSocket { 766 | if (self.socket != NULL) { 767 | CFSocketInvalidate(self.socket); 768 | self.socket = NULL; 769 | } 770 | } 771 | 772 | - (void)stop { 773 | [self stopHostResolution]; 774 | [self stopSocket]; 775 | 776 | // Junk the host address on stop. If the client calls -start again, we'll 777 | // re-resolve the host name. 778 | 779 | self.hostAddress = NULL; 780 | } 781 | 782 | @end 783 | -------------------------------------------------------------------------------- /SGNetObserver/SimplePinger.h: -------------------------------------------------------------------------------- 1 | // 2 | // SimpleHeartbeat.h 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/20. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "SimplePing.h" 11 | 12 | @interface SimplePinger : NSObject 13 | 14 | /** 15 | * 是否ping的通 16 | */ 17 | @property (nonatomic,assign) BOOL reachable; 18 | 19 | /** 20 | * 有很小概率ping失败,设定多少次ping失败认为是断网,默认2次, 必须 >= 2 21 | */ 22 | @property (nonatomic,assign) NSUInteger failureTimes; 23 | 24 | /** 25 | * ping 的频率,默认1s 26 | */ 27 | @property (nonatomic,assign) NSTimeInterval interval; 28 | 29 | /** 30 | * 是否支持IPv4,默认全部支持 31 | */ 32 | @property (nonatomic,assign) BOOL supportIPv4; 33 | 34 | /** 35 | * 是否支持IPv6 36 | */ 37 | @property (nonatomic,assign) BOOL supportIPv6; 38 | 39 | /** 40 | * 回调 41 | */ 42 | @property (nonatomic,copy) void(^networkStatusDidChanged)(); 43 | 44 | 45 | + (instancetype)simplePingerWithHostName:(NSString *)hostName; 46 | 47 | - (void)startNotifier; 48 | 49 | - (void)stopNotifier; 50 | @end 51 | -------------------------------------------------------------------------------- /SGNetObserver/SimplePinger.m: -------------------------------------------------------------------------------- 1 | // 2 | // SimpleHeartbeat.m 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/20. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import "SimplePinger.h" 10 | #import "SimplePing.h" 11 | #include 12 | 13 | @interface SimplePinger() 14 | 15 | @property (nonatomic,strong) SimplePing *pinger; 16 | 17 | @property (nonatomic,copy) NSString *hostName; 18 | 19 | @property (nonatomic,strong) NSTimer *sendTimer; 20 | 21 | /** 22 | * 发送失败记录,失败次数为failuretimes时,认为断网. 23 | */ 24 | @property (nonatomic,strong) NSMutableArray *array; 25 | 26 | @end 27 | 28 | @implementation SimplePinger 29 | 30 | #pragma mark - 初始化 31 | + (instancetype)simplePingerWithHostName:(NSString *)hostName{ 32 | SimplePinger *pinger = [[SimplePinger alloc] init]; 33 | pinger.hostName = hostName; 34 | return pinger; 35 | } 36 | 37 | - (instancetype)init{ 38 | if(self = [super init]){ 39 | self.interval = 1.0; 40 | self.failureTimes = 2; 41 | self.reachable = YES; 42 | } 43 | return self; 44 | } 45 | - (void)dealloc{ 46 | 47 | [self stopPing]; 48 | [self.array removeAllObjects]; 49 | [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sendPing) object:nil]; 50 | } 51 | #pragma mark - function 52 | 53 | - (void)startNotifier{ 54 | [self startForceIPv4:self.supportIPv4 forceIPv6:self.supportIPv6]; 55 | } 56 | 57 | - (void)stopNotifier{ 58 | [self stopPing]; 59 | } 60 | 61 | - (void)startForceIPv4:(BOOL)forceIPv4 forceIPv6:(BOOL)forceIPv6{ 62 | self.pinger = [[SimplePing alloc] initWithHostName:self.hostName]; 63 | if (forceIPv4 && !forceIPv6) { 64 | self.pinger.addressStyle = SimplePingAddressStyleICMPv4; 65 | }else if (forceIPv6 && !forceIPv4){ 66 | self.pinger.addressStyle = SimplePingAddressStyleICMPv6; 67 | }else{ 68 | self.pinger.addressStyle = SimplePingAddressStyleAny; 69 | } 70 | 71 | self.pinger.delegate = self; 72 | 73 | [self.pinger start]; 74 | } 75 | 76 | - (void)sendPing{ 77 | [self.pinger sendPingWithData:nil]; 78 | } 79 | 80 | - (void)stopPing{ 81 | [self.pinger stop]; 82 | self.pinger.delegate = nil; 83 | self.pinger = nil; 84 | 85 | [self.sendTimer invalidate]; 86 | self.sendTimer = nil; 87 | } 88 | 89 | #pragma mark - delegaet 90 | - (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address{ 91 | NSLog(@"didStartPingWithAddress: %@",[self addressWithData:address]); 92 | 93 | [self sendPing]; 94 | 95 | if (!_sendTimer) { 96 | _sendTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(sendPing) userInfo:nil repeats:YES]; 97 | } 98 | } 99 | 100 | //发送成功,sequenceNumber范围:0~65535,超范围后从 0 开始 101 | - (void)simplsentePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber{ 102 | NSLog(@"#%u sent", sequenceNumber); 103 | 104 | if(sequenceNumber == 0){//重置 105 | [self.array removeAllObjects]; 106 | } 107 | 108 | //根据failuretimes判断是否有网 109 | if (self.array.count >= self.failureTimes) { 110 | self.reachable = NO; 111 | [self.array removeAllObjects]; 112 | } 113 | 114 | //将本次记录加入 115 | [self.array addObject:@(sequenceNumber)]; 116 | } 117 | 118 | //发送失败 119 | - (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error{ 120 | NSLog(@"#%u fail sent",sequenceNumber); 121 | 122 | //发送失败,直接认为断网 123 | self.reachable = NO; 124 | [self.array removeAllObjects]; 125 | } 126 | 127 | //接收成功 128 | - (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber{ 129 | //有网 130 | self.reachable = YES; 131 | [self.array removeAllObjects]; 132 | } 133 | 134 | //未知失败,重启ping 135 | - (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error{ 136 | self.reachable = NO; 137 | [self stopPing]; 138 | 139 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 140 | [self startNotifier]; 141 | }); 142 | 143 | } 144 | 145 | //根据data,计算host 146 | - (NSString *)addressWithData:(NSData *)address{ 147 | char *hostStr = malloc(NI_MAXHOST); 148 | memset(hostStr, 0, NI_MAXHOST); 149 | BOOL success = getnameinfo((const struct sockaddr *)address.bytes, (socklen_t)address.length, hostStr, (socklen_t)NI_MAXHOST, nil, 0, NI_NUMERICHOST) == 0; 150 | NSString *result; 151 | if (success) { 152 | result = [NSString stringWithUTF8String:hostStr]; 153 | }else{ 154 | result = @"?"; 155 | } 156 | free(hostStr); 157 | return result; 158 | } 159 | #pragma mark - setter 160 | - (void)setReachable:(BOOL)reachable{ 161 | if (_reachable != reachable) { 162 | _reachable = reachable; 163 | if (self.networkStatusDidChanged) { 164 | self.networkStatusDidChanged(); 165 | } 166 | } 167 | } 168 | 169 | - (void)setFailureTimes:(NSUInteger)failureTimes{ 170 | if (failureTimes < 2) { 171 | failureTimes = 2; 172 | } 173 | _failureTimes = failureTimes; 174 | } 175 | 176 | - (NSMutableArray *)array{ 177 | if (_array == nil) { 178 | _array = [NSMutableArray array]; 179 | } 180 | return _array; 181 | } 182 | @end 183 | -------------------------------------------------------------------------------- /SGNetObserverDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1812BC0F1D8F08BC005FC657 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC0E1D8F08BC005FC657 /* main.m */; }; 11 | 1812BC121D8F08BC005FC657 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC111D8F08BC005FC657 /* AppDelegate.m */; }; 12 | 1812BC151D8F08BC005FC657 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC141D8F08BC005FC657 /* ViewController.m */; }; 13 | 1812BC181D8F08BC005FC657 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1812BC161D8F08BC005FC657 /* Main.storyboard */; }; 14 | 1812BC1A1D8F08BC005FC657 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1812BC191D8F08BC005FC657 /* Assets.xcassets */; }; 15 | 1812BC1D1D8F08BC005FC657 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1812BC1B1D8F08BC005FC657 /* LaunchScreen.storyboard */; }; 16 | 1812BC2D1D901EB4005FC657 /* SimplePing.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC2C1D901EB4005FC657 /* SimplePing.m */; }; 17 | 1812BC301D903F6F005FC657 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC2F1D903F6F005FC657 /* Reachability.m */; }; 18 | 1812BC331D903FD0005FC657 /* SGNetObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC321D903FD0005FC657 /* SGNetObserver.m */; }; 19 | 1812BC381D906C17005FC657 /* SimplePinger.m in Sources */ = {isa = PBXBuildFile; fileRef = 1812BC371D906C17005FC657 /* SimplePinger.m */; }; 20 | 1812BC3A1D92DDEE005FC657 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1812BC391D92DDED005FC657 /* SystemConfiguration.framework */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXFileReference section */ 24 | 1812BC0A1D8F08BC005FC657 /* SGNetObserverDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SGNetObserverDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 25 | 1812BC0E1D8F08BC005FC657 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 26 | 1812BC101D8F08BC005FC657 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 27 | 1812BC111D8F08BC005FC657 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 28 | 1812BC131D8F08BC005FC657 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 29 | 1812BC141D8F08BC005FC657 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 30 | 1812BC171D8F08BC005FC657 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 31 | 1812BC191D8F08BC005FC657 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 32 | 1812BC1C1D8F08BC005FC657 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 33 | 1812BC1E1D8F08BC005FC657 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34 | 1812BC2B1D901EB4005FC657 /* SimplePing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimplePing.h; sourceTree = ""; }; 35 | 1812BC2C1D901EB4005FC657 /* SimplePing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimplePing.m; sourceTree = ""; }; 36 | 1812BC2E1D903F6F005FC657 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = ""; }; 37 | 1812BC2F1D903F6F005FC657 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = ""; }; 38 | 1812BC311D903FD0005FC657 /* SGNetObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGNetObserver.h; sourceTree = ""; }; 39 | 1812BC321D903FD0005FC657 /* SGNetObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGNetObserver.m; sourceTree = ""; }; 40 | 1812BC361D906C17005FC657 /* SimplePinger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimplePinger.h; sourceTree = ""; }; 41 | 1812BC371D906C17005FC657 /* SimplePinger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimplePinger.m; sourceTree = ""; }; 42 | 1812BC391D92DDED005FC657 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; 43 | /* End PBXFileReference section */ 44 | 45 | /* Begin PBXFrameworksBuildPhase section */ 46 | 1812BC071D8F08BC005FC657 /* Frameworks */ = { 47 | isa = PBXFrameworksBuildPhase; 48 | buildActionMask = 2147483647; 49 | files = ( 50 | 1812BC3A1D92DDEE005FC657 /* SystemConfiguration.framework in Frameworks */, 51 | ); 52 | runOnlyForDeploymentPostprocessing = 0; 53 | }; 54 | /* End PBXFrameworksBuildPhase section */ 55 | 56 | /* Begin PBXGroup section */ 57 | 1812BC011D8F08BC005FC657 = { 58 | isa = PBXGroup; 59 | children = ( 60 | 1812BC391D92DDED005FC657 /* SystemConfiguration.framework */, 61 | 1812BC0C1D8F08BC005FC657 /* SGNetObserverDemo */, 62 | 1812BC0B1D8F08BC005FC657 /* Products */, 63 | ); 64 | sourceTree = ""; 65 | }; 66 | 1812BC0B1D8F08BC005FC657 /* Products */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 1812BC0A1D8F08BC005FC657 /* SGNetObserverDemo.app */, 70 | ); 71 | name = Products; 72 | sourceTree = ""; 73 | }; 74 | 1812BC0C1D8F08BC005FC657 /* SGNetObserverDemo */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | 1812BC241D8F08FA005FC657 /* SGNetObserver */, 78 | 1812BC101D8F08BC005FC657 /* AppDelegate.h */, 79 | 1812BC111D8F08BC005FC657 /* AppDelegate.m */, 80 | 1812BC131D8F08BC005FC657 /* ViewController.h */, 81 | 1812BC141D8F08BC005FC657 /* ViewController.m */, 82 | 1812BC161D8F08BC005FC657 /* Main.storyboard */, 83 | 1812BC191D8F08BC005FC657 /* Assets.xcassets */, 84 | 1812BC1B1D8F08BC005FC657 /* LaunchScreen.storyboard */, 85 | 1812BC1E1D8F08BC005FC657 /* Info.plist */, 86 | 1812BC0D1D8F08BC005FC657 /* Supporting Files */, 87 | ); 88 | path = SGNetObserverDemo; 89 | sourceTree = ""; 90 | }; 91 | 1812BC0D1D8F08BC005FC657 /* Supporting Files */ = { 92 | isa = PBXGroup; 93 | children = ( 94 | 1812BC0E1D8F08BC005FC657 /* main.m */, 95 | ); 96 | name = "Supporting Files"; 97 | sourceTree = ""; 98 | }; 99 | 1812BC241D8F08FA005FC657 /* SGNetObserver */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | 1812BC2E1D903F6F005FC657 /* Reachability.h */, 103 | 1812BC2F1D903F6F005FC657 /* Reachability.m */, 104 | 1812BC2B1D901EB4005FC657 /* SimplePing.h */, 105 | 1812BC2C1D901EB4005FC657 /* SimplePing.m */, 106 | 1812BC361D906C17005FC657 /* SimplePinger.h */, 107 | 1812BC371D906C17005FC657 /* SimplePinger.m */, 108 | 1812BC311D903FD0005FC657 /* SGNetObserver.h */, 109 | 1812BC321D903FD0005FC657 /* SGNetObserver.m */, 110 | ); 111 | path = SGNetObserver; 112 | sourceTree = SOURCE_ROOT; 113 | }; 114 | /* End PBXGroup section */ 115 | 116 | /* Begin PBXNativeTarget section */ 117 | 1812BC091D8F08BC005FC657 /* SGNetObserverDemo */ = { 118 | isa = PBXNativeTarget; 119 | buildConfigurationList = 1812BC211D8F08BC005FC657 /* Build configuration list for PBXNativeTarget "SGNetObserverDemo" */; 120 | buildPhases = ( 121 | 1812BC061D8F08BC005FC657 /* Sources */, 122 | 1812BC071D8F08BC005FC657 /* Frameworks */, 123 | 1812BC081D8F08BC005FC657 /* Resources */, 124 | ); 125 | buildRules = ( 126 | ); 127 | dependencies = ( 128 | ); 129 | name = SGNetObserverDemo; 130 | productName = SGNetObserverDemo; 131 | productReference = 1812BC0A1D8F08BC005FC657 /* SGNetObserverDemo.app */; 132 | productType = "com.apple.product-type.application"; 133 | }; 134 | /* End PBXNativeTarget section */ 135 | 136 | /* Begin PBXProject section */ 137 | 1812BC021D8F08BC005FC657 /* Project object */ = { 138 | isa = PBXProject; 139 | attributes = { 140 | LastUpgradeCheck = 0730; 141 | ORGANIZATIONNAME = iOSSinger; 142 | TargetAttributes = { 143 | 1812BC091D8F08BC005FC657 = { 144 | CreatedOnToolsVersion = 7.3; 145 | }; 146 | }; 147 | }; 148 | buildConfigurationList = 1812BC051D8F08BC005FC657 /* Build configuration list for PBXProject "SGNetObserverDemo" */; 149 | compatibilityVersion = "Xcode 3.2"; 150 | developmentRegion = English; 151 | hasScannedForEncodings = 0; 152 | knownRegions = ( 153 | en, 154 | Base, 155 | ); 156 | mainGroup = 1812BC011D8F08BC005FC657; 157 | productRefGroup = 1812BC0B1D8F08BC005FC657 /* Products */; 158 | projectDirPath = ""; 159 | projectRoot = ""; 160 | targets = ( 161 | 1812BC091D8F08BC005FC657 /* SGNetObserverDemo */, 162 | ); 163 | }; 164 | /* End PBXProject section */ 165 | 166 | /* Begin PBXResourcesBuildPhase section */ 167 | 1812BC081D8F08BC005FC657 /* Resources */ = { 168 | isa = PBXResourcesBuildPhase; 169 | buildActionMask = 2147483647; 170 | files = ( 171 | 1812BC1D1D8F08BC005FC657 /* LaunchScreen.storyboard in Resources */, 172 | 1812BC1A1D8F08BC005FC657 /* Assets.xcassets in Resources */, 173 | 1812BC181D8F08BC005FC657 /* Main.storyboard in Resources */, 174 | ); 175 | runOnlyForDeploymentPostprocessing = 0; 176 | }; 177 | /* End PBXResourcesBuildPhase section */ 178 | 179 | /* Begin PBXSourcesBuildPhase section */ 180 | 1812BC061D8F08BC005FC657 /* Sources */ = { 181 | isa = PBXSourcesBuildPhase; 182 | buildActionMask = 2147483647; 183 | files = ( 184 | 1812BC151D8F08BC005FC657 /* ViewController.m in Sources */, 185 | 1812BC121D8F08BC005FC657 /* AppDelegate.m in Sources */, 186 | 1812BC0F1D8F08BC005FC657 /* main.m in Sources */, 187 | 1812BC331D903FD0005FC657 /* SGNetObserver.m in Sources */, 188 | 1812BC381D906C17005FC657 /* SimplePinger.m in Sources */, 189 | 1812BC2D1D901EB4005FC657 /* SimplePing.m in Sources */, 190 | 1812BC301D903F6F005FC657 /* Reachability.m in Sources */, 191 | ); 192 | runOnlyForDeploymentPostprocessing = 0; 193 | }; 194 | /* End PBXSourcesBuildPhase section */ 195 | 196 | /* Begin PBXVariantGroup section */ 197 | 1812BC161D8F08BC005FC657 /* Main.storyboard */ = { 198 | isa = PBXVariantGroup; 199 | children = ( 200 | 1812BC171D8F08BC005FC657 /* Base */, 201 | ); 202 | name = Main.storyboard; 203 | sourceTree = ""; 204 | }; 205 | 1812BC1B1D8F08BC005FC657 /* LaunchScreen.storyboard */ = { 206 | isa = PBXVariantGroup; 207 | children = ( 208 | 1812BC1C1D8F08BC005FC657 /* Base */, 209 | ); 210 | name = LaunchScreen.storyboard; 211 | sourceTree = ""; 212 | }; 213 | /* End PBXVariantGroup section */ 214 | 215 | /* Begin XCBuildConfiguration section */ 216 | 1812BC1F1D8F08BC005FC657 /* Debug */ = { 217 | isa = XCBuildConfiguration; 218 | buildSettings = { 219 | ALWAYS_SEARCH_USER_PATHS = NO; 220 | CLANG_ANALYZER_NONNULL = YES; 221 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 222 | CLANG_CXX_LIBRARY = "libc++"; 223 | CLANG_ENABLE_MODULES = YES; 224 | CLANG_ENABLE_OBJC_ARC = YES; 225 | CLANG_WARN_BOOL_CONVERSION = YES; 226 | CLANG_WARN_CONSTANT_CONVERSION = YES; 227 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 228 | CLANG_WARN_EMPTY_BODY = YES; 229 | CLANG_WARN_ENUM_CONVERSION = YES; 230 | CLANG_WARN_INT_CONVERSION = YES; 231 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 232 | CLANG_WARN_UNREACHABLE_CODE = YES; 233 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 234 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 235 | COPY_PHASE_STRIP = NO; 236 | DEBUG_INFORMATION_FORMAT = dwarf; 237 | ENABLE_STRICT_OBJC_MSGSEND = YES; 238 | ENABLE_TESTABILITY = YES; 239 | GCC_C_LANGUAGE_STANDARD = gnu99; 240 | GCC_DYNAMIC_NO_PIC = NO; 241 | GCC_NO_COMMON_BLOCKS = YES; 242 | GCC_OPTIMIZATION_LEVEL = 0; 243 | GCC_PREPROCESSOR_DEFINITIONS = ( 244 | "DEBUG=1", 245 | "$(inherited)", 246 | ); 247 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 248 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 249 | GCC_WARN_UNDECLARED_SELECTOR = YES; 250 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 251 | GCC_WARN_UNUSED_FUNCTION = YES; 252 | GCC_WARN_UNUSED_VARIABLE = YES; 253 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 254 | MTL_ENABLE_DEBUG_INFO = YES; 255 | ONLY_ACTIVE_ARCH = YES; 256 | SDKROOT = iphoneos; 257 | }; 258 | name = Debug; 259 | }; 260 | 1812BC201D8F08BC005FC657 /* Release */ = { 261 | isa = XCBuildConfiguration; 262 | buildSettings = { 263 | ALWAYS_SEARCH_USER_PATHS = NO; 264 | CLANG_ANALYZER_NONNULL = YES; 265 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 266 | CLANG_CXX_LIBRARY = "libc++"; 267 | CLANG_ENABLE_MODULES = YES; 268 | CLANG_ENABLE_OBJC_ARC = YES; 269 | CLANG_WARN_BOOL_CONVERSION = YES; 270 | CLANG_WARN_CONSTANT_CONVERSION = YES; 271 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 272 | CLANG_WARN_EMPTY_BODY = YES; 273 | CLANG_WARN_ENUM_CONVERSION = YES; 274 | CLANG_WARN_INT_CONVERSION = YES; 275 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 276 | CLANG_WARN_UNREACHABLE_CODE = YES; 277 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 278 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 279 | COPY_PHASE_STRIP = NO; 280 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 281 | ENABLE_NS_ASSERTIONS = NO; 282 | ENABLE_STRICT_OBJC_MSGSEND = YES; 283 | GCC_C_LANGUAGE_STANDARD = gnu99; 284 | GCC_NO_COMMON_BLOCKS = YES; 285 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 286 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 287 | GCC_WARN_UNDECLARED_SELECTOR = YES; 288 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 289 | GCC_WARN_UNUSED_FUNCTION = YES; 290 | GCC_WARN_UNUSED_VARIABLE = YES; 291 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 292 | MTL_ENABLE_DEBUG_INFO = NO; 293 | SDKROOT = iphoneos; 294 | VALIDATE_PRODUCT = YES; 295 | }; 296 | name = Release; 297 | }; 298 | 1812BC221D8F08BC005FC657 /* Debug */ = { 299 | isa = XCBuildConfiguration; 300 | buildSettings = { 301 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 302 | INFOPLIST_FILE = SGNetObserverDemo/Info.plist; 303 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 304 | PRODUCT_BUNDLE_IDENTIFIER = com.iossinger.SGNetObserverDemo; 305 | PRODUCT_NAME = "$(TARGET_NAME)"; 306 | }; 307 | name = Debug; 308 | }; 309 | 1812BC231D8F08BC005FC657 /* Release */ = { 310 | isa = XCBuildConfiguration; 311 | buildSettings = { 312 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 313 | INFOPLIST_FILE = SGNetObserverDemo/Info.plist; 314 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 315 | PRODUCT_BUNDLE_IDENTIFIER = com.iossinger.SGNetObserverDemo; 316 | PRODUCT_NAME = "$(TARGET_NAME)"; 317 | }; 318 | name = Release; 319 | }; 320 | /* End XCBuildConfiguration section */ 321 | 322 | /* Begin XCConfigurationList section */ 323 | 1812BC051D8F08BC005FC657 /* Build configuration list for PBXProject "SGNetObserverDemo" */ = { 324 | isa = XCConfigurationList; 325 | buildConfigurations = ( 326 | 1812BC1F1D8F08BC005FC657 /* Debug */, 327 | 1812BC201D8F08BC005FC657 /* Release */, 328 | ); 329 | defaultConfigurationIsVisible = 0; 330 | defaultConfigurationName = Release; 331 | }; 332 | 1812BC211D8F08BC005FC657 /* Build configuration list for PBXNativeTarget "SGNetObserverDemo" */ = { 333 | isa = XCConfigurationList; 334 | buildConfigurations = ( 335 | 1812BC221D8F08BC005FC657 /* Debug */, 336 | 1812BC231D8F08BC005FC657 /* Release */, 337 | ); 338 | defaultConfigurationIsVisible = 0; 339 | }; 340 | /* End XCConfigurationList section */ 341 | }; 342 | rootObject = 1812BC021D8F08BC005FC657 /* Project object */; 343 | } 344 | -------------------------------------------------------------------------------- /SGNetObserverDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SGNetObserverDemo.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/youngyunxing/SGNetObserver/b67335d9e15e4304592f3b409bf6e5b0617c63bc/SGNetObserverDemo.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /SGNetObserverDemo.xcodeproj/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /SGNetObserverDemo.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/SGNetObserverDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /SGNetObserverDemo.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SGNetObserverDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 1812BC091D8F08BC005FC657 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /SGNetObserverDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /SGNetObserverDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | 20 | return YES; 21 | } 22 | 23 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 24 | 25 | 26 | 27 | } 28 | 29 | 30 | - (void)applicationWillResignActive:(UIApplication *)application { 31 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 32 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 33 | } 34 | 35 | - (void)applicationDidEnterBackground:(UIApplication *)application { 36 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 37 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 38 | } 39 | 40 | - (void)applicationWillEnterForeground:(UIApplication *)application { 41 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 42 | } 43 | 44 | - (void)applicationDidBecomeActive:(UIApplication *)application { 45 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 46 | } 47 | 48 | - (void)applicationWillTerminate:(UIApplication *)application { 49 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /SGNetObserverDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /SGNetObserverDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /SGNetObserverDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SGNetObserverDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /SGNetObserverDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /SGNetObserverDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "SGNetObserver.h" 11 | 12 | @interface ViewController () 13 | @property (nonatomic,strong) SGNetObserver *observer; 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | self.observer = [SGNetObserver defultObsever]; 21 | [self.observer startNotifier]; 22 | 23 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChanged:) name:SGReachabilityChangedNotification object:nil]; 24 | } 25 | - (void)dealloc{ 26 | [[NSNotificationCenter defaultCenter] removeObserver:self name:SGReachabilityChangedNotification object:nil]; 27 | } 28 | - (void)networkStatusChanged:(NSNotification *)notify{ 29 | NSLog(@"notify-------%@",notify.userInfo); 30 | } 31 | 32 | - (void)didReceiveMemoryWarning { 33 | [super didReceiveMemoryWarning]; 34 | // Dispose of any resources that can be recreated. 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /SGNetObserverDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // SGNetObserverDemo 4 | // 5 | // Created by apple on 16/9/19. 6 | // Copyright © 2016年 iOSSinger. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------