├── HLNetWorkReachability.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── haley.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── harvey.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── haley.xcuserdatad │ └── xcschemes │ │ └── xcschememanagement.plist │ └── harvey.xcuserdatad │ └── xcschemes │ ├── HLNetWorkReachability.xcscheme │ └── xcschememanagement.plist ├── HLNetWorkReachability ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── HLNetWorkReachability.h ├── HLNetWorkReachability.m ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m └── README.md /HLNetWorkReachability.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B9775BF81D35E89100407421 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B9775BF71D35E89100407421 /* main.m */; }; 11 | B9775BFB1D35E89100407421 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B9775BFA1D35E89100407421 /* AppDelegate.m */; }; 12 | B9775BFE1D35E89100407421 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B9775BFD1D35E89100407421 /* ViewController.m */; }; 13 | B9775C011D35E89100407421 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B9775BFF1D35E89100407421 /* Main.storyboard */; }; 14 | B9775C031D35E89100407421 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B9775C021D35E89100407421 /* Assets.xcassets */; }; 15 | B9775C061D35E89100407421 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B9775C041D35E89100407421 /* LaunchScreen.storyboard */; }; 16 | B9775C0F1D35E8A700407421 /* HLNetWorkReachability.m in Sources */ = {isa = PBXBuildFile; fileRef = B9775C0E1D35E8A700407421 /* HLNetWorkReachability.m */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | B9775BF31D35E89100407421 /* HLNetWorkReachability.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HLNetWorkReachability.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | B9775BF71D35E89100407421 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | B9775BF91D35E89100407421 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | B9775BFA1D35E89100407421 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | B9775BFC1D35E89100407421 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | B9775BFD1D35E89100407421 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | B9775C001D35E89100407421 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | B9775C021D35E89100407421 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | B9775C051D35E89100407421 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | B9775C071D35E89100407421 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | B9775C0D1D35E8A600407421 /* HLNetWorkReachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HLNetWorkReachability.h; sourceTree = ""; }; 31 | B9775C0E1D35E8A700407421 /* HLNetWorkReachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HLNetWorkReachability.m; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | B9775BF01D35E89100407421 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | B9775BEA1D35E89100407421 = { 46 | isa = PBXGroup; 47 | children = ( 48 | B9775BF51D35E89100407421 /* HLNetWorkReachability */, 49 | B9775BF41D35E89100407421 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | B9775BF41D35E89100407421 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | B9775BF31D35E89100407421 /* HLNetWorkReachability.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | B9775BF51D35E89100407421 /* HLNetWorkReachability */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | B9775BF91D35E89100407421 /* AppDelegate.h */, 65 | B9775BFA1D35E89100407421 /* AppDelegate.m */, 66 | B9775C0D1D35E8A600407421 /* HLNetWorkReachability.h */, 67 | B9775C0E1D35E8A700407421 /* HLNetWorkReachability.m */, 68 | B9775BFC1D35E89100407421 /* ViewController.h */, 69 | B9775BFD1D35E89100407421 /* ViewController.m */, 70 | B9775BFF1D35E89100407421 /* Main.storyboard */, 71 | B9775C021D35E89100407421 /* Assets.xcassets */, 72 | B9775C041D35E89100407421 /* LaunchScreen.storyboard */, 73 | B9775C071D35E89100407421 /* Info.plist */, 74 | B9775BF61D35E89100407421 /* Supporting Files */, 75 | ); 76 | path = HLNetWorkReachability; 77 | sourceTree = ""; 78 | }; 79 | B9775BF61D35E89100407421 /* Supporting Files */ = { 80 | isa = PBXGroup; 81 | children = ( 82 | B9775BF71D35E89100407421 /* main.m */, 83 | ); 84 | name = "Supporting Files"; 85 | sourceTree = ""; 86 | }; 87 | /* End PBXGroup section */ 88 | 89 | /* Begin PBXNativeTarget section */ 90 | B9775BF21D35E89100407421 /* HLNetWorkReachability */ = { 91 | isa = PBXNativeTarget; 92 | buildConfigurationList = B9775C0A1D35E89100407421 /* Build configuration list for PBXNativeTarget "HLNetWorkReachability" */; 93 | buildPhases = ( 94 | B9775BEF1D35E89100407421 /* Sources */, 95 | B9775BF01D35E89100407421 /* Frameworks */, 96 | B9775BF11D35E89100407421 /* Resources */, 97 | ); 98 | buildRules = ( 99 | ); 100 | dependencies = ( 101 | ); 102 | name = HLNetWorkReachability; 103 | productName = HLNetWorkReachability; 104 | productReference = B9775BF31D35E89100407421 /* HLNetWorkReachability.app */; 105 | productType = "com.apple.product-type.application"; 106 | }; 107 | /* End PBXNativeTarget section */ 108 | 109 | /* Begin PBXProject section */ 110 | B9775BEB1D35E89100407421 /* Project object */ = { 111 | isa = PBXProject; 112 | attributes = { 113 | LastUpgradeCheck = 0730; 114 | ORGANIZATIONNAME = Haley; 115 | TargetAttributes = { 116 | B9775BF21D35E89100407421 = { 117 | CreatedOnToolsVersion = 7.3; 118 | }; 119 | }; 120 | }; 121 | buildConfigurationList = B9775BEE1D35E89100407421 /* Build configuration list for PBXProject "HLNetWorkReachability" */; 122 | compatibilityVersion = "Xcode 3.2"; 123 | developmentRegion = English; 124 | hasScannedForEncodings = 0; 125 | knownRegions = ( 126 | en, 127 | Base, 128 | ); 129 | mainGroup = B9775BEA1D35E89100407421; 130 | productRefGroup = B9775BF41D35E89100407421 /* Products */; 131 | projectDirPath = ""; 132 | projectRoot = ""; 133 | targets = ( 134 | B9775BF21D35E89100407421 /* HLNetWorkReachability */, 135 | ); 136 | }; 137 | /* End PBXProject section */ 138 | 139 | /* Begin PBXResourcesBuildPhase section */ 140 | B9775BF11D35E89100407421 /* Resources */ = { 141 | isa = PBXResourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | B9775C061D35E89100407421 /* LaunchScreen.storyboard in Resources */, 145 | B9775C031D35E89100407421 /* Assets.xcassets in Resources */, 146 | B9775C011D35E89100407421 /* Main.storyboard in Resources */, 147 | ); 148 | runOnlyForDeploymentPostprocessing = 0; 149 | }; 150 | /* End PBXResourcesBuildPhase section */ 151 | 152 | /* Begin PBXSourcesBuildPhase section */ 153 | B9775BEF1D35E89100407421 /* Sources */ = { 154 | isa = PBXSourcesBuildPhase; 155 | buildActionMask = 2147483647; 156 | files = ( 157 | B9775BFE1D35E89100407421 /* ViewController.m in Sources */, 158 | B9775C0F1D35E8A700407421 /* HLNetWorkReachability.m in Sources */, 159 | B9775BFB1D35E89100407421 /* AppDelegate.m in Sources */, 160 | B9775BF81D35E89100407421 /* main.m in Sources */, 161 | ); 162 | runOnlyForDeploymentPostprocessing = 0; 163 | }; 164 | /* End PBXSourcesBuildPhase section */ 165 | 166 | /* Begin PBXVariantGroup section */ 167 | B9775BFF1D35E89100407421 /* Main.storyboard */ = { 168 | isa = PBXVariantGroup; 169 | children = ( 170 | B9775C001D35E89100407421 /* Base */, 171 | ); 172 | name = Main.storyboard; 173 | sourceTree = ""; 174 | }; 175 | B9775C041D35E89100407421 /* LaunchScreen.storyboard */ = { 176 | isa = PBXVariantGroup; 177 | children = ( 178 | B9775C051D35E89100407421 /* Base */, 179 | ); 180 | name = LaunchScreen.storyboard; 181 | sourceTree = ""; 182 | }; 183 | /* End PBXVariantGroup section */ 184 | 185 | /* Begin XCBuildConfiguration section */ 186 | B9775C081D35E89100407421 /* Debug */ = { 187 | isa = XCBuildConfiguration; 188 | buildSettings = { 189 | ALWAYS_SEARCH_USER_PATHS = NO; 190 | CLANG_ANALYZER_NONNULL = YES; 191 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 192 | CLANG_CXX_LIBRARY = "libc++"; 193 | CLANG_ENABLE_MODULES = YES; 194 | CLANG_ENABLE_OBJC_ARC = YES; 195 | CLANG_WARN_BOOL_CONVERSION = YES; 196 | CLANG_WARN_CONSTANT_CONVERSION = YES; 197 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 198 | CLANG_WARN_EMPTY_BODY = YES; 199 | CLANG_WARN_ENUM_CONVERSION = YES; 200 | CLANG_WARN_INT_CONVERSION = YES; 201 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 202 | CLANG_WARN_UNREACHABLE_CODE = YES; 203 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 204 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 205 | COPY_PHASE_STRIP = NO; 206 | DEBUG_INFORMATION_FORMAT = dwarf; 207 | ENABLE_STRICT_OBJC_MSGSEND = YES; 208 | ENABLE_TESTABILITY = YES; 209 | GCC_C_LANGUAGE_STANDARD = gnu99; 210 | GCC_DYNAMIC_NO_PIC = NO; 211 | GCC_NO_COMMON_BLOCKS = YES; 212 | GCC_OPTIMIZATION_LEVEL = 0; 213 | GCC_PREPROCESSOR_DEFINITIONS = ( 214 | "DEBUG=1", 215 | "$(inherited)", 216 | ); 217 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 218 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 219 | GCC_WARN_UNDECLARED_SELECTOR = YES; 220 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 221 | GCC_WARN_UNUSED_FUNCTION = YES; 222 | GCC_WARN_UNUSED_VARIABLE = YES; 223 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 224 | MTL_ENABLE_DEBUG_INFO = YES; 225 | ONLY_ACTIVE_ARCH = YES; 226 | SDKROOT = iphoneos; 227 | }; 228 | name = Debug; 229 | }; 230 | B9775C091D35E89100407421 /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | ALWAYS_SEARCH_USER_PATHS = NO; 234 | CLANG_ANALYZER_NONNULL = YES; 235 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 236 | CLANG_CXX_LIBRARY = "libc++"; 237 | CLANG_ENABLE_MODULES = YES; 238 | CLANG_ENABLE_OBJC_ARC = YES; 239 | CLANG_WARN_BOOL_CONVERSION = YES; 240 | CLANG_WARN_CONSTANT_CONVERSION = YES; 241 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 242 | CLANG_WARN_EMPTY_BODY = YES; 243 | CLANG_WARN_ENUM_CONVERSION = YES; 244 | CLANG_WARN_INT_CONVERSION = YES; 245 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 246 | CLANG_WARN_UNREACHABLE_CODE = YES; 247 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 248 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 249 | COPY_PHASE_STRIP = NO; 250 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 251 | ENABLE_NS_ASSERTIONS = NO; 252 | ENABLE_STRICT_OBJC_MSGSEND = YES; 253 | GCC_C_LANGUAGE_STANDARD = gnu99; 254 | GCC_NO_COMMON_BLOCKS = YES; 255 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 256 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 257 | GCC_WARN_UNDECLARED_SELECTOR = YES; 258 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 259 | GCC_WARN_UNUSED_FUNCTION = YES; 260 | GCC_WARN_UNUSED_VARIABLE = YES; 261 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 262 | MTL_ENABLE_DEBUG_INFO = NO; 263 | SDKROOT = iphoneos; 264 | VALIDATE_PRODUCT = YES; 265 | }; 266 | name = Release; 267 | }; 268 | B9775C0B1D35E89100407421 /* Debug */ = { 269 | isa = XCBuildConfiguration; 270 | buildSettings = { 271 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 272 | INFOPLIST_FILE = HLNetWorkReachability/Info.plist; 273 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 274 | PRODUCT_BUNDLE_IDENTIFIER = com.suneee.HLNetWorkReachability; 275 | PRODUCT_NAME = "$(TARGET_NAME)"; 276 | }; 277 | name = Debug; 278 | }; 279 | B9775C0C1D35E89100407421 /* Release */ = { 280 | isa = XCBuildConfiguration; 281 | buildSettings = { 282 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 283 | INFOPLIST_FILE = HLNetWorkReachability/Info.plist; 284 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 285 | PRODUCT_BUNDLE_IDENTIFIER = com.suneee.HLNetWorkReachability; 286 | PRODUCT_NAME = "$(TARGET_NAME)"; 287 | }; 288 | name = Release; 289 | }; 290 | /* End XCBuildConfiguration section */ 291 | 292 | /* Begin XCConfigurationList section */ 293 | B9775BEE1D35E89100407421 /* Build configuration list for PBXProject "HLNetWorkReachability" */ = { 294 | isa = XCConfigurationList; 295 | buildConfigurations = ( 296 | B9775C081D35E89100407421 /* Debug */, 297 | B9775C091D35E89100407421 /* Release */, 298 | ); 299 | defaultConfigurationIsVisible = 0; 300 | defaultConfigurationName = Release; 301 | }; 302 | B9775C0A1D35E89100407421 /* Build configuration list for PBXNativeTarget "HLNetWorkReachability" */ = { 303 | isa = XCConfigurationList; 304 | buildConfigurations = ( 305 | B9775C0B1D35E89100407421 /* Debug */, 306 | B9775C0C1D35E89100407421 /* Release */, 307 | ); 308 | defaultConfigurationIsVisible = 0; 309 | }; 310 | /* End XCConfigurationList section */ 311 | }; 312 | rootObject = B9775BEB1D35E89100407421 /* Project object */; 313 | } 314 | -------------------------------------------------------------------------------- /HLNetWorkReachability.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /HLNetWorkReachability.xcodeproj/project.xcworkspace/xcuserdata/haley.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xhellohaley/HLNetWorkReachability/3065a35933ee0677d1917f40266fd6d911cbed20/HLNetWorkReachability.xcodeproj/project.xcworkspace/xcuserdata/haley.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /HLNetWorkReachability.xcodeproj/project.xcworkspace/xcuserdata/harvey.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0xhellohaley/HLNetWorkReachability/3065a35933ee0677d1917f40266fd6d911cbed20/HLNetWorkReachability.xcodeproj/project.xcworkspace/xcuserdata/harvey.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /HLNetWorkReachability.xcodeproj/xcuserdata/haley.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | HLNetWorkReachability.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /HLNetWorkReachability.xcodeproj/xcuserdata/harvey.xcuserdatad/xcschemes/HLNetWorkReachability.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 | -------------------------------------------------------------------------------- /HLNetWorkReachability.xcodeproj/xcuserdata/harvey.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | HLNetWorkReachability.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | B9775BF21D35E89100407421 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /HLNetWorkReachability/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // HLNetWorkReachability 4 | // 5 | // Created by Harvey on 16/7/13. 6 | // Copyright © 2016年 Haley. 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 | -------------------------------------------------------------------------------- /HLNetWorkReachability/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // HLNetWorkReachability 4 | // 5 | // Created by Harvey on 16/7/13. 6 | // Copyright © 2016年 Haley. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | #import "HLNetWorkReachability.h" 12 | 13 | @interface AppDelegate () 14 | 15 | @property (nonatomic) HLNetWorkReachability *hostReachability; 16 | 17 | @end 18 | 19 | @implementation AppDelegate 20 | 21 | 22 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 23 | // Override point for customization after application launch. 24 | 25 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kNetWorkReachabilityChangedNotification object:nil]; 26 | 27 | HLNetWorkReachability *reachability = [HLNetWorkReachability reachabilityWithHostName:@"www.baidu.com"]; 28 | self.hostReachability = reachability; 29 | [reachability startNotifier]; 30 | 31 | return YES; 32 | } 33 | 34 | - (void)reachabilityChanged:(NSNotification *)notification 35 | { 36 | HLNetWorkReachability *curReach = [notification object]; 37 | HLNetWorkStatus netStatus = [curReach currentReachabilityStatus]; 38 | switch (netStatus) { 39 | case HLNetWorkStatusNotReachable: 40 | NSLog(@"网络不可用"); 41 | break; 42 | case HLNetWorkStatusUnknown: 43 | NSLog(@"未知网络"); 44 | break; 45 | case HLNetWorkStatusWWAN2G: 46 | NSLog(@"2G网络"); 47 | break; 48 | case HLNetWorkStatusWWAN3G: 49 | NSLog(@"3G网络"); 50 | break; 51 | case HLNetWorkStatusWWAN4G: 52 | NSLog(@"4G网络"); 53 | break; 54 | case HLNetWorkStatusWiFi: 55 | NSLog(@"WiFi"); 56 | break; 57 | 58 | default: 59 | break; 60 | } 61 | } 62 | 63 | - (void)applicationWillResignActive:(UIApplication *)application { 64 | // 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. 65 | // 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. 66 | } 67 | 68 | - (void)applicationDidEnterBackground:(UIApplication *)application { 69 | // 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. 70 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 71 | } 72 | 73 | - (void)applicationWillEnterForeground:(UIApplication *)application { 74 | // 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. 75 | } 76 | 77 | - (void)applicationDidBecomeActive:(UIApplication *)application { 78 | // 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. 79 | } 80 | 81 | - (void)applicationWillTerminate:(UIApplication *)application { 82 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 83 | } 84 | 85 | @end 86 | -------------------------------------------------------------------------------- /HLNetWorkReachability/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 | } -------------------------------------------------------------------------------- /HLNetWorkReachability/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 | -------------------------------------------------------------------------------- /HLNetWorkReachability/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /HLNetWorkReachability/HLNetWorkReachability.h: -------------------------------------------------------------------------------- 1 | // 2 | // NetWorkReachability.h 3 | // SECC01 4 | // 5 | // Created by Harvey on 16/6/29. 6 | // Copyright © 2016年 Haley. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef NS_ENUM(NSUInteger, HLNetWorkStatus) { 12 | HLNetWorkStatusNotReachable = 0, 13 | HLNetWorkStatusUnknown = 1, 14 | HLNetWorkStatusWWAN2G = 2, 15 | HLNetWorkStatusWWAN3G = 3, 16 | HLNetWorkStatusWWAN4G = 4, 17 | HLNetWorkStatusWWAN5G = 5, 18 | 19 | HLNetWorkStatusWiFi = 9, 20 | }; 21 | 22 | extern NSString *kNetWorkReachabilityChangedNotification; 23 | 24 | @interface HLNetWorkReachability : NSObject 25 | 26 | /*! 27 | * Use to check the reachability of a given host name. 28 | */ 29 | + (instancetype)reachabilityWithHostName:(NSString *)hostName; 30 | 31 | /*! 32 | * Use to check the reachability of a given IP address. 33 | */ 34 | + (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress; 35 | 36 | /*! 37 | * Checks whether the default route is available. Should be used by applications that do not connect to a particular host. 38 | */ 39 | + (instancetype)reachabilityForInternetConnection; 40 | 41 | - (BOOL)startNotifier; 42 | 43 | - (void)stopNotifier; 44 | 45 | - (HLNetWorkStatus)currentReachabilityStatus; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /HLNetWorkReachability/HLNetWorkReachability.m: -------------------------------------------------------------------------------- 1 | // 2 | // NetWorkReachability.m 3 | // SECC01 4 | // 5 | // Created by Harvey on 16/6/29. 6 | // Copyright © 2016年 Haley. All rights reserved. 7 | // 8 | #import 9 | #import 10 | #import 11 | #import 12 | 13 | #import "HLNetWorkReachability.h" 14 | 15 | NSString *kNetWorkReachabilityChangedNotification = @"kReachabilityChangedNotification"; 16 | 17 | static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 18 | { 19 | HLNetWorkReachability *networkObject = (__bridge HLNetWorkReachability *)info; 20 | 21 | [[NSNotificationCenter defaultCenter] postNotificationName: kNetWorkReachabilityChangedNotification object:networkObject]; 22 | } 23 | 24 | @implementation HLNetWorkReachability 25 | { 26 | SCNetworkReachabilityRef _reachabilityRef; 27 | } 28 | 29 | + (instancetype)reachabilityWithHostName:(NSString *)hostName 30 | { 31 | HLNetWorkReachability *returnValue = NULL; 32 | SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); 33 | if (reachability != NULL) { 34 | returnValue = [[self alloc] init]; 35 | if (returnValue != NULL) { 36 | returnValue->_reachabilityRef = reachability; 37 | } else { 38 | CFRelease(reachability); 39 | } 40 | } 41 | return returnValue; 42 | } 43 | 44 | + (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress 45 | { 46 | SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress); 47 | 48 | HLNetWorkReachability* returnValue = NULL; 49 | 50 | if (reachability != NULL) 51 | { 52 | returnValue = [[self alloc] init]; 53 | if (returnValue != NULL) 54 | { 55 | returnValue->_reachabilityRef = reachability; 56 | } 57 | else { 58 | CFRelease(reachability); 59 | } 60 | } 61 | return returnValue; 62 | } 63 | 64 | + (instancetype)reachabilityForInternetConnection 65 | { 66 | struct sockaddr_in zeroAddress; 67 | bzero(&zeroAddress, sizeof(zeroAddress)); 68 | zeroAddress.sin_len = sizeof(zeroAddress); 69 | zeroAddress.sin_family = AF_INET; 70 | 71 | return [self reachabilityWithAddress: (const struct sockaddr *) &zeroAddress]; 72 | } 73 | 74 | - (BOOL)startNotifier 75 | { 76 | BOOL returnValue = NO; 77 | SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 78 | 79 | if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context)) { 80 | if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) { 81 | returnValue = YES; 82 | } 83 | } 84 | return returnValue; 85 | } 86 | 87 | - (HLNetWorkStatus)currentReachabilityStatus 88 | { 89 | HLNetWorkStatus returnValue = HLNetWorkStatusNotReachable; 90 | SCNetworkReachabilityFlags flags; 91 | if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 92 | { 93 | returnValue = [self networkStatusForFlags:flags]; 94 | } 95 | 96 | return returnValue; 97 | } 98 | 99 | - (HLNetWorkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags 100 | { 101 | if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 102 | { 103 | // The target host is not reachable. 104 | return HLNetWorkStatusNotReachable; 105 | } 106 | 107 | HLNetWorkStatus returnValue = HLNetWorkStatusNotReachable; 108 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 109 | { 110 | /* 111 | If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi... 112 | */ 113 | returnValue = HLNetWorkStatusWiFi; 114 | } 115 | 116 | if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 117 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 118 | { 119 | /* 120 | ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs... 121 | */ 122 | 123 | if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 124 | { 125 | /* 126 | ... and no [user] intervention is needed... 127 | */ 128 | returnValue = HLNetWorkStatusWiFi; 129 | } 130 | } 131 | 132 | if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 133 | { 134 | /* 135 | ... but WWAN connections are OK if the calling application is using the CFNetwork APIs. 136 | */ 137 | NSArray *typeStrings2G = @[CTRadioAccessTechnologyEdge, 138 | CTRadioAccessTechnologyGPRS, 139 | CTRadioAccessTechnologyCDMA1x]; 140 | 141 | NSArray *typeStrings3G = @[CTRadioAccessTechnologyHSDPA, 142 | CTRadioAccessTechnologyWCDMA, 143 | CTRadioAccessTechnologyHSUPA, 144 | CTRadioAccessTechnologyCDMAEVDORev0, 145 | CTRadioAccessTechnologyCDMAEVDORevA, 146 | CTRadioAccessTechnologyCDMAEVDORevB, 147 | CTRadioAccessTechnologyeHRPD]; 148 | 149 | NSArray *typeStrings4G = @[CTRadioAccessTechnologyLTE]; 150 | 151 | NSArray *typeStrings5G = @[]; 152 | if(@available(iOS 14.0, *)){ 153 | typeStrings5G = @[CTRadioAccessTechnologyNRNSA,CTRadioAccessTechnologyNR]; 154 | } 155 | 156 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { 157 | CTTelephonyNetworkInfo *teleInfo= [[CTTelephonyNetworkInfo alloc] init]; 158 | NSString *accessString = teleInfo.currentRadioAccessTechnology; 159 | if ([typeStrings5G containsObject:accessString]) { 160 | return HLNetWorkStatusWWAN5G; 161 | } else if ([typeStrings4G containsObject:accessString]) { 162 | return HLNetWorkStatusWWAN4G; 163 | } else if ([typeStrings3G containsObject:accessString]) { 164 | return HLNetWorkStatusWWAN3G; 165 | } else if ([typeStrings2G containsObject:accessString]) { 166 | return HLNetWorkStatusWWAN2G; 167 | } else { 168 | return HLNetWorkStatusUnknown; 169 | } 170 | } else { 171 | return HLNetWorkStatusUnknown; 172 | } 173 | } 174 | 175 | return returnValue; 176 | } 177 | 178 | - (void)stopNotifier 179 | { 180 | if (_reachabilityRef != NULL) 181 | { 182 | SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 183 | } 184 | } 185 | 186 | - (void)dealloc 187 | { 188 | [self stopNotifier]; 189 | if (_reachabilityRef != NULL) 190 | { 191 | CFRelease(_reachabilityRef); 192 | } 193 | } 194 | 195 | 196 | @end 197 | -------------------------------------------------------------------------------- /HLNetWorkReachability/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 | -------------------------------------------------------------------------------- /HLNetWorkReachability/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // HLNetWorkReachability 4 | // 5 | // Created by Harvey on 16/7/13. 6 | // Copyright © 2016年 Haley. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /HLNetWorkReachability/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // HLNetWorkReachability 4 | // 5 | // Created by Harvey on 16/7/13. 6 | // Copyright © 2016年 Haley. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "HLNetWorkReachability.h" 11 | 12 | @interface ViewController () 13 | 14 | @property (weak, nonatomic) IBOutlet UILabel *statusLabel; 15 | 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view, typically from a nib. 23 | 24 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kNetWorkReachabilityChangedNotification object:nil]; 25 | } 26 | 27 | - (void)reachabilityChanged:(NSNotification *)notification 28 | { 29 | HLNetWorkReachability *curReach = [notification object]; 30 | HLNetWorkStatus netStatus = [curReach currentReachabilityStatus]; 31 | switch (netStatus) { 32 | case HLNetWorkStatusNotReachable: 33 | self.statusLabel.text = @"网络不可用"; 34 | break; 35 | case HLNetWorkStatusUnknown: 36 | self.statusLabel.text = @"未知网络"; 37 | break; 38 | case HLNetWorkStatusWWAN2G: 39 | self.statusLabel.text = @"2G网络"; 40 | break; 41 | case HLNetWorkStatusWWAN3G: 42 | self.statusLabel.text = @"3G网络"; 43 | break; 44 | case HLNetWorkStatusWWAN4G: 45 | self.statusLabel.text = @"4G网络"; 46 | break; 47 | case HLNetWorkStatusWWAN5G: 48 | self.statusLabel.text = @"5G网络"; 49 | break; 50 | case HLNetWorkStatusWiFi: 51 | self.statusLabel.text = @"WiFi"; 52 | break; 53 | 54 | default: 55 | break; 56 | } 57 | } 58 | 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /HLNetWorkReachability/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // HLNetWorkReachability 4 | // 5 | // Created by Harvey on 16/7/13. 6 | // Copyright © 2016年 Haley. 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HLNetWorkReachability 2 | Reachability 已经升级了,新版的Reachability已经支持IPv6了。
3 | 我是在新版的Reachability上做了改进,主要是判断网络环境2G/3G/4G/WiFi等。 4 | 5 | # 使用到的系统库 6 | ``` 7 | #import 8 | #import 9 | #import 10 | ``` 11 | 12 | # 改进 13 | 1、扩展了原来网络环境的枚举类型 14 | ``` 15 | typedef NS_ENUM(NSUInteger, HLNetWorkStatus) { 16 | HLNetWorkStatusNotReachable = 0, 17 | HLNetWorkStatusUnknown = 1, 18 | HLNetWorkStatusWWAN2G = 2, 19 | HLNetWorkStatusWWAN3G = 3, 20 | HLNetWorkStatusWWAN4G = 4, 21 | 22 | HLNetWorkStatusWiFi = 9, 23 | }; 24 | ``` 25 | 2、修改了原来的网络粗略判断 26 | ``` 27 | - (HLNetWorkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags 28 | { 29 | if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 30 | { 31 | // The target host is not reachable. 32 | return HLNetWorkStatusNotReachable; 33 | } 34 | 35 | HLNetWorkStatus returnValue = HLNetWorkStatusNotReachable; 36 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 37 | { 38 | /* 39 | If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi... 40 | */ 41 | returnValue = HLNetWorkStatusWiFi; 42 | } 43 | 44 | if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 45 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 46 | { 47 | /* 48 | ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs... 49 | */ 50 | 51 | if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 52 | { 53 | /* 54 | ... and no [user] intervention is needed... 55 | */ 56 | returnValue = HLNetWorkStatusWiFi; 57 | } 58 | } 59 | 60 | if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 61 | { 62 | /* 63 | ... but WWAN connections are OK if the calling application is using the CFNetwork APIs. 64 | */ 65 | NSArray *typeStrings2G = @[CTRadioAccessTechnologyEdge, 66 | CTRadioAccessTechnologyGPRS, 67 | CTRadioAccessTechnologyCDMA1x]; 68 | 69 | NSArray *typeStrings3G = @[CTRadioAccessTechnologyHSDPA, 70 | CTRadioAccessTechnologyWCDMA, 71 | CTRadioAccessTechnologyHSUPA, 72 | CTRadioAccessTechnologyCDMAEVDORev0, 73 | CTRadioAccessTechnologyCDMAEVDORevA, 74 | CTRadioAccessTechnologyCDMAEVDORevB, 75 | CTRadioAccessTechnologyeHRPD]; 76 | 77 | NSArray *typeStrings4G = @[CTRadioAccessTechnologyLTE]; 78 | 79 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { 80 | CTTelephonyNetworkInfo *teleInfo= [[CTTelephonyNetworkInfo alloc] init]; 81 | NSString *accessString = teleInfo.currentRadioAccessTechnology; 82 | if ([typeStrings4G containsObject:accessString]) { 83 | return HLNetWorkStatusWWAN4G; 84 | } else if ([typeStrings3G containsObject:accessString]) { 85 | return HLNetWorkStatusWWAN3G; 86 | } else if ([typeStrings2G containsObject:accessString]) { 87 | return HLNetWorkStatusWWAN2G; 88 | } else { 89 | return HLNetWorkStatusUnknown; 90 | } 91 | } else { 92 | return HLNetWorkStatusUnknown; 93 | } 94 | } 95 | 96 | return returnValue; 97 | } 98 | ``` 99 | 100 | # 用法 101 | 还是原来那个用法 102 | ``` 103 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kNetWorkReachabilityChangedNotification object:nil]; 104 | 105 | HLNetWorkReachability *reachability = [HLNetWorkReachability reachabilityWithHostName:@"www.baidu.com"]; 106 | self.hostReachability = reachability; 107 | [reachability startNotifier]; 108 | 109 | ``` 110 | 111 | 有什么问题或者建议可以联系我。 112 | 113 | --------------------------------------------------------------------------------