├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── RealReachability.podspec ├── RealReachability.xcodeproj ├── project.pbxproj └── xcshareddata │ └── xcschemes │ └── RealReachability.xcscheme ├── RealReachability ├── Connection │ ├── LocalConnection.h │ └── LocalConnection.m ├── FSM │ ├── FSMDefines.h │ ├── FSMEngine.h │ ├── FSMEngine.m │ ├── FSMStateUtil.h │ ├── FSMStateUtil.m │ ├── ReachState.h │ ├── ReachState.m │ ├── ReachStateLoading.h │ ├── ReachStateLoading.m │ ├── ReachStateUnReachable.h │ ├── ReachStateUnReachable.m │ ├── ReachStateUnloaded.h │ ├── ReachStateUnloaded.m │ ├── ReachStateWIFI.h │ ├── ReachStateWIFI.m │ ├── ReachStateWWAN.h │ └── ReachStateWWAN.m ├── Info.plist ├── Ping │ ├── PingFoundation.h │ ├── PingFoundation.m │ ├── PingHelper.h │ ├── PingHelper.m │ └── README.md ├── RealReachability.h └── RealReachability.m ├── testRealReachability.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ └── testRealReachability.xcscheme └── testRealReachability ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── KVO ├── NSObject+SimpleKVO.h └── NSObject+SimpleKVO.m ├── ViewController.h ├── ViewController.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | *.pbxuser 3 | !default.pbxuser 4 | *.mode1v3 5 | !default.mode1v3 6 | *.mode2v3 7 | !default.mode2v3 8 | *.perspectivev3 9 | !default.perspectivev3 10 | xcuserdata 11 | *.xccheckout 12 | *.moved-aside 13 | DerivedData 14 | *.hmap 15 | *.ipa 16 | *.xcuserstate 17 | project.xcworkspace 18 | .DS_Store 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: objective-c 2 | xcode_project: testRealReachability.xcodeproj 3 | xcode_scheme: testRealReachability 4 | osx_image: xcode7 5 | 6 | script: xcodebuild -project testRealReachability.xcodeproj -scheme testRealReachability -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Dustturtle openglnewbee@163.com 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RealReachability 2 | [![Version](https://img.shields.io/badge/pod-1.4.1-yellow.svg)](http://cocoadocs.org/docsets/RealReachability/1.4.1/) 3 | [![License](https://img.shields.io/badge/License-MIT-blue.svg)](http://cocoadocs.org/docsets/RealReachability/1.4.1) 4 | [![Platform](https://img.shields.io/badge/Platform-iOS-orange.svg)](http://cocoadocs.org/docsets/RealReachability/1.4.1/) 5 | [![Platform](https://img.shields.io/badge/Build-Passed-green.svg)](http://cocoadocs.org/docsets/RealReachability/1.4.1/) 6 | #### We need to observe the REAL reachability of network for iOS. That's what RealReachability do. 7 | 8 | # Why RealReachability? 9 | As we know, we already have reachability framework for us to choose. Such as the famous repository [Reachability](https://github.com/tonymillion/Reachability). 10 | 11 | **BUT we really need a tool for us to get the reachability, not the local connection!** 12 | 13 | **Apple doc tells us something about SCNetworkReachability API: 14 | "Note that reachability does not guarantee that the data packet will actually be received by the host."** 15 | 16 | The called "reachability" we already know can only tell us the local connection status.These tools currently we know are all supported by the SCNetworkReachability API. 17 | 18 | 19 | **Now [RealReachability](https://github.com/dustturtle/RealReachability) can do this for you~** 20 | 21 | We introduce ping module for us to check the real network status, together with SCNetworkReachability API. And we use FSM(finite state machine) to control all of the network status to confirm that only status change will be sent to application. 22 | 23 | Enjoy it! 24 | 25 | # Quick Start With Cocoapods 26 | [CocoaPods](http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like RealReachability in your projects. You can install it with the following command: 27 | 28 | ```bash 29 | $ gem install cocoapods 30 | ``` 31 | 32 | #### Podfile 33 | 34 | To integrate RealReachability into your Xcode project using CocoaPods, specify it in your `Podfile`: 35 | 36 | ```ruby 37 | source 'https://github.com/CocoaPods/Specs.git' 38 | platform :ios, '8.0' 39 | 40 | pod 'RealReachability' 41 | ``` 42 | 43 | Then, run the following command: 44 | 45 | ```bash 46 | $ pod install 47 | ``` 48 | # Installation with Carthage 49 | [Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. 50 | 51 | You can install Carthage with [Homebrew](http://brew.sh/) using the following command: 52 | 53 | ```bash 54 | $ brew update 55 | $ brew install carthage 56 | ``` 57 | 58 | RealReachability in your `Cartfile`: 59 | 60 | ``` 61 | github "dustturtle/RealReachability" 62 | ``` 63 | 64 | # Manual Start 65 | If you'd rather do everything by hand, just add the folder "RealReachability" to your project, then all of the files will be added to your project. 66 | 67 | 68 | # Dependencies 69 | 70 | - Xcode 5.0+ for ARC support, automatic synthesis and compatibility 71 | libraries. iOS 6.0+. 72 | - The SystemConfiguration Framework should be added to your project. 73 | 74 | # Usage 75 | #### Start to notify(we suggest you to start notify in didFinishLaunchingWithOptions): 76 | 77 | ```objective-c 78 | 79 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 80 | [GLobalRealReachability startNotifier]; 81 | return YES; 82 | } 83 | ``` 84 | #### Add Observer(anywhere you like): 85 | ```objective-c 86 | [[NSNotificationCenter defaultCenter] addObserver:self 87 | selector:@selector(networkChanged:) 88 | name:kRealReachabilityChangedNotification 89 | object:nil]; 90 | 91 | ``` 92 | 93 | #### Observer method like below: 94 | ```objective-c 95 | - (void)networkChanged:(NSNotification *)notification 96 | { 97 | RealReachability *reachability = (RealReachability *)notification.object; 98 | ReachabilityStatus status = [reachability currentReachabilityStatus]; 99 | NSLog(@"currentStatus:%@",@(status)); 100 | } 101 | 102 | ``` 103 | #### Trigger realtime Reachability like below: 104 | ```objective-c 105 | [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) { 106 | switch (status) 107 | { 108 | case RealStatusNotReachable: 109 | { 110 | // case NotReachable handler 111 | break; 112 | } 113 | 114 | case RealStatusViaWiFi: 115 | { 116 | // case WiFi handler 117 | break; 118 | } 119 | 120 | case RealStatusViaWWAN: 121 | { 122 | // case WWAN handler 123 | break; 124 | } 125 | 126 | default: 127 | break; 128 | } 129 | }]; 130 | ``` 131 | #### Query currentStatus 132 | ``` 133 | ReachabilityStatus status = [reachability currentReachabilityStatus]; 134 | ``` 135 | 136 | Once the reachabilityWithBlock was called, the "currentReachabilityStatus" will be refreshed synchronously. 137 | #### Set your own host for Ping (optional) 138 | ##### Note that now we introduced the new feature "doublecheck" to make the status more reliable in 1.2.0! 139 | Please make sure the host you set here is available for pinging. Large, stable website suggested. 140 | This step is optional. If you do not set this, our default host is: www.apple.com. 141 | You may set your own host any time you like. Codes just like below: 142 | 143 | ``` 144 | GLobalRealReachability.hostForPing = @"www.apple.com"; 145 | GLobalRealReachability.hostForCheck = @"www.youOwnHostExample.com"; 146 | ``` 147 | We suggest you use two hosts: one your own(if you have one available for pinging), one public; Just like the example below. 148 | 149 | For more details about the "doublecheck" feature, you can go deep into the codes. 150 | 151 | #### Get current WWAN type (optional) 152 | ``` 153 | WWANAccessType accessType = [GLobalRealReachability currentWWANtype]; 154 | 155 | ``` 156 | Current WWAN type might be used to improve your app's user experience(e.g, set different network request timeout interval for different WWAN type). 157 | #### Check the VPN status of your network 158 | ``` 159 | - (BOOL)isVPNOn; 160 | ``` 161 | With the help of this method, we have improved our reachability check logic when using VPN. 162 | #### More: 163 | We can also use PingHelper or LocalConnection alone to make a ping action or just observe the local connection. 164 | Pod usage like blow (we have two pod subspecs): 165 | 166 | ```ruby 167 | pod 'RealReachability/Ping' 168 | ``` 169 | ```ruby 170 | pod 'RealReachability/Connection' 171 | ``` 172 | This is the only API we need to invoke about Ping: 173 | 174 | ```objective-c 175 | - (void)pingWithBlock:(void (^)(BOOL isSuccess))completion; 176 | 177 | ``` 178 | **More about the ping usage**, please see the **PingHelper.h** or codes in [**the demo project**](https://github.com/dustturtle/RealReachability). 179 | 180 | **LocalConnection module is very similar with Reachability**. 181 | **More about its usage**, please see the **LocalConnection.h** or codes in [**the demo project**](https://github.com/dustturtle/RealReachability). 182 | 183 | 184 | # Demo 185 | We already put the demo project in the [repository](https://github.com/dustturtle/RealReachability). 186 | 187 | # License 188 | 189 | RealReachability is released under the MIT license. See LICENSE for details. 190 | 191 | ## And finally... 192 | 193 | Please use and improve! Patches accepted, or create an issue. 194 | 195 | I'd love it if you could send me a note as to which app you're using it with! Thank you! 196 | 197 | ## 支持我 198 | 199 | 老司机技术周报出品的《WWDC 内参》系列,一直是 iOS 开发的精品阅读,几乎涵盖了每年需要了解的所有 iOS 新技术。尤其是今年《WWDC21 内参》的质量比去年有了比较大的提升, 作为作者之一,在这里安利给大家: https://xiaozhuanlan.com/wwdc21?rel=4203097925。目前活动价五折销售,抓紧入手啦。 200 | 201 | 202 | ## [中文版使用指南](http://blog.csdn.net/openglnewbee/article/details/50705146) 203 | 204 | -------------------------------------------------------------------------------- /RealReachability.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod spec lint RealReachability.podspec' to ensure this is a 3 | # valid spec and to remove all comments including this before submitting the spec. 4 | # 5 | # To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html 6 | # To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ 7 | # 8 | Pod::Spec.new do |s| 9 | s.name = "RealReachability" 10 | s.version = "1.4.1" 11 | s.summary = "We need to observe the REAL reachability of network for iOS. That's what RealReachability do." 12 | 13 | 14 | # Add desc next time. 15 | s.homepage = "https://github.com/dustturtle/RealReachability" 16 | # Add screenshots next time. 17 | s.license = "MIT" 18 | s.author = { "GuanZhenwei" => "openglnewbee@163.com" } 19 | s.platform = :ios 20 | s.ios.deployment_target = '7.0' 21 | s.source = { :git => "https://github.com/dustturtle/RealReachability.git", :tag => s.version, :submodules => true } 22 | s.source_files = "RealReachability", "RealReachability/FSM" 23 | s.requires_arc = true 24 | 25 | s.public_header_files = 'RealReachability/RealReachability.h' 26 | 27 | s.subspec 'Connection' do |ss| 28 | ss.source_files = "RealReachability/Connection" 29 | ss.public_header_files = 'RealReachability/Connection/LocalConnection.h' 30 | end 31 | 32 | s.subspec 'Ping' do |ss| 33 | ss.source_files = "RealReachability/Ping" 34 | ss.public_header_files = 'RealReachability/Ping/PingHelper.h' 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /RealReachability.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 7F3A815F1D5220D6004B78CE /* RealReachability.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A815E1D5220D6004B78CE /* RealReachability.h */; settings = {ATTRIBUTES = (Public, ); }; }; 11 | 7F3A81821D522132004B78CE /* LocalConnection.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A81671D522132004B78CE /* LocalConnection.h */; settings = {ATTRIBUTES = (Public, ); }; }; 12 | 7F3A81831D522132004B78CE /* LocalConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81681D522132004B78CE /* LocalConnection.m */; }; 13 | 7F3A81841D522132004B78CE /* FSMDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A816A1D522132004B78CE /* FSMDefines.h */; }; 14 | 7F3A81851D522132004B78CE /* FSMEngine.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A816B1D522132004B78CE /* FSMEngine.h */; }; 15 | 7F3A81861D522132004B78CE /* FSMEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A816C1D522132004B78CE /* FSMEngine.m */; }; 16 | 7F3A81871D522132004B78CE /* FSMStateUtil.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A816D1D522132004B78CE /* FSMStateUtil.h */; }; 17 | 7F3A81881D522132004B78CE /* FSMStateUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A816E1D522132004B78CE /* FSMStateUtil.m */; }; 18 | 7F3A81891D522132004B78CE /* ReachState.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A816F1D522132004B78CE /* ReachState.h */; }; 19 | 7F3A818A1D522132004B78CE /* ReachState.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81701D522132004B78CE /* ReachState.m */; }; 20 | 7F3A818B1D522132004B78CE /* ReachStateLoading.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A81711D522132004B78CE /* ReachStateLoading.h */; }; 21 | 7F3A818C1D522132004B78CE /* ReachStateLoading.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81721D522132004B78CE /* ReachStateLoading.m */; }; 22 | 7F3A818D1D522132004B78CE /* ReachStateUnloaded.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A81731D522132004B78CE /* ReachStateUnloaded.h */; }; 23 | 7F3A818E1D522132004B78CE /* ReachStateUnloaded.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81741D522132004B78CE /* ReachStateUnloaded.m */; }; 24 | 7F3A818F1D522132004B78CE /* ReachStateUnReachable.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A81751D522132004B78CE /* ReachStateUnReachable.h */; }; 25 | 7F3A81901D522132004B78CE /* ReachStateUnReachable.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81761D522132004B78CE /* ReachStateUnReachable.m */; }; 26 | 7F3A81911D522132004B78CE /* ReachStateWIFI.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A81771D522132004B78CE /* ReachStateWIFI.h */; }; 27 | 7F3A81921D522132004B78CE /* ReachStateWIFI.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81781D522132004B78CE /* ReachStateWIFI.m */; }; 28 | 7F3A81931D522132004B78CE /* ReachStateWWAN.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A81791D522132004B78CE /* ReachStateWWAN.h */; }; 29 | 7F3A81941D522132004B78CE /* ReachStateWWAN.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A817A1D522132004B78CE /* ReachStateWWAN.m */; }; 30 | 7F3A81951D522132004B78CE /* PingFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A817C1D522132004B78CE /* PingFoundation.h */; }; 31 | 7F3A81961D522132004B78CE /* PingFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A817D1D522132004B78CE /* PingFoundation.m */; }; 32 | 7F3A81971D522132004B78CE /* PingHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 7F3A817E1D522132004B78CE /* PingHelper.h */; }; 33 | 7F3A81981D522132004B78CE /* PingHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A817F1D522132004B78CE /* PingHelper.m */; }; 34 | 7F3A819A1D522132004B78CE /* RealReachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F3A81811D522132004B78CE /* RealReachability.m */; }; 35 | /* End PBXBuildFile section */ 36 | 37 | /* Begin PBXFileReference section */ 38 | 7F3A815B1D5220D6004B78CE /* RealReachability.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RealReachability.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 39 | 7F3A815E1D5220D6004B78CE /* RealReachability.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RealReachability.h; sourceTree = ""; }; 40 | 7F3A81601D5220D6004B78CE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 41 | 7F3A81671D522132004B78CE /* LocalConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocalConnection.h; sourceTree = ""; }; 42 | 7F3A81681D522132004B78CE /* LocalConnection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocalConnection.m; sourceTree = ""; }; 43 | 7F3A816A1D522132004B78CE /* FSMDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSMDefines.h; sourceTree = ""; }; 44 | 7F3A816B1D522132004B78CE /* FSMEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSMEngine.h; sourceTree = ""; }; 45 | 7F3A816C1D522132004B78CE /* FSMEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSMEngine.m; sourceTree = ""; }; 46 | 7F3A816D1D522132004B78CE /* FSMStateUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSMStateUtil.h; sourceTree = ""; }; 47 | 7F3A816E1D522132004B78CE /* FSMStateUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSMStateUtil.m; sourceTree = ""; }; 48 | 7F3A816F1D522132004B78CE /* ReachState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachState.h; sourceTree = ""; }; 49 | 7F3A81701D522132004B78CE /* ReachState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachState.m; sourceTree = ""; }; 50 | 7F3A81711D522132004B78CE /* ReachStateLoading.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateLoading.h; sourceTree = ""; }; 51 | 7F3A81721D522132004B78CE /* ReachStateLoading.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateLoading.m; sourceTree = ""; }; 52 | 7F3A81731D522132004B78CE /* ReachStateUnloaded.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateUnloaded.h; sourceTree = ""; }; 53 | 7F3A81741D522132004B78CE /* ReachStateUnloaded.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateUnloaded.m; sourceTree = ""; }; 54 | 7F3A81751D522132004B78CE /* ReachStateUnReachable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateUnReachable.h; sourceTree = ""; }; 55 | 7F3A81761D522132004B78CE /* ReachStateUnReachable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateUnReachable.m; sourceTree = ""; }; 56 | 7F3A81771D522132004B78CE /* ReachStateWIFI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateWIFI.h; sourceTree = ""; }; 57 | 7F3A81781D522132004B78CE /* ReachStateWIFI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateWIFI.m; sourceTree = ""; }; 58 | 7F3A81791D522132004B78CE /* ReachStateWWAN.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateWWAN.h; sourceTree = ""; }; 59 | 7F3A817A1D522132004B78CE /* ReachStateWWAN.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateWWAN.m; sourceTree = ""; }; 60 | 7F3A817C1D522132004B78CE /* PingFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PingFoundation.h; sourceTree = ""; }; 61 | 7F3A817D1D522132004B78CE /* PingFoundation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PingFoundation.m; sourceTree = ""; }; 62 | 7F3A817E1D522132004B78CE /* PingHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PingHelper.h; sourceTree = ""; }; 63 | 7F3A817F1D522132004B78CE /* PingHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PingHelper.m; sourceTree = ""; }; 64 | 7F3A81811D522132004B78CE /* RealReachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RealReachability.m; sourceTree = ""; }; 65 | /* End PBXFileReference section */ 66 | 67 | /* Begin PBXFrameworksBuildPhase section */ 68 | 7F3A81571D5220D6004B78CE /* Frameworks */ = { 69 | isa = PBXFrameworksBuildPhase; 70 | buildActionMask = 2147483647; 71 | files = ( 72 | ); 73 | runOnlyForDeploymentPostprocessing = 0; 74 | }; 75 | /* End PBXFrameworksBuildPhase section */ 76 | 77 | /* Begin PBXGroup section */ 78 | 7F3A81511D5220D6004B78CE = { 79 | isa = PBXGroup; 80 | children = ( 81 | 7F3A815D1D5220D6004B78CE /* RealReachability */, 82 | 7F3A815C1D5220D6004B78CE /* Products */, 83 | ); 84 | sourceTree = ""; 85 | }; 86 | 7F3A815C1D5220D6004B78CE /* Products */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 7F3A815B1D5220D6004B78CE /* RealReachability.framework */, 90 | ); 91 | name = Products; 92 | sourceTree = ""; 93 | }; 94 | 7F3A815D1D5220D6004B78CE /* RealReachability */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 7F3A815E1D5220D6004B78CE /* RealReachability.h */, 98 | 7F3A81661D522132004B78CE /* Connection */, 99 | 7F3A81691D522132004B78CE /* FSM */, 100 | 7F3A817B1D522132004B78CE /* Ping */, 101 | 7F3A81811D522132004B78CE /* RealReachability.m */, 102 | 7F3A81601D5220D6004B78CE /* Info.plist */, 103 | ); 104 | path = RealReachability; 105 | sourceTree = ""; 106 | }; 107 | 7F3A81661D522132004B78CE /* Connection */ = { 108 | isa = PBXGroup; 109 | children = ( 110 | 7F3A81671D522132004B78CE /* LocalConnection.h */, 111 | 7F3A81681D522132004B78CE /* LocalConnection.m */, 112 | ); 113 | path = Connection; 114 | sourceTree = ""; 115 | }; 116 | 7F3A81691D522132004B78CE /* FSM */ = { 117 | isa = PBXGroup; 118 | children = ( 119 | 7F3A816A1D522132004B78CE /* FSMDefines.h */, 120 | 7F3A816B1D522132004B78CE /* FSMEngine.h */, 121 | 7F3A816C1D522132004B78CE /* FSMEngine.m */, 122 | 7F3A816D1D522132004B78CE /* FSMStateUtil.h */, 123 | 7F3A816E1D522132004B78CE /* FSMStateUtil.m */, 124 | 7F3A816F1D522132004B78CE /* ReachState.h */, 125 | 7F3A81701D522132004B78CE /* ReachState.m */, 126 | 7F3A81711D522132004B78CE /* ReachStateLoading.h */, 127 | 7F3A81721D522132004B78CE /* ReachStateLoading.m */, 128 | 7F3A81731D522132004B78CE /* ReachStateUnloaded.h */, 129 | 7F3A81741D522132004B78CE /* ReachStateUnloaded.m */, 130 | 7F3A81751D522132004B78CE /* ReachStateUnReachable.h */, 131 | 7F3A81761D522132004B78CE /* ReachStateUnReachable.m */, 132 | 7F3A81771D522132004B78CE /* ReachStateWIFI.h */, 133 | 7F3A81781D522132004B78CE /* ReachStateWIFI.m */, 134 | 7F3A81791D522132004B78CE /* ReachStateWWAN.h */, 135 | 7F3A817A1D522132004B78CE /* ReachStateWWAN.m */, 136 | ); 137 | path = FSM; 138 | sourceTree = ""; 139 | }; 140 | 7F3A817B1D522132004B78CE /* Ping */ = { 141 | isa = PBXGroup; 142 | children = ( 143 | 7F3A817C1D522132004B78CE /* PingFoundation.h */, 144 | 7F3A817D1D522132004B78CE /* PingFoundation.m */, 145 | 7F3A817E1D522132004B78CE /* PingHelper.h */, 146 | 7F3A817F1D522132004B78CE /* PingHelper.m */, 147 | ); 148 | path = Ping; 149 | sourceTree = ""; 150 | }; 151 | /* End PBXGroup section */ 152 | 153 | /* Begin PBXHeadersBuildPhase section */ 154 | 7F3A81581D5220D6004B78CE /* Headers */ = { 155 | isa = PBXHeadersBuildPhase; 156 | buildActionMask = 2147483647; 157 | files = ( 158 | 7F3A81971D522132004B78CE /* PingHelper.h in Headers */, 159 | 7F3A81821D522132004B78CE /* LocalConnection.h in Headers */, 160 | 7F3A81841D522132004B78CE /* FSMDefines.h in Headers */, 161 | 7F3A81851D522132004B78CE /* FSMEngine.h in Headers */, 162 | 7F3A818D1D522132004B78CE /* ReachStateUnloaded.h in Headers */, 163 | 7F3A81891D522132004B78CE /* ReachState.h in Headers */, 164 | 7F3A81931D522132004B78CE /* ReachStateWWAN.h in Headers */, 165 | 7F3A81911D522132004B78CE /* ReachStateWIFI.h in Headers */, 166 | 7F3A818B1D522132004B78CE /* ReachStateLoading.h in Headers */, 167 | 7F3A81871D522132004B78CE /* FSMStateUtil.h in Headers */, 168 | 7F3A818F1D522132004B78CE /* ReachStateUnReachable.h in Headers */, 169 | 7F3A815F1D5220D6004B78CE /* RealReachability.h in Headers */, 170 | 7F3A81951D522132004B78CE /* PingFoundation.h in Headers */, 171 | ); 172 | runOnlyForDeploymentPostprocessing = 0; 173 | }; 174 | /* End PBXHeadersBuildPhase section */ 175 | 176 | /* Begin PBXNativeTarget section */ 177 | 7F3A815A1D5220D6004B78CE /* RealReachability */ = { 178 | isa = PBXNativeTarget; 179 | buildConfigurationList = 7F3A81631D5220D6004B78CE /* Build configuration list for PBXNativeTarget "RealReachability" */; 180 | buildPhases = ( 181 | 7F3A81561D5220D6004B78CE /* Sources */, 182 | 7F3A81571D5220D6004B78CE /* Frameworks */, 183 | 7F3A81581D5220D6004B78CE /* Headers */, 184 | 7F3A81591D5220D6004B78CE /* Resources */, 185 | ); 186 | buildRules = ( 187 | ); 188 | dependencies = ( 189 | ); 190 | name = RealReachability; 191 | productName = RealReachability; 192 | productReference = 7F3A815B1D5220D6004B78CE /* RealReachability.framework */; 193 | productType = "com.apple.product-type.framework"; 194 | }; 195 | /* End PBXNativeTarget section */ 196 | 197 | /* Begin PBXProject section */ 198 | 7F3A81521D5220D6004B78CE /* Project object */ = { 199 | isa = PBXProject; 200 | attributes = { 201 | LastUpgradeCheck = 0730; 202 | ORGANIZATIONNAME = QCStudio; 203 | TargetAttributes = { 204 | 7F3A815A1D5220D6004B78CE = { 205 | CreatedOnToolsVersion = 7.3.1; 206 | }; 207 | }; 208 | }; 209 | buildConfigurationList = 7F3A81551D5220D6004B78CE /* Build configuration list for PBXProject "RealReachability" */; 210 | compatibilityVersion = "Xcode 3.2"; 211 | developmentRegion = English; 212 | hasScannedForEncodings = 0; 213 | knownRegions = ( 214 | English, 215 | en, 216 | ); 217 | mainGroup = 7F3A81511D5220D6004B78CE; 218 | productRefGroup = 7F3A815C1D5220D6004B78CE /* Products */; 219 | projectDirPath = ""; 220 | projectRoot = ""; 221 | targets = ( 222 | 7F3A815A1D5220D6004B78CE /* RealReachability */, 223 | ); 224 | }; 225 | /* End PBXProject section */ 226 | 227 | /* Begin PBXResourcesBuildPhase section */ 228 | 7F3A81591D5220D6004B78CE /* Resources */ = { 229 | isa = PBXResourcesBuildPhase; 230 | buildActionMask = 2147483647; 231 | files = ( 232 | ); 233 | runOnlyForDeploymentPostprocessing = 0; 234 | }; 235 | /* End PBXResourcesBuildPhase section */ 236 | 237 | /* Begin PBXSourcesBuildPhase section */ 238 | 7F3A81561D5220D6004B78CE /* Sources */ = { 239 | isa = PBXSourcesBuildPhase; 240 | buildActionMask = 2147483647; 241 | files = ( 242 | 7F3A81861D522132004B78CE /* FSMEngine.m in Sources */, 243 | 7F3A81881D522132004B78CE /* FSMStateUtil.m in Sources */, 244 | 7F3A81831D522132004B78CE /* LocalConnection.m in Sources */, 245 | 7F3A81981D522132004B78CE /* PingHelper.m in Sources */, 246 | 7F3A819A1D522132004B78CE /* RealReachability.m in Sources */, 247 | 7F3A818C1D522132004B78CE /* ReachStateLoading.m in Sources */, 248 | 7F3A818A1D522132004B78CE /* ReachState.m in Sources */, 249 | 7F3A81941D522132004B78CE /* ReachStateWWAN.m in Sources */, 250 | 7F3A81901D522132004B78CE /* ReachStateUnReachable.m in Sources */, 251 | 7F3A81921D522132004B78CE /* ReachStateWIFI.m in Sources */, 252 | 7F3A81961D522132004B78CE /* PingFoundation.m in Sources */, 253 | 7F3A818E1D522132004B78CE /* ReachStateUnloaded.m in Sources */, 254 | ); 255 | runOnlyForDeploymentPostprocessing = 0; 256 | }; 257 | /* End PBXSourcesBuildPhase section */ 258 | 259 | /* Begin XCBuildConfiguration section */ 260 | 7F3A81611D5220D6004B78CE /* Debug */ = { 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 | CURRENT_PROJECT_VERSION = 1; 281 | DEBUG_INFORMATION_FORMAT = dwarf; 282 | ENABLE_STRICT_OBJC_MSGSEND = YES; 283 | ENABLE_TESTABILITY = YES; 284 | GCC_C_LANGUAGE_STANDARD = gnu99; 285 | GCC_DYNAMIC_NO_PIC = NO; 286 | GCC_NO_COMMON_BLOCKS = YES; 287 | GCC_OPTIMIZATION_LEVEL = 0; 288 | GCC_PREPROCESSOR_DEFINITIONS = ( 289 | "DEBUG=1", 290 | "$(inherited)", 291 | ); 292 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 293 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 294 | GCC_WARN_UNDECLARED_SELECTOR = YES; 295 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 296 | GCC_WARN_UNUSED_FUNCTION = YES; 297 | GCC_WARN_UNUSED_VARIABLE = YES; 298 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 299 | MTL_ENABLE_DEBUG_INFO = YES; 300 | ONLY_ACTIVE_ARCH = YES; 301 | SDKROOT = iphoneos; 302 | TARGETED_DEVICE_FAMILY = "1,2"; 303 | VERSIONING_SYSTEM = "apple-generic"; 304 | VERSION_INFO_PREFIX = ""; 305 | }; 306 | name = Debug; 307 | }; 308 | 7F3A81621D5220D6004B78CE /* Release */ = { 309 | isa = XCBuildConfiguration; 310 | buildSettings = { 311 | ALWAYS_SEARCH_USER_PATHS = NO; 312 | CLANG_ANALYZER_NONNULL = YES; 313 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 314 | CLANG_CXX_LIBRARY = "libc++"; 315 | CLANG_ENABLE_MODULES = YES; 316 | CLANG_ENABLE_OBJC_ARC = YES; 317 | CLANG_WARN_BOOL_CONVERSION = YES; 318 | CLANG_WARN_CONSTANT_CONVERSION = YES; 319 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 320 | CLANG_WARN_EMPTY_BODY = YES; 321 | CLANG_WARN_ENUM_CONVERSION = YES; 322 | CLANG_WARN_INT_CONVERSION = YES; 323 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 324 | CLANG_WARN_UNREACHABLE_CODE = YES; 325 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 326 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 327 | COPY_PHASE_STRIP = NO; 328 | CURRENT_PROJECT_VERSION = 1; 329 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 330 | ENABLE_NS_ASSERTIONS = NO; 331 | ENABLE_STRICT_OBJC_MSGSEND = YES; 332 | GCC_C_LANGUAGE_STANDARD = gnu99; 333 | GCC_NO_COMMON_BLOCKS = YES; 334 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 335 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 336 | GCC_WARN_UNDECLARED_SELECTOR = YES; 337 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 338 | GCC_WARN_UNUSED_FUNCTION = YES; 339 | GCC_WARN_UNUSED_VARIABLE = YES; 340 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 341 | MTL_ENABLE_DEBUG_INFO = NO; 342 | SDKROOT = iphoneos; 343 | TARGETED_DEVICE_FAMILY = "1,2"; 344 | VALIDATE_PRODUCT = YES; 345 | VERSIONING_SYSTEM = "apple-generic"; 346 | VERSION_INFO_PREFIX = ""; 347 | }; 348 | name = Release; 349 | }; 350 | 7F3A81641D5220D6004B78CE /* Debug */ = { 351 | isa = XCBuildConfiguration; 352 | buildSettings = { 353 | APPLICATION_EXTENSION_API_ONLY = YES; 354 | DEFINES_MODULE = YES; 355 | DYLIB_COMPATIBILITY_VERSION = 1; 356 | DYLIB_CURRENT_VERSION = 1; 357 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 358 | INFOPLIST_FILE = RealReachability/Info.plist; 359 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 360 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 361 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 362 | PRODUCT_BUNDLE_IDENTIFIER = qc.RealReachability; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | SKIP_INSTALL = YES; 365 | }; 366 | name = Debug; 367 | }; 368 | 7F3A81651D5220D6004B78CE /* Release */ = { 369 | isa = XCBuildConfiguration; 370 | buildSettings = { 371 | APPLICATION_EXTENSION_API_ONLY = YES; 372 | DEFINES_MODULE = YES; 373 | DYLIB_COMPATIBILITY_VERSION = 1; 374 | DYLIB_CURRENT_VERSION = 1; 375 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 376 | INFOPLIST_FILE = RealReachability/Info.plist; 377 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 378 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 379 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 380 | PRODUCT_BUNDLE_IDENTIFIER = qc.RealReachability; 381 | PRODUCT_NAME = "$(TARGET_NAME)"; 382 | SKIP_INSTALL = YES; 383 | }; 384 | name = Release; 385 | }; 386 | /* End XCBuildConfiguration section */ 387 | 388 | /* Begin XCConfigurationList section */ 389 | 7F3A81551D5220D6004B78CE /* Build configuration list for PBXProject "RealReachability" */ = { 390 | isa = XCConfigurationList; 391 | buildConfigurations = ( 392 | 7F3A81611D5220D6004B78CE /* Debug */, 393 | 7F3A81621D5220D6004B78CE /* Release */, 394 | ); 395 | defaultConfigurationIsVisible = 0; 396 | defaultConfigurationName = Release; 397 | }; 398 | 7F3A81631D5220D6004B78CE /* Build configuration list for PBXNativeTarget "RealReachability" */ = { 399 | isa = XCConfigurationList; 400 | buildConfigurations = ( 401 | 7F3A81641D5220D6004B78CE /* Debug */, 402 | 7F3A81651D5220D6004B78CE /* Release */, 403 | ); 404 | defaultConfigurationIsVisible = 0; 405 | defaultConfigurationName = Release; 406 | }; 407 | /* End XCConfigurationList section */ 408 | }; 409 | rootObject = 7F3A81521D5220D6004B78CE /* Project object */; 410 | } 411 | -------------------------------------------------------------------------------- /RealReachability.xcodeproj/xcshareddata/xcschemes/RealReachability.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /RealReachability/Connection/LocalConnection.h: -------------------------------------------------------------------------------- 1 | // 2 | // LocalConnection.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | /// We post self to this notification, 13 | /// then you should invoke currentLocalConnectionStatus method to fetch current status. 14 | extern NSString *const kLocalConnectionChangedNotification; 15 | 16 | /// After start observering, we post this notification once, 17 | /// you may invoke currentLocalConnectionStatus method to fetch initial status. 18 | extern NSString *const kLocalConnectionInitializedNotification; 19 | 20 | typedef NS_ENUM(NSInteger, LocalConnectionStatus) 21 | { 22 | LC_UnReachable = 0, 23 | LC_WWAN = 1, 24 | LC_WiFi = 2 25 | }; 26 | 27 | @interface LocalConnection : NSObject 28 | 29 | /// Newly added property for KVO usage: 30 | /// maybe you only want to observe the local network is available or not. 31 | @property (nonatomic, assign) BOOL isReachable; 32 | 33 | /** 34 | * Start observering local connection status. 35 | */ 36 | - (void)startNotifier; 37 | 38 | /** 39 | * Stop observering local connection status. 40 | */ 41 | - (void)stopNotifier; 42 | 43 | /** 44 | * Return current local connection status immediately. 45 | * 46 | * @return see enum LocalConnectionStatus 47 | */ 48 | - (LocalConnectionStatus)currentLocalConnectionStatus; 49 | 50 | @end 51 | 52 | -------------------------------------------------------------------------------- /RealReachability/Connection/LocalConnection.m: -------------------------------------------------------------------------------- 1 | // 2 | // LocalConnection.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "LocalConnection.h" 10 | #import 11 | #import 12 | #import 13 | #import 14 | 15 | #if (!defined(DEBUG)) 16 | #define NSLog(...) 17 | #endif 18 | 19 | NSString *const kLocalConnectionInitializedNotification = @"kLocalConnectionInitializedNotification"; 20 | NSString *const kLocalConnectionChangedNotification = @"kLocalConnectionChangedNotification"; 21 | 22 | @interface LocalConnection () 23 | @property (assign, nonatomic) SCNetworkReachabilityRef reachabilityRef; 24 | @property (nonatomic, strong) dispatch_queue_t reachabilitySerialQueue; 25 | @property (nonatomic, assign) BOOL isNotifying; 26 | 27 | -(void)localConnectionChanged; 28 | @end 29 | 30 | // Start listening for reachability notifications on the current run loop 31 | static void LocalConnectionCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 32 | { 33 | #pragma unused (target) 34 | LocalConnection *connection = ((__bridge LocalConnection*)info); 35 | 36 | @autoreleasepool 37 | { 38 | [connection localConnectionChanged]; 39 | } 40 | } 41 | 42 | static NSString *connectionFlags(SCNetworkReachabilityFlags flags) 43 | { 44 | return [NSString stringWithFormat:@"%c%c %c%c%c%c%c%c%c", 45 | (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', 46 | (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', 47 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', 48 | (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', 49 | (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', 50 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', 51 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', 52 | (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', 53 | (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-']; 54 | } 55 | 56 | @implementation LocalConnection 57 | 58 | #pragma mark - Life Circle 59 | 60 | - (id)init 61 | { 62 | if ((self = [super init])) 63 | { 64 | struct sockaddr_in address; 65 | bzero(&address, sizeof(address)); 66 | address.sin_len = sizeof(address); 67 | address.sin_family = AF_INET; 68 | _reachabilityRef = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &address); 69 | 70 | _reachabilitySerialQueue = dispatch_queue_create("com.dustturtle.realreachability", NULL); 71 | } 72 | return self; 73 | } 74 | 75 | - (void)dealloc 76 | { 77 | [self stopNotifier]; 78 | 79 | if(_reachabilityRef != NULL) 80 | { 81 | CFRelease(_reachabilityRef); 82 | _reachabilityRef = NULL; 83 | } 84 | 85 | self.reachabilitySerialQueue = nil; 86 | } 87 | 88 | #pragma mark - Singlton Method 89 | 90 | + (instancetype)sharedInstance 91 | { 92 | static id localConnection = nil; 93 | static dispatch_once_t onceToken; 94 | dispatch_once(&onceToken, ^{ 95 | localConnection = [[self alloc] init]; 96 | }); 97 | 98 | return localConnection; 99 | } 100 | 101 | #pragma mark - actions 102 | 103 | - (void)startNotifier 104 | { 105 | if (self.isNotifying) 106 | { 107 | return; 108 | } 109 | 110 | self.isNotifying = YES; 111 | 112 | SCNetworkReachabilityContext context = { 0, NULL, NULL, NULL, NULL }; 113 | context.info = (__bridge void *)self; 114 | 115 | if(SCNetworkReachabilitySetCallback(self.reachabilityRef, LocalConnectionCallback, &context)) 116 | { 117 | // Set it as our reachability queue, which will retain the queue 118 | if(!SCNetworkReachabilitySetDispatchQueue(self.reachabilityRef, self.reachabilitySerialQueue)) 119 | { 120 | SCNetworkReachabilitySetCallback(self.reachabilityRef, NULL, NULL); 121 | NSLog(@"SCNetworkReachabilitySetDispatchQueue() failed: %s", SCErrorString(SCError())); 122 | } 123 | } 124 | else 125 | { 126 | NSLog(@"SCNetworkReachabilitySetCallback() failed: %s", SCErrorString(SCError())); 127 | } 128 | 129 | // First time we come in, notify the initialization of local connection. 130 | 131 | self.isReachable = [self _isReachable]; 132 | 133 | __weak __typeof(self)weakSelf = self; 134 | dispatch_async(dispatch_get_main_queue(), ^{ 135 | __strong __typeof(weakSelf)strongSelf = weakSelf; 136 | [[NSNotificationCenter defaultCenter] postNotificationName:kLocalConnectionInitializedNotification 137 | object:strongSelf]; 138 | }); 139 | } 140 | 141 | -(void)stopNotifier 142 | { 143 | if (!self.isNotifying) 144 | { 145 | return; 146 | } 147 | 148 | self.isNotifying = NO; 149 | 150 | // First: stop any callbacks. 151 | SCNetworkReachabilitySetCallback(self.reachabilityRef, NULL, NULL); 152 | 153 | // Second: unregister target from the GCD serial dispatch queue. 154 | SCNetworkReachabilitySetDispatchQueue(self.reachabilityRef, NULL); 155 | } 156 | 157 | #pragma mark - outside invoke 158 | - (LocalConnectionStatus)currentLocalConnectionStatus 159 | { 160 | if ([self _isReachable]) 161 | { 162 | if ([self isReachableViaWiFi]) 163 | { 164 | return LC_WiFi; 165 | } 166 | else 167 | { 168 | return LC_WWAN; 169 | } 170 | } 171 | else 172 | { 173 | return LC_UnReachable; 174 | } 175 | } 176 | 177 | #pragma mark - inner methods 178 | 179 | - (void)localConnectionChanged 180 | { 181 | self.isReachable = [self _isReachable]; 182 | 183 | // this makes sure the change notification happens on the MAIN THREAD 184 | __weak __typeof(self)weakSelf = self; 185 | dispatch_async(dispatch_get_main_queue(), ^{ 186 | __strong __typeof(weakSelf)strongSelf = weakSelf; 187 | [[NSNotificationCenter defaultCenter] postNotificationName:kLocalConnectionChangedNotification 188 | object:strongSelf]; 189 | }); 190 | } 191 | 192 | // for inner testing & debugging 193 | - (NSString *)currentConnectionFlags 194 | { 195 | return connectionFlags([self reachabilityFlags]); 196 | } 197 | 198 | -(SCNetworkReachabilityFlags)reachabilityFlags 199 | { 200 | SCNetworkReachabilityFlags flags = 0; 201 | 202 | if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags)) 203 | { 204 | return flags; 205 | } 206 | 207 | return 0; 208 | } 209 | 210 | #pragma mark - LocalReachability 211 | 212 | /// added underline prefix to distinguish the method from the property "isReachable" 213 | - (BOOL)_isReachable 214 | { 215 | SCNetworkReachabilityFlags flags; 216 | 217 | if(!SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags)) 218 | { 219 | return NO; 220 | } 221 | else 222 | { 223 | return [self isReachableWithFlags:flags]; 224 | } 225 | } 226 | 227 | - (BOOL)isReachableWithFlags:(SCNetworkReachabilityFlags)flags 228 | { 229 | if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 230 | { 231 | // if target host is not reachable 232 | return NO; 233 | } 234 | 235 | if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 236 | { 237 | // if target host is reachable and no connection is required 238 | // then we'll assume (for now) that you're on Wi-Fi 239 | return YES; 240 | } 241 | 242 | if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 243 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 244 | { 245 | // ... and the connection is on-demand (or on-traffic) if the 246 | // calling application is using the CFSocketStream or higher APIs 247 | 248 | if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 249 | { 250 | // ... and no [user] intervention is needed 251 | return YES; 252 | } 253 | } 254 | 255 | return NO; 256 | } 257 | 258 | - (BOOL)isReachableViaWWAN 259 | { 260 | SCNetworkReachabilityFlags flags = 0; 261 | 262 | if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags)) 263 | { 264 | if(flags & kSCNetworkReachabilityFlagsReachable) 265 | { 266 | if(flags & kSCNetworkReachabilityFlagsIsWWAN) 267 | { 268 | return YES; 269 | } 270 | } 271 | } 272 | 273 | return NO; 274 | } 275 | 276 | -(BOOL)isReachableViaWiFi 277 | { 278 | SCNetworkReachabilityFlags flags = 0; 279 | 280 | if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags)) 281 | { 282 | if((flags & kSCNetworkReachabilityFlagsReachable)) 283 | { 284 | if((flags & kSCNetworkReachabilityFlagsIsWWAN)) 285 | { 286 | return NO; 287 | } 288 | 289 | return YES; 290 | } 291 | } 292 | 293 | return NO; 294 | } 295 | 296 | @end 297 | -------------------------------------------------------------------------------- /RealReachability/FSM/FSMDefines.h: -------------------------------------------------------------------------------- 1 | // 2 | // FSMDefines.h 3 | // RealReachability 4 | // Defines of FSM (finite state machine) 5 | // 6 | // Created by Dustturtle on 16/1/19. 7 | // Copyright © 2016 Dustturtle. All rights reserved. 8 | // 9 | 10 | #ifndef FSMDefine_h 11 | #define FSMDefine_h 12 | 13 | #define kEventKeyID @"event_id" 14 | #define kEventKeyParam @"event_param" 15 | 16 | #define kParamValueUnReachable @"ParamValueUnReachable" 17 | #define kParamValueWWAN @"ParamValueWWAN" 18 | #define kParamValueWIFI @"ParamValueWIFI" 19 | 20 | typedef enum 21 | { 22 | RRStateInvalid = -1, 23 | RRStateUnloaded = 0, 24 | RRStateLoading, 25 | RRStateUnReachable, 26 | RRStateWIFI, 27 | RRStateWWAN 28 | }RRStateID; 29 | 30 | typedef enum 31 | { 32 | RREventLoad = 0, 33 | RREventUnLoad, 34 | RREventLocalConnectionCallback, 35 | RREventPingCallback 36 | }RREventID; 37 | 38 | /// FSM error codes below 39 | #define kFSMErrorNotAccept 13 40 | 41 | #endif /* FSMDefine_h */ 42 | -------------------------------------------------------------------------------- /RealReachability/FSM/FSMEngine.h: -------------------------------------------------------------------------------- 1 | // 2 | // FSMEngine.h 3 | // RealReachability 4 | // Engine of FSM (finite state machine) 5 | // 6 | // Created by Dustturtle on 16/1/19. 7 | // Copyright © 2016 Dustturtle. All rights reserved. 8 | // 9 | 10 | #import 11 | #import "FSMDefines.h" 12 | 13 | @interface FSMEngine : NSObject 14 | 15 | @property (nonatomic, readonly) RRStateID currentStateID; 16 | @property (nonatomic, readonly) NSArray *allStates; 17 | 18 | - (void)start; 19 | 20 | /** 21 | * trigger event 22 | * 23 | * @param dic inputDic 24 | * 25 | * @return -1 -> no state changed, 0 ->state changed 26 | */ 27 | - (NSInteger)receiveInput:(NSDictionary *)dic; 28 | 29 | - (BOOL)isCurrentStateAvailable; 30 | @end 31 | 32 | -------------------------------------------------------------------------------- /RealReachability/FSM/FSMEngine.m: -------------------------------------------------------------------------------- 1 | // 2 | // FSMEngine.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "FSMEngine.h" 10 | #import "ReachState.h" 11 | #import "ReachStateWIFI.h" 12 | #import "ReachStateUnloaded.h" 13 | #import "ReachStateWWAN.h" 14 | #import "ReachStateUnReachable.h" 15 | #import "ReachStateLoading.h" 16 | 17 | #if (!defined(DEBUG)) 18 | #define NSLog(...) 19 | #endif 20 | 21 | @interface FSMEngine() 22 | 23 | @property (nonatomic, assign) RRStateID currentStateID; 24 | @property (nonatomic, strong) NSArray *allStates; 25 | 26 | @end 27 | 28 | @implementation FSMEngine 29 | 30 | - (id)init 31 | { 32 | if (self = [super init]) 33 | { 34 | // created only once 35 | _allStates = @[[ReachStateUnloaded state], [ReachStateLoading state], [ReachStateUnReachable state], [ReachStateWIFI state], [ReachStateWWAN state]]; 36 | } 37 | return self; 38 | } 39 | 40 | - (void)dealloc 41 | { 42 | self.allStates = nil; 43 | } 44 | 45 | - (void)start 46 | { 47 | self.currentStateID = RRStateUnloaded; 48 | } 49 | 50 | - (NSInteger)receiveInput:(NSDictionary *)dic 51 | { 52 | NSError *error = nil; 53 | ReachState *currentState = self.allStates[self.currentStateID]; 54 | RRStateID newStateID = [currentState onEvent:dic withError:&error]; 55 | if (error) 56 | { 57 | NSLog(@"onEvent error:%@", error); 58 | } 59 | 60 | RRStateID previousStateID = self.currentStateID; 61 | self.currentStateID = newStateID; 62 | //NSLog(@"curStateID is %@", @(self.currentStateID)); 63 | 64 | return (previousStateID == self.currentStateID) ? -1 : 0; 65 | } 66 | 67 | - (BOOL)isCurrentStateAvailable 68 | { 69 | if (self.currentStateID == RRStateUnReachable || self.currentStateID == RRStateWWAN 70 | || self.currentStateID == RRStateWIFI) 71 | { 72 | return YES; 73 | } 74 | else 75 | { 76 | return NO; 77 | } 78 | } 79 | 80 | @end 81 | 82 | -------------------------------------------------------------------------------- /RealReachability/FSM/FSMStateUtil.h: -------------------------------------------------------------------------------- 1 | // 2 | // FSMStateUtil.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "FSMDefines.h" 11 | 12 | @interface FSMStateUtil : NSObject 13 | 14 | + (RRStateID)RRStateFromValue:(NSString *)LCEventValue; 15 | 16 | + (RRStateID)RRStateFromPingFlag:(BOOL)isSuccess; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /RealReachability/FSM/FSMStateUtil.m: -------------------------------------------------------------------------------- 1 | // 2 | // FSMStateUtil.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "FSMStateUtil.h" 10 | #import "RealReachability.h" 11 | 12 | @implementation FSMStateUtil 13 | 14 | + (RRStateID)RRStateFromValue:(NSString *)LCEventValue 15 | { 16 | if ([LCEventValue isEqualToString:kParamValueUnReachable]) 17 | { 18 | return RRStateUnReachable; 19 | } 20 | else if ([LCEventValue isEqualToString:kParamValueWWAN]) 21 | { 22 | return RRStateWWAN; 23 | } 24 | else if ([LCEventValue isEqualToString:kParamValueWIFI]) 25 | { 26 | return RRStateWIFI; 27 | } 28 | else 29 | { 30 | NSLog(@"Error! no matching LCEventValue!"); 31 | return RRStateInvalid; 32 | } 33 | } 34 | 35 | + (RRStateID)RRStateFromPingFlag:(BOOL)isSuccess 36 | { 37 | LocalConnectionStatus status = GLobalRealReachability.localObserver.currentLocalConnectionStatus; 38 | 39 | if (!isSuccess) 40 | { 41 | return RRStateUnReachable; 42 | } 43 | else 44 | { 45 | switch (status) 46 | { 47 | case LC_UnReachable: 48 | { 49 | NSLog(@"MisMatch! RRStateFromPingFlag success, but LC_UnReachable!"); 50 | return RRStateUnReachable; 51 | } 52 | case LC_WiFi: 53 | { 54 | return RRStateWIFI; 55 | } 56 | case LC_WWAN: 57 | { 58 | return RRStateWWAN; 59 | } 60 | 61 | default: 62 | { 63 | NSLog(@"RealReachability error! RRStateFromPingFlag not matched!"); 64 | return RRStateWIFI; 65 | } 66 | } 67 | } 68 | } 69 | 70 | @end 71 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachState.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReachState.h 3 | // RealReachability 4 | // Only handle events here: receive event in state,then transfer current state. 5 | // 6 | // Created by Dustturtle on 16/1/19. 7 | // Copyright © 2016 Dustturtle. All rights reserved. 8 | // 9 | 10 | #import 11 | #import "FSMStateUtil.h" 12 | 13 | @interface ReachState : NSObject 14 | 15 | /** 16 | * factory method 17 | * 18 | * @return state object 19 | */ 20 | + (id)state; 21 | 22 | /** 23 | * vitual method, for subclass override 24 | * 25 | * @param event see FSMDefines.h,dictionary with keys:kEventKeyID,kEventKeyParam 26 | * @param error error pointer 27 | * 28 | * @return return value description 29 | */ 30 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error; 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachState.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReachState.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachState.h" 10 | 11 | @implementation ReachState 12 | 13 | + (id)state 14 | { 15 | return [[self alloc] init]; 16 | } 17 | 18 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error 19 | { 20 | return RRStateInvalid; 21 | } 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateLoading.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateLoading.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachState.h" 10 | 11 | @interface ReachStateLoading : ReachState 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateLoading.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateLoading.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachStateLoading.h" 10 | 11 | @implementation ReachStateLoading 12 | 13 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error 14 | { 15 | RRStateID resStateID = RRStateLoading; 16 | 17 | NSNumber *eventID = event[kEventKeyID]; 18 | 19 | switch ([eventID intValue]) 20 | { 21 | case RREventUnLoad: 22 | { 23 | resStateID = RRStateUnloaded; 24 | break; 25 | } 26 | case RREventPingCallback: 27 | { 28 | NSNumber *eventParam = event[kEventKeyParam]; 29 | resStateID = [FSMStateUtil RRStateFromPingFlag:[eventParam boolValue]]; 30 | break; 31 | } 32 | case RREventLocalConnectionCallback: 33 | { 34 | resStateID = [FSMStateUtil RRStateFromValue:event[kEventKeyParam]]; 35 | break; 36 | } 37 | default: 38 | { 39 | if (error != NULL) 40 | { 41 | *error = [NSError errorWithDomain:@"FSM" code:kFSMErrorNotAccept userInfo:nil]; 42 | } 43 | break; 44 | } 45 | } 46 | return resStateID; 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateUnReachable.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateUnReachable.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachState.h" 10 | 11 | @interface ReachStateUnReachable : ReachState 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateUnReachable.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateUnReachable.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachStateUnReachable.h" 10 | 11 | @implementation ReachStateUnReachable 12 | 13 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error 14 | { 15 | RRStateID resStateID = RRStateUnReachable; 16 | 17 | NSNumber *eventID = event[kEventKeyID]; 18 | 19 | switch ([eventID intValue]) 20 | { 21 | case RREventUnLoad: 22 | { 23 | resStateID = RRStateUnloaded; 24 | break; 25 | } 26 | case RREventPingCallback: 27 | { 28 | NSNumber *eventParam = event[kEventKeyParam]; 29 | resStateID = [FSMStateUtil RRStateFromPingFlag:[eventParam boolValue]]; 30 | break; 31 | } 32 | case RREventLocalConnectionCallback: 33 | { 34 | resStateID = [FSMStateUtil RRStateFromValue:event[kEventKeyParam]]; 35 | break; 36 | } 37 | default: 38 | { 39 | if (error != NULL) 40 | { 41 | *error = [NSError errorWithDomain:@"FSM" code:kFSMErrorNotAccept userInfo:nil]; 42 | } 43 | break; 44 | } 45 | } 46 | return resStateID; 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateUnloaded.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateUnloaded.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachState.h" 10 | 11 | @interface ReachStateUnloaded : ReachState 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateUnloaded.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateUnloaded.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachStateUnloaded.h" 10 | 11 | @implementation ReachStateUnloaded 12 | 13 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error 14 | { 15 | RRStateID resStateID = RRStateUnloaded; 16 | 17 | NSNumber *eventID = event[kEventKeyID]; 18 | 19 | switch ([eventID intValue]) 20 | { 21 | case RREventLoad: 22 | { 23 | resStateID = RRStateLoading; 24 | break; 25 | } 26 | default: 27 | { 28 | if (error != NULL) 29 | { 30 | *error = [NSError errorWithDomain:@"FSM" code:kFSMErrorNotAccept userInfo:nil]; 31 | } 32 | break; 33 | } 34 | } 35 | return resStateID; 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateWIFI.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateWIFI.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachState.h" 10 | 11 | @interface ReachStateWIFI : ReachState 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateWIFI.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateWIFI.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachStateWIFI.h" 10 | 11 | @implementation ReachStateWIFI 12 | 13 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error 14 | { 15 | RRStateID resStateID = RRStateWIFI; 16 | 17 | NSNumber *eventID = event[kEventKeyID]; 18 | 19 | switch ([eventID intValue]) 20 | { 21 | case RREventUnLoad: 22 | { 23 | resStateID = RRStateUnloaded; 24 | break; 25 | } 26 | case RREventPingCallback: 27 | { 28 | NSNumber *eventParam = event[kEventKeyParam]; 29 | resStateID = [FSMStateUtil RRStateFromPingFlag:[eventParam boolValue]]; 30 | break; 31 | } 32 | case RREventLocalConnectionCallback: 33 | { 34 | resStateID = [FSMStateUtil RRStateFromValue:event[kEventKeyParam]]; 35 | break; 36 | } 37 | default: 38 | { 39 | if (error != NULL) 40 | { 41 | *error = [NSError errorWithDomain:@"FSM" code:kFSMErrorNotAccept userInfo:nil]; 42 | } 43 | break; 44 | } 45 | } 46 | return resStateID; 47 | } 48 | 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateWWAN.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateWWAN.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachState.h" 10 | 11 | @interface ReachStateWWAN : ReachState 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /RealReachability/FSM/ReachStateWWAN.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReachStateWWAN.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ReachStateWWAN.h" 10 | 11 | @implementation ReachStateWWAN 12 | 13 | - (RRStateID)onEvent:(NSDictionary *)event withError:(NSError **)error 14 | { 15 | RRStateID resStateID = RRStateWWAN; 16 | 17 | NSNumber *eventID = event[kEventKeyID]; 18 | 19 | switch ([eventID intValue]) 20 | { 21 | case RREventUnLoad: 22 | { 23 | resStateID = RRStateUnloaded; 24 | break; 25 | } 26 | case RREventPingCallback: 27 | { 28 | NSNumber *eventParam = event[kEventKeyParam]; 29 | resStateID = [FSMStateUtil RRStateFromPingFlag:[eventParam boolValue]]; 30 | break; 31 | } 32 | case RREventLocalConnectionCallback: 33 | { 34 | resStateID = [FSMStateUtil RRStateFromValue:event[kEventKeyParam]]; 35 | break; 36 | } 37 | default: 38 | { 39 | if (error != NULL) 40 | { 41 | *error = [NSError errorWithDomain:@"FSM" code:kFSMErrorNotAccept userInfo:nil]; 42 | } 43 | break; 44 | } 45 | } 46 | return resStateID; 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /RealReachability/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 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /RealReachability/Ping/PingFoundation.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | LICENSE: 4 | IMPORTANT: This Apple software is supplied to you by Apple 5 | Inc. ("Apple") in consideration of your agreement to the following 6 | terms, and your use, installation, modification or redistribution of 7 | this Apple software constitutes acceptance of these terms. If you do 8 | not agree with these terms, please do not use, install, modify or 9 | redistribute this Apple software. 10 | 11 | In consideration of your agreement to abide by the following terms, and 12 | subject to these terms, Apple grants you a personal, non-exclusive 13 | license, under Apple's copyrights in this original Apple software (the 14 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 15 | Software, with or without modifications, in source and/or binary forms; 16 | provided that if you redistribute the Apple Software in its entirety and 17 | without modifications, you must retain this notice and the following 18 | text and disclaimers in all such redistributions of the Apple Software. 19 | Neither the name, trademarks, service marks or logos of Apple Inc. may 20 | be used to endorse or promote products derived from the Apple Software 21 | without specific prior written permission from Apple. Except as 22 | expressly stated in this notice, no other rights or licenses, express or 23 | implied, are granted by Apple herein, including but not limited to any 24 | patent rights that may be infringed by your derivative works or by other 25 | works in which the Apple Software may be incorporated. 26 | 27 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 28 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 29 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 30 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 31 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 32 | 33 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 34 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 37 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 38 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 39 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 40 | POSSIBILITY OF SUCH DAMAGE. 41 | 42 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 43 | 44 | Abstract: 45 | An object wrapper around the low-level BSD Sockets ping function. 46 | 47 | Modified by dustturtle:openglnewbee@163.com, follow the license below. 48 | */ 49 | 50 | #import 51 | #import 52 | 53 | #if TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR 54 | #import 55 | #else 56 | #import 57 | #endif 58 | 59 | #include 60 | 61 | #pragma mark * PingFoundation 62 | 63 | // The PingFoundation class is a very simple class that lets you send and receive pings. 64 | 65 | @protocol PingFoundationDelegate; 66 | 67 | typedef NS_ENUM(NSInteger, PingFoundationAddressStyle) { 68 | PingFoundationAddressStyleAny, ///< Use the first IPv4 or IPv6 address found; the default. 69 | PingFoundationAddressStyleICMPv4, ///< Use the first IPv4 address found. 70 | PingFoundationAddressStyleICMPv6 ///< Use the first IPv6 address found. 71 | }; 72 | 73 | @interface PingFoundation : NSObject 74 | 75 | - (instancetype)init NS_UNAVAILABLE; 76 | 77 | /*! Initialise the object to ping the specified host. 78 | * \param hostName The DNS name of the host to ping; an IPv4 or IPv6 address in string form will 79 | * work here. 80 | * \returns The initialised object. 81 | */ 82 | 83 | - (instancetype)initWithHostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER; 84 | 85 | /*! A copy of the value passed to `-initWithHostName:`. 86 | */ 87 | 88 | @property (nonatomic, copy, readonly) NSString * hostName; 89 | 90 | /*! The delegate for this object. 91 | * \details Delegate callbacks are schedule in the default run loop mode of the run loop of the 92 | * thread that calls `-start`. 93 | */ 94 | 95 | @property (nonatomic, weak, readwrite) id delegate; 96 | 97 | /*! Controls the IP address version used by the object. 98 | * \details You should set this value before starting the object. 99 | */ 100 | 101 | @property (nonatomic, assign, readwrite) PingFoundationAddressStyle addressStyle; 102 | 103 | /*! The address being pinged. 104 | * \details The contents of the NSData is a (struct sockaddr) of some form. The 105 | * value is nil while the object is stopped and remains nil on start until 106 | * `-pingFoundation:didStartWithAddress:` is called. 107 | */ 108 | 109 | @property (nonatomic, copy, readonly) NSData * hostAddress; 110 | 111 | /*! The address family for `hostAddress`, or `AF_UNSPEC` if that's nil. 112 | */ 113 | 114 | @property (nonatomic, assign, readonly) sa_family_t hostAddressFamily; 115 | 116 | /*! The identifier used by pings by this object. 117 | * \details When you create an instance of this object it generates a random identifier 118 | * that it uses to identify its own pings. 119 | */ 120 | 121 | @property (nonatomic, assign, readonly) uint16_t identifier; 122 | 123 | /*! The next sequence number to be used by this object. 124 | * \details This value starts at zero and increments each time you send a ping (safely 125 | * wrapping back to zero if necessary). The sequence number is included in the ping, 126 | * allowing you to match up requests and responses, and thus calculate ping times and 127 | * so on. 128 | */ 129 | 130 | @property (nonatomic, assign, readonly) uint16_t nextSequenceNumber; 131 | 132 | - (void)start; 133 | // Starts the pinger object pinging. You should call this after 134 | // you've setup the delegate and any ping parameters. 135 | 136 | - (void)sendPingWithData:(NSData *)data; 137 | // Sends an actual ping. Pass nil for data to use a standard 56 byte payload (resulting in a 138 | // standard 64 byte ping). Otherwise pass a non-nil value and it will be appended to the 139 | // ICMP header. 140 | // 141 | // Do not try to send a ping before you receive the -PingFoundation:didStartWithAddress: delegate 142 | // callback. 143 | 144 | - (void)stop; 145 | // Stops the pinger object. You should call this when you're done 146 | // pinging. 147 | 148 | @end 149 | 150 | @protocol PingFoundationDelegate 151 | 152 | @optional 153 | 154 | /*! A PingFoundation delegate callback, called once the object has started up. 155 | * \details This is called shortly after you start the object to tell you that the 156 | * object has successfully started. On receiving this callback, you can call 157 | * `-sendPingWithData:` to send pings. 158 | * 159 | * If the object didn't start, `-pingFoundation:didFailWithError:` is called instead. 160 | * \param pinger The object issuing the callback. 161 | * \param address The address that's being pinged; at the time this delegate callback 162 | * is made, this will have the same value as the `hostAddress` property. 163 | */ 164 | 165 | - (void)pingFoundation:(PingFoundation *)pinger didStartWithAddress:(NSData *)address; 166 | 167 | /*! A PingFoundation delegate callback, called if the object fails to start up. 168 | * \details This is called shortly after you start the object to tell you that the 169 | * object has failed to start. The most likely cause of failure is a problem 170 | * resolving `hostName`. 171 | * 172 | * By the time this callback is called, the object has stopped (that is, you don't 173 | * need to call `-stop` yourself). 174 | * \param pinger The object issuing the callback. 175 | * \param error Describes the failure. 176 | */ 177 | 178 | - (void)pingFoundation:(PingFoundation *)pinger didFailWithError:(NSError *)error; 179 | 180 | /*! A PingFoundation delegate callback, called when the object has successfully sent a ping packet. 181 | * \details Each call to `-sendPingWithData:` will result in either a 182 | * `-pingFoundation:didSendPacket:sequenceNumber:` delegate callback or a 183 | * `-pingFoundation:didFailToSendPacket:sequenceNumber:error:` delegate callback (unless you 184 | * stop the object before you get the callback). These callbacks are currently delivered 185 | * synchronously from within `-sendPingWithData:`, but this synchronous behaviour is not 186 | * considered API. 187 | * \param pinger The object issuing the callback. 188 | * \param packet The packet that was sent; this includes the ICMP header (`ICMPHeader`) and the 189 | * data you passed to `-sendPingWithData:` but does not include any IP-level headers. 190 | * \param sequenceNumber The ICMP sequence number of that packet. 191 | */ 192 | 193 | - (void)pingFoundation:(PingFoundation *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; 194 | 195 | /*! A PingFoundation delegate callback, called when the object fails to send a ping packet. 196 | * \details Each call to `-sendPingWithData:` will result in either a 197 | * `-pingFoundation:didSendPacket:sequenceNumber:` delegate callback or a 198 | * `-pingFoundation:didFailToSendPacket:sequenceNumber:error:` delegate callback (unless you 199 | * stop the object before you get the callback). These callbacks are currently delivered 200 | * synchronously from within `-sendPingWithData:`, but this synchronous behaviour is not 201 | * considered API. 202 | * \param pinger The object issuing the callback. 203 | * \param packet The packet that was not sent; see `-pingFoundation:didSendPacket:sequenceNumber:` 204 | * for details. 205 | * \param sequenceNumber The ICMP sequence number of that packet. 206 | * \param error Describes the failure. 207 | */ 208 | 209 | - (void)pingFoundation:(PingFoundation *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error; 210 | 211 | /*! A PingFoundation delegate callback, called when the object receives a ping response. 212 | * \details If the object receives an ping response that matches a ping request that it 213 | * sent, it informs the delegate via this callback. Matching is primarily done based on 214 | * the ICMP identifier, although other criteria are used as well. 215 | * \param pinger The object issuing the callback. 216 | * \param packet The packet received; this includes the ICMP header (`ICMPHeader`) and any data that 217 | * follows that in the ICMP message but does not include any IP-level headers. 218 | * \param sequenceNumber The ICMP sequence number of that packet. 219 | */ 220 | 221 | - (void)pingFoundation:(PingFoundation *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber; 222 | 223 | /*! A PingFoundation delegate callback, called when the object receives an unmatched ICMP message. 224 | * \details If the object receives an ICMP message that does not match a ping request that it 225 | * sent, it informs the delegate via this callback. The nature of ICMP handling in a 226 | * BSD kernel makes this a common event because, when an ICMP message arrives, it is 227 | * delivered to all ICMP sockets. 228 | * 229 | * IMPORTANT: This callback is especially common when using IPv6 because IPv6 uses ICMP 230 | * for important network management functions. For example, IPv6 routers periodically 231 | * send out Router Advertisement (RA) packets via Neighbor Discovery Protocol (NDP), which 232 | * is implemented on top of ICMP. 233 | * 234 | * For more on matching, see the discussion associated with 235 | * `-pingFoundation:didReceivePingResponsePacket:sequenceNumber:`. 236 | * \param pinger The object issuing the callback. 237 | * \param packet The packet received; this includes the ICMP header (`ICMPHeader`) and any data that 238 | * follows that in the ICMP message but does not include any IP-level headers. 239 | */ 240 | 241 | - (void)pingFoundation:(PingFoundation *)pinger didReceiveUnexpectedPacket:(NSData *)packet; 242 | 243 | @end 244 | 245 | #pragma mark * IP and ICMP On-The-Wire Format 246 | 247 | /*! Describes the on-the-wire header format for an ICMP ping. 248 | * \details This defines the header structure of ping packets on the wire. Both IPv4 and 249 | * IPv6 use the same basic structure. 250 | * 251 | * This is declared in the header because clients of PingFoundation might want to use 252 | * it parse received ping packets. 253 | */ 254 | 255 | // ICMP type and code combinations: 256 | 257 | enum { 258 | ICMPv4TypeEchoRequest = 8, ///< The ICMP `type` for a ping request; in this case `code` is always 0. 259 | ICMPv4TypeEchoReply = 0 ///< The ICMP `type` for a ping response; in this case `code` is always 0. 260 | }; 261 | 262 | enum { 263 | ICMPv6TypeEchoRequest = 128, ///< The ICMP `type` for a ping request; in this case `code` is always 0. 264 | ICMPv6TypeEchoReply = 129 ///< The ICMP `type` for a ping response; in this case `code` is always 0. 265 | }; 266 | 267 | // ICMP header structure: 268 | 269 | struct ICMPHeader 270 | { 271 | uint8_t type; 272 | uint8_t code; 273 | uint16_t checksum; 274 | uint16_t identifier; 275 | uint16_t sequenceNumber; 276 | // data... 277 | }; 278 | typedef struct ICMPHeader ICMPHeader; 279 | 280 | __Check_Compile_Time(sizeof(ICMPHeader) == 8); 281 | __Check_Compile_Time(offsetof(ICMPHeader, type) == 0); 282 | __Check_Compile_Time(offsetof(ICMPHeader, code) == 1); 283 | __Check_Compile_Time(offsetof(ICMPHeader, checksum) == 2); 284 | __Check_Compile_Time(offsetof(ICMPHeader, identifier) == 4); 285 | __Check_Compile_Time(offsetof(ICMPHeader, sequenceNumber) == 6); 286 | 287 | -------------------------------------------------------------------------------- /RealReachability/Ping/PingFoundation.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 | LICENSE: 4 | IMPORTANT: This Apple software is supplied to you by Apple 5 | Inc. ("Apple") in consideration of your agreement to the following 6 | terms, and your use, installation, modification or redistribution of 7 | this Apple software constitutes acceptance of these terms. If you do 8 | not agree with these terms, please do not use, install, modify or 9 | redistribute this Apple software. 10 | 11 | In consideration of your agreement to abide by the following terms, and 12 | subject to these terms, Apple grants you a personal, non-exclusive 13 | license, under Apple's copyrights in this original Apple software (the 14 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 15 | Software, with or without modifications, in source and/or binary forms; 16 | provided that if you redistribute the Apple Software in its entirety and 17 | without modifications, you must retain this notice and the following 18 | text and disclaimers in all such redistributions of the Apple Software. 19 | Neither the name, trademarks, service marks or logos of Apple Inc. may 20 | be used to endorse or promote products derived from the Apple Software 21 | without specific prior written permission from Apple. Except as 22 | expressly stated in this notice, no other rights or licenses, express or 23 | implied, are granted by Apple herein, including but not limited to any 24 | patent rights that may be infringed by your derivative works or by other 25 | works in which the Apple Software may be incorporated. 26 | 27 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 28 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 29 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 30 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 31 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 32 | 33 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 34 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 37 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 38 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 39 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 40 | POSSIBILITY OF SUCH DAMAGE. 41 | 42 | Copyright (C) 2016 Apple Inc. All Rights Reserved. 43 | 44 | Abstract: 45 | An object wrapper around the low-level BSD Sockets ping function. 46 | 47 | Modified by dustturtle:openglnewbee@163.com, follow the license below. 48 | */ 49 | 50 | #import "PingFoundation.h" 51 | 52 | #include 53 | #include 54 | #include 55 | 56 | #pragma mark * IPv4 and ICMPv4 On-The-Wire Format 57 | 58 | /*! Describes the on-the-wire header format for an IPv4 packet. 59 | * \details This defines the header structure of IPv4 packets on the wire. We need 60 | * this in order to skip this header in the IPv4 case, where the kernel passes 61 | * it to us for no obvious reason. 62 | */ 63 | 64 | struct IPv4Header { 65 | uint8_t versionAndHeaderLength; 66 | uint8_t differentiatedServices; 67 | uint16_t totalLength; 68 | uint16_t identification; 69 | uint16_t flagsAndFragmentOffset; 70 | uint8_t timeToLive; 71 | uint8_t protocol; 72 | uint16_t headerChecksum; 73 | uint8_t sourceAddress[4]; 74 | uint8_t destinationAddress[4]; 75 | // options... 76 | // data... 77 | }; 78 | typedef struct IPv4Header IPv4Header; 79 | 80 | __Check_Compile_Time(sizeof(IPv4Header) == 20); 81 | __Check_Compile_Time(offsetof(IPv4Header, versionAndHeaderLength) == 0); 82 | __Check_Compile_Time(offsetof(IPv4Header, differentiatedServices) == 1); 83 | __Check_Compile_Time(offsetof(IPv4Header, totalLength) == 2); 84 | __Check_Compile_Time(offsetof(IPv4Header, identification) == 4); 85 | __Check_Compile_Time(offsetof(IPv4Header, flagsAndFragmentOffset) == 6); 86 | __Check_Compile_Time(offsetof(IPv4Header, timeToLive) == 8); 87 | __Check_Compile_Time(offsetof(IPv4Header, protocol) == 9); 88 | __Check_Compile_Time(offsetof(IPv4Header, headerChecksum) == 10); 89 | __Check_Compile_Time(offsetof(IPv4Header, sourceAddress) == 12); 90 | __Check_Compile_Time(offsetof(IPv4Header, destinationAddress) == 16); 91 | 92 | /*! Calculates an IP checksum. 93 | * \details This is the standard BSD checksum code, modified to use modern types. 94 | * \param buffer A pointer to the data to checksum. 95 | * \param bufferLen The length of that data. 96 | * \returns The checksum value, in network byte order. 97 | */ 98 | 99 | static uint16_t in_cksum(const void *buffer, size_t bufferLen) { 100 | // 101 | size_t bytesLeft; 102 | int32_t sum; 103 | const uint16_t * cursor; 104 | union { 105 | uint16_t us; 106 | uint8_t uc[2]; 107 | } last; 108 | uint16_t answer; 109 | 110 | bytesLeft = bufferLen; 111 | sum = 0; 112 | cursor = buffer; 113 | 114 | /* 115 | * Our algorithm is simple, using a 32 bit accumulator (sum), we add 116 | * sequential 16 bit words to it, and at the end, fold back all the 117 | * carry bits from the top 16 bits into the lower 16 bits. 118 | */ 119 | while (bytesLeft > 1) { 120 | sum += *cursor; 121 | cursor += 1; 122 | bytesLeft -= 2; 123 | } 124 | 125 | /* mop up an odd byte, if necessary */ 126 | if (bytesLeft == 1) { 127 | last.uc[0] = * (const uint8_t *) cursor; 128 | last.uc[1] = 0; 129 | sum += last.us; 130 | } 131 | 132 | /* add back carry outs from top 16 bits to low 16 bits */ 133 | sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 134 | sum += (sum >> 16); /* add carry */ 135 | answer = (uint16_t) ~sum; /* truncate to 16 bits */ 136 | 137 | return answer; 138 | } 139 | 140 | #pragma mark * PingFoundation 141 | 142 | @interface PingFoundation () 143 | 144 | // read/write versions of public properties 145 | 146 | @property (nonatomic, copy, readwrite, nullable) NSData * hostAddress; 147 | @property (nonatomic, assign, readwrite ) uint16_t nextSequenceNumber; 148 | 149 | // private properties 150 | 151 | /*! True if nextSequenceNumber has wrapped from 65535 to 0. 152 | */ 153 | 154 | @property (nonatomic, assign, readwrite) BOOL nextSequenceNumberHasWrapped; 155 | 156 | /*! A host object for name-to-address resolution. 157 | */ 158 | 159 | @property (nonatomic, strong, readwrite, nullable) CFHostRef host __attribute__ ((NSObject)); 160 | 161 | /*! A socket object for ICMP send and receive. 162 | */ 163 | 164 | @property (nonatomic, strong, readwrite, nullable) CFSocketRef socket __attribute__ ((NSObject)); 165 | 166 | @end 167 | 168 | @implementation PingFoundation 169 | 170 | - (instancetype)initWithHostName:(NSString *)hostName 171 | { 172 | if ([hostName length] <= 0) 173 | { 174 | return nil; 175 | } 176 | 177 | self = [super init]; 178 | if (self != nil) { 179 | self->_hostName = [hostName copy]; 180 | self->_identifier = (uint16_t) arc4random(); 181 | } 182 | return self; 183 | } 184 | 185 | - (void)dealloc 186 | { 187 | [self stop]; 188 | } 189 | 190 | - (sa_family_t)hostAddressFamily { 191 | sa_family_t result; 192 | 193 | result = AF_UNSPEC; 194 | if ( (self.hostAddress != nil) && (self.hostAddress.length >= sizeof(struct sockaddr)) ) 195 | { 196 | result = ((const struct sockaddr *) self.hostAddress.bytes)->sa_family; 197 | } 198 | return result; 199 | } 200 | 201 | /*! Shuts down the pinger object and tell the delegate about the error. 202 | * \param error Describes the failure. 203 | */ 204 | 205 | - (void)didFailWithError:(NSError *)error { 206 | id strongDelegate; 207 | 208 | // We retain ourselves temporarily because it's common for the delegate method 209 | // to release its last reference to us, which causes -dealloc to be called here. 210 | // If we then reference self on the return path, things go badly. I don't think 211 | // that happens currently, but I've got into the habit of doing this as a 212 | // defensive measure. 213 | 214 | CFAutorelease(CFBridgingRetain(self)); 215 | 216 | [self stop]; 217 | 218 | strongDelegate = self.delegate; 219 | if ((strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(pingFoundation:didFailWithError:)]) 220 | { 221 | [strongDelegate pingFoundation:self didFailWithError:error]; 222 | } 223 | } 224 | 225 | /*! Shuts down the pinger object and tell the delegate about the error. 226 | * \details This converts the CFStreamError to an NSError and then call through to 227 | * -didFailWithError: to do the real work. 228 | * \param streamError Describes the failure. 229 | */ 230 | 231 | - (void)didFailWithHostStreamError:(CFStreamError)streamError { 232 | NSDictionary * userInfo; 233 | NSError * error; 234 | 235 | if (streamError.domain == kCFStreamErrorDomainNetDB) { 236 | userInfo = @{(id) kCFGetAddrInfoFailureKey: @(streamError.error)}; 237 | } else { 238 | userInfo = nil; 239 | } 240 | error = [NSError errorWithDomain:(NSString *) kCFErrorDomainCFNetwork code:kCFHostErrorUnknown userInfo:userInfo]; 241 | 242 | [self didFailWithError:error]; 243 | } 244 | 245 | /*! Builds a ping packet from the supplied parameters. 246 | * \param type The packet type, which is different for IPv4 and IPv6. 247 | * \param payload Data to place after the ICMP header. 248 | * \param requiresChecksum Determines whether a checksum is calculated (IPv4) or not (IPv6). 249 | * \returns A ping packet suitable to be passed to the kernel. 250 | */ 251 | 252 | - (NSData *)pingPacketWithType:(uint8_t)type payload:(NSData *)payload requiresChecksum:(BOOL)requiresChecksum { 253 | NSMutableData * packet; 254 | ICMPHeader * icmpPtr; 255 | 256 | packet = [NSMutableData dataWithLength:sizeof(*icmpPtr) + payload.length]; 257 | 258 | icmpPtr = packet.mutableBytes; 259 | icmpPtr->type = type; 260 | icmpPtr->code = 0; 261 | icmpPtr->checksum = 0; 262 | icmpPtr->identifier = OSSwapHostToBigInt16(self.identifier); 263 | icmpPtr->sequenceNumber = OSSwapHostToBigInt16(self.nextSequenceNumber); 264 | memcpy(&icmpPtr[1], [payload bytes], [payload length]); 265 | 266 | if (requiresChecksum) { 267 | // The IP checksum routine returns a 16-bit number that's already in correct byte order 268 | // (due to wacky 1's complement maths), so we just put it into the packet as a 16-bit unit. 269 | 270 | icmpPtr->checksum = in_cksum(packet.bytes, packet.length); 271 | } 272 | 273 | return packet; 274 | } 275 | 276 | - (void)sendPingWithData:(NSData *)data { 277 | int err; 278 | NSData * payload; 279 | NSData * packet; 280 | ssize_t bytesSent; 281 | id strongDelegate; 282 | 283 | // data may be nil 284 | 285 | // Construct the ping packet. 286 | 287 | payload = data; 288 | if (payload == nil) 289 | { 290 | payload = [[NSString stringWithFormat:@"%28zd bottles of beer on the wall", (ssize_t) 99 - (size_t) (self.nextSequenceNumber % 100) ] dataUsingEncoding:NSASCIIStringEncoding]; 291 | // Our dummy payload is sized so that the resulting ICMP packet, including the ICMPHeader, is 292 | // 64-bytes, which makes it easier to recognise our packets on the wire. 293 | } 294 | 295 | switch (self.hostAddressFamily) { 296 | case AF_INET: { 297 | packet = [self pingPacketWithType:ICMPv4TypeEchoRequest payload:payload requiresChecksum:YES]; 298 | } break; 299 | case AF_INET6: { 300 | packet = [self pingPacketWithType:ICMPv6TypeEchoRequest payload:payload requiresChecksum:NO]; 301 | } break; 302 | default: { 303 | assert(NO); 304 | } break; 305 | } 306 | 307 | // Send the packet. 308 | 309 | if (self.socket == NULL) { 310 | bytesSent = -1; 311 | err = EBADF; 312 | } else { 313 | bytesSent = sendto( 314 | CFSocketGetNative(self.socket), 315 | packet.bytes, 316 | packet.length, 317 | SO_NOSIGPIPE, 318 | self.hostAddress.bytes, 319 | (socklen_t) self.hostAddress.length 320 | ); 321 | err = 0; 322 | if (bytesSent < 0) { 323 | err = errno; 324 | } 325 | } 326 | 327 | // Handle the results of the send. 328 | 329 | strongDelegate = self.delegate; 330 | if ( (bytesSent > 0) && (((NSUInteger) bytesSent) == packet.length) ) { 331 | 332 | // Complete success. Tell the client. 333 | 334 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(pingFoundation:didSendPacket:sequenceNumber:)] ) { 335 | [strongDelegate pingFoundation:self didSendPacket:packet sequenceNumber:self.nextSequenceNumber]; 336 | } 337 | } else { 338 | NSError * error; 339 | 340 | // Some sort of failure. Tell the client. 341 | 342 | if (err == 0) { 343 | err = ENOBUFS; // This is not a hugely descriptor error, alas. 344 | } 345 | error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]; 346 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(pingFoundation:didFailToSendPacket:sequenceNumber:error:)] ) { 347 | [strongDelegate pingFoundation:self didFailToSendPacket:packet sequenceNumber:self.nextSequenceNumber error:error]; 348 | } 349 | } 350 | 351 | self.nextSequenceNumber += 1; 352 | if (self.nextSequenceNumber == 0) { 353 | self.nextSequenceNumberHasWrapped = YES; 354 | } 355 | } 356 | 357 | /*! Calculates the offset of the ICMP header within an IPv4 packet. 358 | * \details In the IPv4 case the kernel returns us a buffer that includes the 359 | * IPv4 header. We're not interested in that, so we have to skip over it. 360 | * This code does a rough check of the IPv4 header and, if it looks OK, 361 | * returns the offset of the ICMP header. 362 | * \param packet The IPv4 packet, as returned to us by the kernel. 363 | * \returns The offset of the ICMP header, or NSNotFound. 364 | */ 365 | 366 | + (NSUInteger)icmpHeaderOffsetInIPv4Packet:(NSData *)packet { 367 | // Returns the offset of the ICMPv4Header within an IP packet. 368 | NSUInteger result; 369 | const struct IPv4Header * ipPtr; 370 | size_t ipHeaderLength; 371 | 372 | result = NSNotFound; 373 | if (packet.length >= (sizeof(IPv4Header) + sizeof(ICMPHeader))) { 374 | ipPtr = (const IPv4Header *) packet.bytes; 375 | if ( ((ipPtr->versionAndHeaderLength & 0xF0) == 0x40) && // IPv4 376 | ( ipPtr->protocol == IPPROTO_ICMP ) ) { 377 | ipHeaderLength = (ipPtr->versionAndHeaderLength & 0x0F) * sizeof(uint32_t); 378 | if (packet.length >= (ipHeaderLength + sizeof(ICMPHeader))) { 379 | result = ipHeaderLength; 380 | } 381 | } 382 | } 383 | return result; 384 | } 385 | 386 | /*! Checks whether the specified sequence number is one we sent. 387 | * \param sequenceNumber The incoming sequence number. 388 | * \returns YES if the sequence number looks like one we sent. 389 | */ 390 | 391 | - (BOOL)validateSequenceNumber:(uint16_t)sequenceNumber { 392 | if (self.nextSequenceNumberHasWrapped) { 393 | // If the sequence numbers have wrapped that we can't reliably check 394 | // whether this is a sequence number we sent. Rather, we check to see 395 | // whether the sequence number is within the last 120 sequence numbers 396 | // we sent. Note that the uint16_t subtraction here does the right 397 | // thing regardless of the wrapping. 398 | // 399 | // Why 120? Well, if we send one ping per second, 120 is 2 minutes, which 400 | // is the standard "max time a packet can bounce around the Internet" value. 401 | return ((uint16_t) (self.nextSequenceNumber - sequenceNumber)) < (uint16_t) 120; 402 | } else { 403 | return sequenceNumber < self.nextSequenceNumber; 404 | } 405 | } 406 | 407 | /*! Checks whether an incoming IPv4 packet looks like a ping response. 408 | * \details This routine modifies this `packet` data! It does this for two reasons: 409 | * 410 | * * It needs to zero out the `checksum` field of the ICMPHeader in order to do 411 | * its checksum calculation. 412 | * 413 | * * It removes the IPv4 header from the front of the packet. 414 | * \param packet The IPv4 packet, as returned to us by the kernel. 415 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 416 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 417 | */ 418 | 419 | - (BOOL)validatePing4ResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 420 | BOOL result; 421 | NSUInteger icmpHeaderOffset; 422 | ICMPHeader * icmpPtr; 423 | uint16_t receivedChecksum; 424 | uint16_t calculatedChecksum; 425 | 426 | result = NO; 427 | 428 | icmpHeaderOffset = [[self class] icmpHeaderOffsetInIPv4Packet:packet]; 429 | if (icmpHeaderOffset != NSNotFound) { 430 | icmpPtr = (struct ICMPHeader *) (((uint8_t *) packet.mutableBytes) + icmpHeaderOffset); 431 | 432 | receivedChecksum = icmpPtr->checksum; 433 | icmpPtr->checksum = 0; 434 | calculatedChecksum = in_cksum(icmpPtr, packet.length - icmpHeaderOffset); 435 | icmpPtr->checksum = receivedChecksum; 436 | 437 | if (receivedChecksum == calculatedChecksum) { 438 | if ( (icmpPtr->type == ICMPv4TypeEchoReply) && (icmpPtr->code == 0) ) { 439 | if ( OSSwapBigToHostInt16(icmpPtr->identifier) == self.identifier ) { 440 | uint16_t sequenceNumber; 441 | 442 | sequenceNumber = OSSwapBigToHostInt16(icmpPtr->sequenceNumber); 443 | if ([self validateSequenceNumber:sequenceNumber]) { 444 | 445 | // Remove the IPv4 header off the front of the data we received, leaving us with 446 | // just the ICMP header and the ping payload. 447 | [packet replaceBytesInRange:NSMakeRange(0, icmpHeaderOffset) withBytes:NULL length:0]; 448 | 449 | *sequenceNumberPtr = sequenceNumber; 450 | result = YES; 451 | } 452 | } 453 | } 454 | } 455 | } 456 | 457 | return result; 458 | } 459 | 460 | /*! Checks whether an incoming IPv6 packet looks like a ping response. 461 | * \param packet The IPv6 packet, as returned to us by the kernel; note that this routine 462 | * could modify this data but does not need to in the IPv6 case. 463 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 464 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 465 | */ 466 | 467 | - (BOOL)validatePing6ResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 468 | BOOL result; 469 | const ICMPHeader * icmpPtr; 470 | 471 | result = NO; 472 | 473 | if (packet.length >= sizeof(*icmpPtr)) { 474 | icmpPtr = packet.bytes; 475 | 476 | // In the IPv6 case we don't check the checksum because that's hard (we need to 477 | // cook up an IPv6 pseudo header and we don't have the ingredients) and unnecessary 478 | // (the kernel has already done this check). 479 | 480 | if ( (icmpPtr->type == ICMPv6TypeEchoReply) && (icmpPtr->code == 0) ) { 481 | if ( OSSwapBigToHostInt16(icmpPtr->identifier) == self.identifier ) { 482 | uint16_t sequenceNumber; 483 | 484 | sequenceNumber = OSSwapBigToHostInt16(icmpPtr->sequenceNumber); 485 | if ([self validateSequenceNumber:sequenceNumber]) { 486 | *sequenceNumberPtr = sequenceNumber; 487 | result = YES; 488 | } 489 | } 490 | } 491 | } 492 | return result; 493 | } 494 | 495 | /*! Checks whether an incoming packet looks like a ping response. 496 | * \param packet The packet, as returned to us by the kernel; note that may end up modifying 497 | * this data. 498 | * \param sequenceNumberPtr A pointer to a place to start the ICMP sequence number. 499 | * \returns YES if the packet looks like a reasonable IPv4 ping response. 500 | */ 501 | 502 | - (BOOL)validatePingResponsePacket:(NSMutableData *)packet sequenceNumber:(uint16_t *)sequenceNumberPtr { 503 | BOOL result; 504 | 505 | switch (self.hostAddressFamily) { 506 | case AF_INET: { 507 | result = [self validatePing4ResponsePacket:packet sequenceNumber:sequenceNumberPtr]; 508 | } break; 509 | case AF_INET6: { 510 | result = [self validatePing6ResponsePacket:packet sequenceNumber:sequenceNumberPtr]; 511 | } break; 512 | default: { 513 | assert(NO); 514 | result = NO; 515 | } break; 516 | } 517 | return result; 518 | } 519 | 520 | /*! Reads data from the ICMP socket. 521 | * \details Called by the socket handling code (SocketReadCallback) to process an ICMP 522 | * message waiting on the socket. 523 | */ 524 | 525 | - (void)readData { 526 | int err; 527 | struct sockaddr_storage addr; 528 | socklen_t addrLen; 529 | ssize_t bytesRead; 530 | void * buffer; 531 | enum { kBufferSize = 65535 }; 532 | 533 | // 65535 is the maximum IP packet size, which seems like a reasonable bound 534 | // here (plus it's what uses). 535 | 536 | buffer = malloc(kBufferSize); 537 | if (buffer == NULL) 538 | { 539 | return; 540 | } 541 | 542 | // Actually read the data. We use recvfrom(), and thus get back the source address, 543 | // but we don't actually do anything with it. It would be trivial to pass it to 544 | // the delegate but we don't need it in this example. 545 | 546 | addrLen = sizeof(addr); 547 | bytesRead = recvfrom(CFSocketGetNative(self.socket), buffer, kBufferSize, 0, (struct sockaddr *) &addr, &addrLen); 548 | err = 0; 549 | if (bytesRead < 0) { 550 | err = errno; 551 | } 552 | 553 | // Process the data we read. 554 | 555 | if (bytesRead > 0) { 556 | NSMutableData * packet; 557 | id strongDelegate; 558 | uint16_t sequenceNumber; 559 | 560 | packet = [NSMutableData dataWithBytes:buffer length:(NSUInteger) bytesRead]; 561 | // We got some data, pass it up to our client. 562 | 563 | strongDelegate = self.delegate; 564 | if ( [self validatePingResponsePacket:packet sequenceNumber:&sequenceNumber] ) { 565 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(pingFoundation:didReceivePingResponsePacket:sequenceNumber:)] ) { 566 | [strongDelegate pingFoundation:self didReceivePingResponsePacket:packet sequenceNumber:sequenceNumber]; 567 | } 568 | } else { 569 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(pingFoundation:didReceiveUnexpectedPacket:)] ) { 570 | [strongDelegate pingFoundation:self didReceiveUnexpectedPacket:packet]; 571 | } 572 | } 573 | } else { 574 | 575 | // We failed to read the data, so shut everything down. 576 | 577 | if (err == 0) { 578 | err = EPIPE; 579 | } 580 | [self didFailWithError:[NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]]; 581 | } 582 | 583 | free(buffer); 584 | 585 | // Note that we don't loop back trying to read more data. Rather, we just 586 | // let CFSocket call us again. 587 | } 588 | 589 | /*! The callback for our CFSocket object. 590 | * \details This simply routes the call to our `-readData` method. 591 | * \param s See the documentation for CFSocketCallBack. 592 | * \param type See the documentation for CFSocketCallBack. 593 | * \param address See the documentation for CFSocketCallBack. 594 | * \param data See the documentation for CFSocketCallBack. 595 | * \param info See the documentation for CFSocketCallBack; this is actually a pointer to the 596 | * 'owning' object. 597 | */ 598 | 599 | static void SocketReadCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { 600 | // This C routine is called by CFSocket when there's data waiting on our 601 | // ICMP socket. It just redirects the call to Objective-C code. 602 | PingFoundation * obj; 603 | 604 | obj = (__bridge PingFoundation *) info; 605 | 606 | [obj readData]; 607 | } 608 | 609 | /*! Starts the send and receive infrastructure. 610 | * \details This is called once we've successfully resolved `hostName` in to 611 | * `hostAddress`. It's responsible for setting up the socket for sending and 612 | * receiving pings. 613 | */ 614 | 615 | - (void)startWithHostAddress 616 | { 617 | if (self.hostAddress == nil) 618 | { 619 | return; 620 | } 621 | 622 | int err; 623 | int fd; 624 | 625 | // Open the socket. 626 | 627 | fd = -1; 628 | err = 0; 629 | switch (self.hostAddressFamily) { 630 | // gzw here to decide what to use! see what is going on in iOS12! TODO: 631 | case AF_INET: { 632 | fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); 633 | if (fd < 0) { 634 | err = errno; 635 | } 636 | } break; 637 | case AF_INET6: { 638 | fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6); 639 | if (fd < 0) { 640 | err = errno; 641 | } 642 | } break; 643 | default: { 644 | err = EPROTONOSUPPORT; 645 | } break; 646 | } 647 | 648 | if (err != 0) { 649 | [self didFailWithError:[NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil]]; 650 | } else { 651 | CFSocketContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 652 | CFRunLoopSourceRef rls; 653 | id strongDelegate; 654 | 655 | // Wrap it in a CFSocket and schedule it on the runloop. 656 | 657 | self.socket = (CFSocketRef) CFAutorelease( CFSocketCreateWithNative(NULL, fd, kCFSocketReadCallBack, SocketReadCallback, &context) ); 658 | 659 | // The socket will now take care of cleaning up our file descriptor. 660 | 661 | rls = CFSocketCreateRunLoopSource(NULL, self.socket, 0); 662 | 663 | CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode); 664 | 665 | CFRelease(rls); 666 | 667 | strongDelegate = self.delegate; 668 | if ( (strongDelegate != nil) && [strongDelegate respondsToSelector:@selector(pingFoundation:didStartWithAddress:)] ) { 669 | [strongDelegate pingFoundation:self didStartWithAddress:self.hostAddress]; 670 | } 671 | } 672 | } 673 | 674 | /*! Processes the results of our name-to-address resolution. 675 | * \details Called by our CFHost resolution callback (HostResolveCallback) when host 676 | * resolution is complete. We just latch the first appropriate address and kick 677 | * off the send and receive infrastructure. 678 | */ 679 | 680 | - (void)hostResolutionDone { 681 | Boolean resolved; 682 | NSArray * addresses; 683 | 684 | // Find the first appropriate address. 685 | 686 | addresses = (__bridge NSArray *) CFHostGetAddressing(self.host, &resolved); 687 | if ( resolved && (addresses != nil) ) { 688 | resolved = false; 689 | for (NSData * address in addresses) { 690 | const struct sockaddr * addrPtr; 691 | 692 | addrPtr = (const struct sockaddr *) address.bytes; 693 | if ( address.length >= sizeof(struct sockaddr) ) { 694 | switch (addrPtr->sa_family) { 695 | case AF_INET: { 696 | if (self.addressStyle != PingFoundationAddressStyleICMPv6) { 697 | self.hostAddress = address; 698 | resolved = true; 699 | } 700 | } break; 701 | case AF_INET6: { 702 | if (self.addressStyle != PingFoundationAddressStyleICMPv4) { 703 | self.hostAddress = address; 704 | resolved = true; 705 | } 706 | } break; 707 | } 708 | } 709 | if (resolved) { 710 | break; 711 | } 712 | } 713 | } 714 | 715 | // We're done resolving, so shut that down. 716 | 717 | [self stopHostResolution]; 718 | 719 | // If all is OK, start the send and receive infrastructure, otherwise stop. 720 | 721 | if (resolved) { 722 | [self startWithHostAddress]; 723 | } else { 724 | [self didFailWithError:[NSError errorWithDomain:(NSString *)kCFErrorDomainCFNetwork code:kCFHostErrorHostNotFound userInfo:nil]]; 725 | } 726 | } 727 | 728 | /*! The callback for our CFHost object. 729 | * \details This simply routes the call to our `-hostResolutionDone` or 730 | * `-didFailWithHostStreamError:` methods. 731 | * \param theHost See the documentation for CFHostClientCallBack. 732 | * \param typeInfo See the documentation for CFHostClientCallBack. 733 | * \param error See the documentation for CFHostClientCallBack. 734 | * \param info See the documentation for CFHostClientCallBack; this is actually a pointer to 735 | * the 'owning' object. 736 | */ 737 | 738 | static void HostResolveCallback(CFHostRef theHost, CFHostInfoType typeInfo, const CFStreamError *error, void *info) { 739 | // This C routine is called by CFHost when the host resolution is complete. 740 | // It just redirects the call to the appropriate Objective-C method. 741 | PingFoundation * obj; 742 | 743 | obj = (__bridge PingFoundation *) info; 744 | 745 | if (([obj isKindOfClass:[PingFoundation class]] && typeInfo == kCFHostAddresses)) 746 | { 747 | if (theHost != obj->_host) 748 | { 749 | // unmatched, do nothing. 750 | return; 751 | } 752 | 753 | if ((error != NULL) && (error->domain != 0)) 754 | { 755 | [obj didFailWithHostStreamError:*error]; 756 | } 757 | else 758 | { 759 | [obj hostResolutionDone]; 760 | } 761 | } 762 | } 763 | 764 | - (void)start 765 | { 766 | // If the user supplied us with an address, just start pinging that. Otherwise 767 | // start a host resolution. 768 | 769 | Boolean success; 770 | CFHostClientContext context = {0, (__bridge void *)(self), NULL, NULL, NULL}; 771 | CFStreamError streamError; 772 | 773 | self.host = (CFHostRef)CFAutorelease(CFHostCreateWithName(NULL, (__bridge CFStringRef) self.hostName)); 774 | 775 | if (self.host == NULL) 776 | { 777 | // host NULL; do nothing. 778 | return; 779 | } 780 | 781 | CFHostSetClient(self.host, HostResolveCallback, &context); 782 | CFHostScheduleWithRunLoop(self.host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 783 | success = CFHostStartInfoResolution(self.host, kCFHostAddresses, &streamError); 784 | if (!success) 785 | { 786 | [self didFailWithHostStreamError:streamError]; 787 | } 788 | } 789 | 790 | /*! Stops the name-to-address resolution infrastructure. 791 | */ 792 | 793 | - (void)stopHostResolution { 794 | // Shut down the CFHost. 795 | if (self.host != NULL) { 796 | CFHostSetClient(self.host, NULL, NULL); 797 | CFHostUnscheduleFromRunLoop(self.host, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 798 | self.host = NULL; 799 | } 800 | } 801 | 802 | /*! Stops the send and receive infrastructure. 803 | */ 804 | 805 | - (void)stopSocket { 806 | if (self.socket != NULL) { 807 | CFSocketInvalidate(self.socket); 808 | self.socket = NULL; 809 | } 810 | } 811 | 812 | - (void)stop { 813 | [self stopHostResolution]; 814 | [self stopSocket]; 815 | 816 | // Junk the host address on stop. If the client calls -start again, we'll 817 | // re-resolve the host name. 818 | 819 | self.hostAddress = NULL; 820 | } 821 | 822 | @end 823 | -------------------------------------------------------------------------------- /RealReachability/Ping/PingHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // PingHelper.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface PingHelper : NSObject 12 | 13 | /// You MUST have already set the host before your ping action. 14 | /// Think about that: if you never set this, we don't know where to ping. 15 | @property (nonatomic, copy) NSString *host; 16 | 17 | /// Used as a backup for double checking. 18 | @property (nonatomic, copy) NSString *hostForCheck; 19 | 20 | /// Ping timeout. Default is 2 seconds 21 | @property (nonatomic, assign) NSTimeInterval timeout; 22 | 23 | /** 24 | * trigger a ping action with a completion block 25 | * 26 | * @param completion : Async completion block 27 | */ 28 | - (void)pingWithBlock:(void (^)(BOOL isSuccess, NSTimeInterval latency))completion; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /RealReachability/Ping/PingHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // PingHelper.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/19. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "PingHelper.h" 10 | #import "PingFoundation.h" 11 | 12 | #if (!defined(DEBUG)) 13 | #define NSLog(...) 14 | #endif 15 | 16 | @interface PingHelper() 17 | 18 | @property (nonatomic, strong) NSMutableArray *completionBlocks; 19 | @property(nonatomic, strong) PingFoundation *pingFoundation; 20 | @property (nonatomic, assign) BOOL isPinging; 21 | @property (nonatomic, assign) CFAbsoluteTime pingStartTime; 22 | 23 | @end 24 | 25 | @implementation PingHelper 26 | 27 | #pragma mark - Life Circle 28 | 29 | - (id)init 30 | { 31 | if ((self = [super init])) 32 | { 33 | _isPinging = NO; 34 | _timeout = 2.0f; 35 | _completionBlocks = [NSMutableArray array]; 36 | } 37 | return self; 38 | } 39 | 40 | - (void)dealloc 41 | { 42 | [self.completionBlocks removeAllObjects]; 43 | self.completionBlocks = nil; 44 | 45 | [self clearPingFoundation]; 46 | } 47 | 48 | #pragma mark - actions 49 | 50 | - (void)pingWithBlock:(void (^)(BOOL isSuccess, NSTimeInterval latency))completion 51 | { 52 | //NSLog(@"pingWithBlock"); 53 | if (completion) 54 | { 55 | // copy the block, then added to the blocks array. 56 | @synchronized(self) 57 | { 58 | [self.completionBlocks addObject:[completion copy]]; 59 | } 60 | } 61 | 62 | if (!self.isPinging) 63 | { 64 | // MUST make sure pingFoundation in mainThread 65 | __weak __typeof(self)weakSelf = self; 66 | if (![[NSThread currentThread] isMainThread]) { 67 | dispatch_sync(dispatch_get_main_queue(), ^{ 68 | __strong __typeof(weakSelf)strongSelf = weakSelf; 69 | [strongSelf startPing]; 70 | }); 71 | } 72 | else 73 | { 74 | [self startPing]; 75 | } 76 | } 77 | } 78 | 79 | - (void)clearPingFoundation 80 | { 81 | //NSLog(@"clearPingFoundation"); 82 | 83 | if (self.pingFoundation) 84 | { 85 | [self.pingFoundation stop]; 86 | self.pingFoundation.delegate = nil; 87 | self.pingFoundation = nil; 88 | } 89 | } 90 | 91 | - (void)startPing 92 | { 93 | //NSLog(@"startPing"); 94 | [self clearPingFoundation]; 95 | 96 | self.isPinging = YES; 97 | 98 | self.pingStartTime = CFAbsoluteTimeGetCurrent(); 99 | 100 | 101 | self.pingFoundation = [[PingFoundation alloc] initWithHostName:self.host]; 102 | self.pingFoundation.delegate = self; 103 | [self.pingFoundation start]; 104 | 105 | [self performSelector:@selector(pingTimeOut) withObject:nil afterDelay:self.timeout]; 106 | } 107 | 108 | - (void)setHost:(NSString *)host 109 | { 110 | _host = nil; 111 | _host = [host copy]; 112 | 113 | self.pingFoundation.delegate = nil; 114 | self.pingFoundation = nil; 115 | 116 | self.pingFoundation = [[PingFoundation alloc] initWithHostName:_host]; 117 | 118 | self.pingFoundation.delegate = self; 119 | } 120 | 121 | #pragma mark - inner methods 122 | 123 | - (void)doubleCheck 124 | { 125 | [self clearPingFoundation]; 126 | 127 | self.isPinging = YES; 128 | 129 | self.pingFoundation = [[PingFoundation alloc] initWithHostName:self.hostForCheck]; 130 | self.pingFoundation.delegate = self; 131 | [self.pingFoundation start]; 132 | 133 | } 134 | 135 | - (void)endWithFlag:(BOOL)isSuccess 136 | { 137 | [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(pingTimeOut) object:nil]; 138 | 139 | if (!self.isPinging) 140 | { 141 | return; 142 | } 143 | 144 | self.isPinging = NO; 145 | 146 | CFAbsoluteTime end = CFAbsoluteTimeGetCurrent(); 147 | NSTimeInterval latency = isSuccess ? (end - self.pingStartTime) * 1000 : 0; 148 | 149 | [self clearPingFoundation]; 150 | 151 | @synchronized(self) 152 | { 153 | for (void (^completion)(BOOL, NSTimeInterval) in self.completionBlocks) 154 | { 155 | completion(isSuccess, latency); 156 | } 157 | [self.completionBlocks removeAllObjects]; 158 | } 159 | } 160 | 161 | #pragma mark - PingFoundation delegate 162 | 163 | // When the pinger starts, send the ping immediately 164 | - (void)pingFoundation:(PingFoundation *)pinger didStartWithAddress:(NSData *)address 165 | { 166 | //NSLog(@"didStartWithAddress"); 167 | [self.pingFoundation sendPingWithData:nil]; 168 | } 169 | 170 | - (void)pingFoundation:(PingFoundation *)pinger didFailWithError:(NSError *)error 171 | { 172 | //NSLog(@"didFailWithError, error=%@", error); 173 | [self endWithFlag:NO]; 174 | } 175 | 176 | - (void)pingFoundation:(PingFoundation *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error 177 | { 178 | //NSLog(@"didFailToSendPacket, sequenceNumber = %@, error=%@", @(sequenceNumber), error); 179 | [self endWithFlag:NO]; 180 | } 181 | 182 | - (void)pingFoundation:(PingFoundation *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber 183 | { 184 | //NSLog(@"didReceivePingResponsePacket, sequenceNumber = %@", @(sequenceNumber)); 185 | [self endWithFlag:YES]; 186 | } 187 | 188 | #pragma mark - TimeOut handler 189 | 190 | - (void)pingTimeOut 191 | { 192 | if (!self.isPinging) 193 | { 194 | return; 195 | } 196 | 197 | self.isPinging = NO; 198 | [self clearPingFoundation]; 199 | 200 | @synchronized(self) 201 | { 202 | for (void (^completion)(BOOL, NSTimeInterval) in self.completionBlocks) 203 | { 204 | completion(NO, self.timeout); 205 | } 206 | [self.completionBlocks removeAllObjects]; 207 | } 208 | } 209 | 210 | @end 211 | -------------------------------------------------------------------------------- /RealReachability/Ping/README.md: -------------------------------------------------------------------------------- 1 | # SimplePing by apple(That is where PingFoundation modified from!!!Many Thanks to APPLE!) 2 | 3 | 1.4 4 | 5 | SimplePing demonstrates ping (ICMP) send and receive. 6 | 7 | ## Requirements 8 | 9 | ### Build 10 | 11 | Xcode 7.3 12 | 13 | The sample was built using Xcode 7.2.1 on OS X 10.11.2 with the OS X 10.11 SDK. You should be able to just open the project, select the *MacTool* scheme, and choose *Product* > *Build*. 14 | 15 | ### Runtime 16 | 17 | 10.11 18 | 19 | Although the sample requires 10.11, the core code works just fine on all versions of iOS and the underlying approach works on earlier versions of OS X (back to 10.2). 20 | 21 | ## Packing List 22 | 23 | The sample contains the following items: 24 | 25 | * `README.md` — This file. 26 | 27 | * `LICENSE.txt` — The standard sample code license. 28 | 29 | * `SimplePing.xcodeproj` — An Xcode project for the program. 30 | 31 | * `Common` — A directory containing common code, namely, the SimplePing class, an object wrapper around the low-level BSD Sockets ping function. 32 | 33 | * `MacTool` — A directory containing a single file, `main.m`, which is a Mac command line tool to exercise the SimplePing class. 34 | 35 | * `iOSApp` — A directory containing a trivial iOS app to test the SimplePing class. Most of it is boilerplate; the only interesting code is in `MainViewController.swift`, which is the iOS analogue of `main.m`. 36 | 37 | ## Using the Sample 38 | 39 | Once you’ve built the SimplePing tool you can test it by running it from Terminal with the address of a host to ping. For example: 40 | 41 | $ ./SimplePing www.apple.com 42 | 2016-02-29 10:28:03.589 SimplePing[58314:6905268] pinging 23.53.214.138 43 | 2016-02-29 10:28:03.590 SimplePing[58314:6905268] #0 sent 44 | 2016-02-29 10:28:03.626 SimplePing[58314:6905268] #0 received, size=64 45 | 2016-02-29 10:28:04.592 SimplePing[58314:6905268] #1 sent 46 | 2016-02-29 10:28:04.630 SimplePing[58314:6905268] #1 received, size=64 47 | ^C 48 | 49 | The program will keep pinging until you stop it with ^C. 50 | 51 | ## How it Works 52 | 53 | On most platforms ping requires privileges (it’s implemented with a raw IP socket). Apple platforms include a special facility that allows you to ping without privileges. Specifically, you can open a special, non-privileged ICMP socket that allows you to send and receive pings. Look at the `-[SimplePing startWithHostAddress]` method for the details. 54 | 55 | Beyond that, SimplePing is a very simply application of CFHost (for name-to-address resolution) and CFSocket (for integrating the ICMP socket into the runloop). 56 | 57 | ## Using the SimplePing Class 58 | 59 | To use the SimplePing class in your app, add `SimplePing.h` and `SimplePing.m` to your project. 60 | 61 | **Note** If you’re developing solely in Swift, Xcode will prompt you as to whether you want to create a bridging header or not. Agree to that by clicking Create Bridging Header. 62 | 63 | If you want to call SimplePing from your Swift code, add the following line to your bridging header. 64 | 65 | #include "SimplePing.h" 66 | 67 | Alternatively, if you’re using Objective-C, add the same line at the start of your `.m` file. 68 | 69 | Finally, to use the SimplePing class: 70 | 71 | 1. create an instance of the SimplePing class and keep a reference to that instance 72 | 73 | 2. set the `delegate` property 74 | 75 | 3. call `start()`. 76 | 77 | 4. if things go well, your delegate’s `simplePing(_:didStartWithAddress:)` method will be called; to send a ping, call `sendPingWithData(_:)` 78 | 79 | 5. when SimplePing receives an ICMP packet, it will call the `simplePing(_:didReceivePingResponsePacket:sequenceNumber:)` or `simplePing(:didReceiveUnexpectedPacket:)` delegate method 80 | 81 | SimplePing can be used from any thread but the use of any single instance must be confined to a specific thread. Moreover, that thread must run its run loop. In most cases it’s best to use SimplePing from the main thread. 82 | 83 | The SimplePing class has lots of header docs that explain it’s features in more depth. And if you’re looking for an example, there’s both Objective-C (`MacTool/main.m`) and Swift (`iOSApp/MainViewController.swift`) ones available. 84 | 85 | ## Caveats 86 | 87 | Much of what SimplePing does with CFSocket can also be done with GCD. Specifically, GCD makes it easy to integrate a BSD Socket into a typical run loop based program. This sample uses CFSocket because a) there are other examples of using GCD for socket-based networking, b) there are cases where CFSocket has advantages over GCD (for example, if you want to target a specific run loop, or a specific run loop mode), and c) at the time the sample was originally created, CFSocket was more commonly used. 88 | 89 | ## Feedback 90 | 91 | If you find any problems with this sample, or you’d like to suggest improvements, please [file a bug][bug] against it. 92 | 93 | [bug]: 94 | 95 | ## Version History 96 | 97 | 1.0.1 (Oct 2003) was the first shipping version. 98 | 99 | 1.1 (Feb 2010) was a complete rewrite with the twin goals of tidying the code and making the core code portable to iOS. 100 | 101 | 1.2 (Mar 2010) fixed a trivial problem that was causing the SimplePing module to not compile for iOS out of the box. 102 | 103 | 1.3 (Jul 2012) was a minor update to adopt the latest tools and techniques (most notably, ARC). 104 | 105 | 1.4 (Feb 2016) added support for IPv6. There were many other editorial changes that don’t don’t impact on the core functionality of the sample. 106 | 107 | Share and Enjoy 108 | 109 | Apple Developer Technical Support
110 | Core OS/Hardware 111 | 112 | 18 Apr 2016 113 | 114 | Copyright (C) 2016 Apple Inc. All rights reserved. 115 | -------------------------------------------------------------------------------- /RealReachability/RealReachability.h: -------------------------------------------------------------------------------- 1 | // 2 | // RealReachability.h 3 | // Version 1.3.0 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright (c) 2016 Dustturtle. All rights reserved. 7 | // 8 | // This code is distributed under the terms and conditions of the MIT license. 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy 11 | // of this software and associated documentation files (the "Software"), to deal 12 | // in the Software without restriction, including without limitation the rights 13 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | // copies of the Software, and to permit persons to whom the Software is 15 | // furnished to do so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in 18 | // all copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 | // THE SOFTWARE. 27 | // 28 | 29 | #import 30 | #import "LocalConnection.h" 31 | 32 | #define GLobalRealReachability [RealReachability sharedInstance] 33 | 34 | ///This notification was called only when reachability really changed; 35 | ///We use FSM to promise this for you; 36 | ///We post self to this notification, then you can invoke currentReachabilityStatus method to fetch current status. 37 | extern NSString *const kRealReachabilityChangedNotification; 38 | 39 | extern NSString *const kRRVPNStatusChangedNotification; 40 | 41 | typedef NS_ENUM(NSInteger, ReachabilityStatus) { 42 | ///Direct match with Apple networkStatus, just a force type convert. 43 | RealStatusUnknown = -1, 44 | RealStatusNotReachable = 0, 45 | RealStatusViaWWAN = 1, 46 | RealStatusViaWiFi = 2 47 | }; 48 | 49 | typedef NS_ENUM(NSInteger, WWANAccessType) { 50 | WWANTypeUnknown = -1, /// maybe iOS6 51 | WWANType5G = 2, 52 | WWANType4G = 0, 53 | WWANType3G = 1, 54 | WWANType2G = 3 55 | }; 56 | 57 | @protocol RealReachabilityDelegate 58 | @optional 59 | /// TODO:通过挂载一个定制的代理请求来检查网络,需要用户自己实现,我们会给出一个示例。 60 | /// 可以通过这种方式规避解决http可用但icmp被阻止的场景下框架判断不正确的问题。 61 | /// (Update: 已经添加了判断VPN的相关逻辑,以解决这种场景下大概率误判的问题) 62 | /// 此方法阻塞?同步返回?还是异步?如果阻塞主线程超过n秒是不行的。 63 | /// 当CustomAgent的doubleCheck被启用时,ping的doubleCheck将不再工作。 64 | /// TODO: We introduce a custom agent to check the network by making http request, that need 65 | /// the user to achieve this. 66 | /// We want to solve the issue on special case(http available but icmp prevented). 67 | /// NOTE: When the double check of the custom agent was used, the double check by ping will work no longer. 68 | - (BOOL)doubleCheckByCustomAgent; 69 | @end 70 | 71 | @interface RealReachability : NSObject 72 | 73 | // local connection observer 74 | @property (nonatomic, strong) LocalConnection *localObserver; 75 | 76 | /// Please make sure this host is available for pinging! default host:www.apple.com 77 | @property (nonatomic, copy) NSString *hostForPing; 78 | 79 | @property (nonatomic, copy) NSString *hostForCheck; 80 | 81 | /// Interval in minutes; default is 2.0f, suggest value from 0.3f to 60.0f; 82 | /// If exceeded, the value will be reset to 0.3f or 60.0f (the closer one). 83 | @property (nonatomic, assign) float autoCheckInterval; 84 | 85 | // Timeout used for ping. Default is 2 seconds 86 | @property (nonatomic, assign) NSTimeInterval pingTimeout; 87 | 88 | // Latency from latest ping result 89 | @property (nonatomic, assign) NSTimeInterval latency; 90 | 91 | + (instancetype)sharedInstance; 92 | 93 | - (void)startNotifier; 94 | 95 | - (void)stopNotifier; 96 | 97 | /** 98 | * To get real reachability we need to do async request, 99 | * then we use the block blow for invoker to handle business request(need real reachability). 100 | * Now we have introduced a double check to make our result more reliable. 101 | * 102 | * @param asyncHandler async request handler, return in 5 seconds(max limit). 103 | * The limit time may be adjusted later for better experience. 104 | */ 105 | - (void)reachabilityWithBlock:(void (^)(ReachabilityStatus status))asyncHandler; 106 | 107 | /** 108 | * Return current reachability immediately. 109 | * 110 | * @return see enum LocalConnectionStatus 111 | */ 112 | - (ReachabilityStatus)currentReachabilityStatus; 113 | 114 | /** 115 | * Return previous reachability status. 116 | * 117 | * @return see enum LocalConnectionStatus 118 | */ 119 | - (ReachabilityStatus)previousReachabilityStatus; 120 | 121 | /** 122 | * Return current WWAN type immediately. 123 | * 124 | * @return unknown/5g/4g/3g/2g. 125 | * 126 | * This method can be used to improve app's further network performance 127 | * (different strategies for different WWAN types). 128 | */ 129 | - (WWANAccessType)currentWWANtype; 130 | 131 | /** 132 | * Sometimes people use VPN on the device. 133 | * In this situation we need to ignore the ping error. 134 | * (VPN usually do not support ICMP.) 135 | * 136 | * @return current VPN status: YES->ON, NO->OFF. 137 | * 138 | * This method can be used to improve app's further network performance 139 | * (different strategies for different WWAN types). 140 | */ 141 | - (BOOL)isVPNOn; 142 | 143 | @end 144 | -------------------------------------------------------------------------------- /RealReachability/RealReachability.m: -------------------------------------------------------------------------------- 1 | // 2 | // RealReachability.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #include 10 | 11 | #import "RealReachability.h" 12 | #import "FSMEngine.h" 13 | #import "PingHelper.h" 14 | #import 15 | #import 16 | 17 | #if (!defined(DEBUG)) 18 | #define NSLog(...) 19 | #endif 20 | 21 | #define kDefaultHost @"www.apple.com" 22 | #define kDefaultCheckInterval 2.0f 23 | #define kDefaultPingTimeout 2.0f 24 | 25 | #define kMinAutoCheckInterval 0.3f 26 | #define kMaxAutoCheckInterval 60.0f 27 | 28 | NSString *const kRealReachabilityChangedNotification = @"kRealReachabilityChangedNotification"; 29 | 30 | NSString *const kRRVPNStatusChangedNotification = @"kRRVPNStatusChangedNotification"; 31 | 32 | @interface RealReachability() 33 | { 34 | BOOL _vpnFlag; 35 | } 36 | 37 | @property (nonatomic, strong) FSMEngine *engine; 38 | @property (nonatomic, assign) BOOL isNotifying; 39 | 40 | @property (nonatomic,strong) NSArray *typeStrings5G; 41 | @property (nonatomic,strong) NSArray *typeStrings4G; 42 | @property (nonatomic,strong) NSArray *typeStrings3G; 43 | @property (nonatomic,strong) NSArray *typeStrings2G; 44 | 45 | @property (nonatomic, assign) ReachabilityStatus previousStatus; 46 | 47 | /// main helper 48 | @property (nonatomic, strong) PingHelper *pingHelper; 49 | 50 | /// for double check 51 | @property (nonatomic, strong) PingHelper *pingChecker; 52 | 53 | @end 54 | 55 | @implementation RealReachability 56 | 57 | #pragma mark - Life Circle 58 | 59 | - (id)init 60 | { 61 | if ((self = [super init])) 62 | { 63 | _engine = [[FSMEngine alloc] init]; 64 | [_engine start]; 65 | 66 | _typeStrings2G = @[CTRadioAccessTechnologyEdge, 67 | CTRadioAccessTechnologyGPRS, 68 | CTRadioAccessTechnologyCDMA1x]; 69 | 70 | _typeStrings3G = @[CTRadioAccessTechnologyHSDPA, 71 | CTRadioAccessTechnologyWCDMA, 72 | CTRadioAccessTechnologyHSUPA, 73 | CTRadioAccessTechnologyCDMAEVDORev0, 74 | CTRadioAccessTechnologyCDMAEVDORevA, 75 | CTRadioAccessTechnologyCDMAEVDORevB, 76 | CTRadioAccessTechnologyeHRPD]; 77 | 78 | _typeStrings4G = @[CTRadioAccessTechnologyLTE]; 79 | 80 | _typeStrings5G = @[@"CTRadioAccessTechnologyNR", @"CTRadioAccessTechnologyNRNSA"]; 81 | 82 | _hostForPing = kDefaultHost; 83 | _hostForCheck = kDefaultHost; 84 | _autoCheckInterval = kDefaultCheckInterval; 85 | _pingTimeout = kDefaultPingTimeout; 86 | 87 | _vpnFlag = NO; 88 | 89 | [[NSNotificationCenter defaultCenter] addObserver:self 90 | selector:@selector(appBecomeActive) 91 | name:UIApplicationDidBecomeActiveNotification 92 | object:nil]; 93 | 94 | _localObserver = [[LocalConnection alloc] init]; 95 | _pingHelper = [[PingHelper alloc] init]; 96 | _pingChecker = [[PingHelper alloc] init]; 97 | } 98 | return self; 99 | } 100 | 101 | - (void)dealloc 102 | { 103 | [[NSNotificationCenter defaultCenter] removeObserver:self]; 104 | 105 | self.engine = nil; 106 | 107 | [self.localObserver stopNotifier]; 108 | _localObserver = nil; 109 | } 110 | 111 | #pragma mark - Handle system event 112 | 113 | - (void)appBecomeActive 114 | { 115 | if (self.isNotifying) 116 | { 117 | [self reachabilityWithBlock:nil]; 118 | } 119 | } 120 | 121 | #pragma mark - Singlton Method 122 | 123 | + (instancetype)sharedInstance 124 | { 125 | static id sharedRRInstance = nil; 126 | static dispatch_once_t onceToken; 127 | dispatch_once(&onceToken, ^{ 128 | sharedRRInstance = [[self alloc] init]; 129 | }); 130 | 131 | return sharedRRInstance; 132 | } 133 | 134 | #pragma mark - actions 135 | 136 | - (void)startNotifier 137 | { 138 | if (self.isNotifying) 139 | { 140 | // avoid duplicate action 141 | return; 142 | } 143 | 144 | self.isNotifying = YES; 145 | self.previousStatus = RealStatusUnknown; 146 | 147 | NSDictionary *inputDic = @{kEventKeyID:@(RREventLoad)}; 148 | [self.engine receiveInput:inputDic]; 149 | 150 | [self.localObserver startNotifier]; 151 | [[NSNotificationCenter defaultCenter] addObserver:self 152 | selector:@selector(localConnectionHandler:) 153 | name:kLocalConnectionChangedNotification 154 | object:nil]; 155 | [[NSNotificationCenter defaultCenter] addObserver:self 156 | selector:@selector(localConnectionHandler:) 157 | name:kLocalConnectionInitializedNotification 158 | object:nil]; 159 | 160 | self.pingHelper.host = _hostForPing; 161 | self.pingHelper.timeout = self.pingTimeout; 162 | 163 | self.pingChecker.host = _hostForCheck; 164 | self.pingChecker.timeout = self.pingTimeout; 165 | 166 | [self autoCheckReachability]; 167 | } 168 | 169 | - (void)stopNotifier 170 | { 171 | if (!self.isNotifying) 172 | { 173 | // avoid duplicate action 174 | return; 175 | } 176 | 177 | [[NSNotificationCenter defaultCenter] removeObserver:self 178 | name:kLocalConnectionChangedNotification 179 | object:nil]; 180 | [[NSNotificationCenter defaultCenter] removeObserver:self 181 | name:kLocalConnectionInitializedNotification 182 | object:nil]; 183 | 184 | NSDictionary *inputDic = @{kEventKeyID:@(RREventUnLoad)}; 185 | [self.engine receiveInput:inputDic]; 186 | 187 | [self.localObserver stopNotifier]; 188 | 189 | self.isNotifying = NO; 190 | } 191 | 192 | #pragma mark - outside invoke 193 | 194 | - (void)reachabilityWithBlock:(void (^)(ReachabilityStatus status))asyncHandler 195 | { 196 | // logic optimization: no need to ping when Local connection unavailable! 197 | if ([self.localObserver currentLocalConnectionStatus] == LC_UnReachable) 198 | { 199 | if (asyncHandler != nil) 200 | { 201 | asyncHandler(RealStatusNotReachable); 202 | } 203 | return; 204 | } 205 | 206 | // special case, VPN on; just skipping (ICMP not working now). 207 | if ([self isVPNOn]) 208 | { 209 | ReachabilityStatus status = [self currentReachabilityStatus]; 210 | if (asyncHandler != nil) 211 | { 212 | asyncHandler(status); 213 | } 214 | return; 215 | } 216 | 217 | __weak __typeof(self)weakSelf = self; 218 | [self.pingHelper pingWithBlock:^(BOOL isSuccess, NSTimeInterval latency) 219 | { 220 | __strong __typeof(weakSelf)strongSelf = weakSelf; 221 | strongSelf.latency = latency; 222 | if (isSuccess) 223 | { 224 | ReachabilityStatus status = [self currentReachabilityStatus]; 225 | 226 | // Post the notification if the state changed here. 227 | NSDictionary *inputDic = @{kEventKeyID:@(RREventPingCallback), kEventKeyParam:@(YES)}; 228 | NSInteger rtn = [strongSelf.engine receiveInput:inputDic]; 229 | if (rtn == 0) // state changed & state available, post notification. 230 | { 231 | if ([strongSelf.engine isCurrentStateAvailable]) 232 | { 233 | strongSelf.previousStatus = status; 234 | __weak __typeof(self)weakSelf = strongSelf; 235 | dispatch_async(dispatch_get_main_queue(), ^{ 236 | __strong __typeof(weakSelf)strongSelf = weakSelf; 237 | [[NSNotificationCenter defaultCenter] postNotificationName:kRealReachabilityChangedNotification 238 | object:strongSelf]; 239 | }); 240 | } 241 | } 242 | 243 | if (asyncHandler != nil) 244 | { 245 | ReachabilityStatus currentStatus = [strongSelf currentReachabilityStatus]; 246 | asyncHandler(currentStatus); 247 | } 248 | } 249 | else 250 | { 251 | if ([self isVPNOn]) 252 | { 253 | // special case, VPN connected. Just ignore the ping result. 254 | } 255 | else 256 | { 257 | // delay 1 seconds, then make a double check. 258 | dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)); 259 | __weak __typeof(self)weakSelf = self; 260 | dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 261 | __strong __typeof(weakSelf)self = weakSelf; 262 | [self makeDoubleCheck:asyncHandler]; 263 | }); 264 | } 265 | } 266 | }]; 267 | } 268 | 269 | - (ReachabilityStatus)currentReachabilityStatus 270 | { 271 | RRStateID currentID = self.engine.currentStateID; 272 | 273 | switch (currentID) 274 | { 275 | case RRStateUnReachable: 276 | { 277 | return RealStatusNotReachable; 278 | } 279 | case RRStateWIFI: 280 | { 281 | return RealStatusViaWiFi; 282 | } 283 | case RRStateWWAN: 284 | { 285 | return RealStatusViaWWAN; 286 | } 287 | case RRStateLoading: 288 | { 289 | // status on loading, return local status temporary. 290 | return (ReachabilityStatus)(self.localObserver.currentLocalConnectionStatus); 291 | } 292 | 293 | default: 294 | { 295 | NSLog(@"No normal status matched, return unreachable temporary"); 296 | return RealStatusNotReachable; 297 | } 298 | } 299 | } 300 | 301 | - (ReachabilityStatus)previousReachabilityStatus 302 | { 303 | return self.previousStatus; 304 | } 305 | 306 | - (void)setHostForPing:(NSString *)hostForPing 307 | { 308 | _hostForPing = nil; 309 | _hostForPing = [hostForPing copy]; 310 | 311 | self.pingHelper.host = _hostForPing; 312 | } 313 | 314 | - (void)setHostForCheck:(NSString *)hostForCheck 315 | { 316 | _hostForCheck = nil; 317 | _hostForCheck = [hostForCheck copy]; 318 | 319 | self.pingChecker.host = _hostForCheck; 320 | } 321 | 322 | - (void)setPingTimeout:(NSTimeInterval)pingTimeout 323 | { 324 | _pingTimeout = pingTimeout; 325 | self.pingHelper.timeout = pingTimeout; 326 | self.pingChecker.timeout = pingTimeout; 327 | } 328 | 329 | - (WWANAccessType)currentWWANtype 330 | { 331 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) 332 | { 333 | CTTelephonyNetworkInfo *teleInfo = [[CTTelephonyNetworkInfo alloc] init]; 334 | NSString *accessString = @""; 335 | 336 | if (@available(iOS 12.0, *)) 337 | { 338 | // dual sim, maybe dual cards, simple choose one. 339 | NSDictionary *infoDic = teleInfo.serviceCurrentRadioAccessTechnology; 340 | NSArray *accessStrings = infoDic.allValues; 341 | if (accessStrings.count > 0) 342 | { 343 | accessString = accessStrings[0]; 344 | } 345 | } 346 | else 347 | { 348 | accessString = teleInfo.currentRadioAccessTechnology; 349 | } 350 | 351 | if ([accessString length] > 0) 352 | { 353 | return [self accessTypeForString:accessString]; 354 | } 355 | else 356 | { 357 | return WWANTypeUnknown; 358 | } 359 | } 360 | else 361 | { 362 | return WWANTypeUnknown; 363 | } 364 | } 365 | 366 | #pragma mark - inner methods 367 | - (void)makeDoubleCheck:(void (^)(ReachabilityStatus status))asyncHandler 368 | { 369 | __weak __typeof(self)weakSelf = self; 370 | [self.pingChecker pingWithBlock:^(BOOL isSuccess, NSTimeInterval latency) { 371 | __strong __typeof(weakSelf)strongSelf = weakSelf; 372 | strongSelf.latency = latency; 373 | ReachabilityStatus status = [strongSelf currentReachabilityStatus]; 374 | 375 | NSDictionary *inputDic = @{kEventKeyID:@(RREventPingCallback), kEventKeyParam:@(isSuccess)}; 376 | NSInteger rtn = [strongSelf.engine receiveInput:inputDic]; 377 | if (rtn == 0) // state changed & state available, post notification. 378 | { 379 | if ([strongSelf.engine isCurrentStateAvailable]) 380 | { 381 | strongSelf.previousStatus = status; 382 | __weak __typeof(self)weakSelf = strongSelf; 383 | dispatch_async(dispatch_get_main_queue(), ^{ 384 | __strong __typeof(weakSelf)strongSelf = weakSelf; 385 | [[NSNotificationCenter defaultCenter] postNotificationName:kRealReachabilityChangedNotification 386 | object:strongSelf]; 387 | }); 388 | } 389 | } 390 | 391 | if (asyncHandler != nil) 392 | { 393 | ReachabilityStatus currentStatus = [strongSelf currentReachabilityStatus]; 394 | asyncHandler(currentStatus); 395 | } 396 | }]; 397 | } 398 | 399 | - (NSString *)paramValueFromStatus:(LocalConnectionStatus)status 400 | { 401 | switch (status) 402 | { 403 | case LC_UnReachable: 404 | { 405 | return kParamValueUnReachable; 406 | } 407 | case LC_WiFi: 408 | { 409 | return kParamValueWIFI; 410 | } 411 | case LC_WWAN: 412 | { 413 | return kParamValueWWAN; 414 | } 415 | 416 | default: 417 | { 418 | NSLog(@"RealReachability error! paramValueFromStatus not matched!"); 419 | return @""; 420 | } 421 | } 422 | } 423 | 424 | // auto checking after every autoCheckInterval minutes 425 | - (void)autoCheckReachability 426 | { 427 | if (!self.isNotifying) 428 | { 429 | return; 430 | } 431 | 432 | if (self.autoCheckInterval < kMinAutoCheckInterval) 433 | { 434 | self.autoCheckInterval = kMinAutoCheckInterval; 435 | } 436 | 437 | if (self.autoCheckInterval > kMaxAutoCheckInterval) 438 | { 439 | self.autoCheckInterval = kMaxAutoCheckInterval; 440 | } 441 | 442 | 443 | dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.autoCheckInterval*60*NSEC_PER_SEC)); 444 | __weak __typeof(self)weakSelf = self; 445 | dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 446 | __strong __typeof(weakSelf)strongSelf = weakSelf; 447 | [strongSelf reachabilityWithBlock:nil]; 448 | [strongSelf autoCheckReachability]; 449 | }); 450 | } 451 | 452 | - (WWANAccessType)accessTypeForString:(NSString *)accessString 453 | { 454 | if ([self.typeStrings5G containsObject:accessString]) 455 | { 456 | return WWANType5G; 457 | } 458 | else if ([self.typeStrings4G containsObject:accessString]) 459 | { 460 | return WWANType4G; 461 | } 462 | else if ([self.typeStrings3G containsObject:accessString]) 463 | { 464 | return WWANType3G; 465 | } 466 | else if ([self.typeStrings2G containsObject:accessString]) 467 | { 468 | return WWANType2G; 469 | } 470 | else 471 | { 472 | return WWANTypeUnknown; 473 | } 474 | } 475 | 476 | #pragma mark - Notification observer 477 | - (void)localConnectionHandler:(NSNotification *)notification 478 | { 479 | LocalConnection *lc = (LocalConnection *)notification.object; 480 | LocalConnectionStatus lcStatus = [lc currentLocalConnectionStatus]; 481 | //NSLog(@"currentLocalConnectionStatus:%@, receive notification:%@",@(lcStatus), notification.name); 482 | ReachabilityStatus status = [self currentReachabilityStatus]; 483 | 484 | NSDictionary *inputDic = @{kEventKeyID:@(RREventLocalConnectionCallback), kEventKeyParam:[self paramValueFromStatus:lcStatus]}; 485 | NSInteger rtn = [self.engine receiveInput:inputDic]; 486 | 487 | if (rtn == 0) // state changed & state available, post notification. 488 | { 489 | if ([self.engine isCurrentStateAvailable]) 490 | { 491 | self.previousStatus = status; 492 | 493 | // already in main thread. 494 | if ([notification.name isEqualToString:kLocalConnectionChangedNotification]) 495 | { 496 | [[NSNotificationCenter defaultCenter] postNotificationName:kRealReachabilityChangedNotification 497 | object:self]; 498 | } 499 | 500 | if (lcStatus != LC_UnReachable) 501 | { 502 | // To make sure your reachability is "Real". 503 | [self reachabilityWithBlock:nil]; 504 | } 505 | } 506 | } 507 | } 508 | 509 | - (BOOL)isVPNOn 510 | { 511 | BOOL flag = NO; 512 | NSString *version = [UIDevice currentDevice].systemVersion; 513 | // need two ways to judge this. 514 | if (version.doubleValue >= 9.0) 515 | { 516 | NSDictionary *dict = CFBridgingRelease(CFNetworkCopySystemProxySettings()); 517 | NSArray *keys = [dict[@"__SCOPED__"] allKeys]; 518 | for (NSString *key in keys) { 519 | if ([key rangeOfString:@"tap"].location != NSNotFound || 520 | [key rangeOfString:@"tun"].location != NSNotFound || 521 | [key rangeOfString:@"ipsec"].location != NSNotFound || 522 | [key rangeOfString:@"ppp"].location != NSNotFound){ 523 | flag = YES; 524 | break; 525 | } 526 | } 527 | } 528 | else 529 | { 530 | struct ifaddrs *interfaces = NULL; 531 | struct ifaddrs *temp_addr = NULL; 532 | int success = 0; 533 | 534 | // retrieve the current interfaces - returns 0 on success 535 | success = getifaddrs(&interfaces); 536 | if (success == 0) 537 | { 538 | // Loop through linked list of interfaces 539 | temp_addr = interfaces; 540 | while (temp_addr != NULL) 541 | { 542 | NSString *string = [NSString stringWithFormat:@"%s" , temp_addr->ifa_name]; 543 | if ([string rangeOfString:@"tap"].location != NSNotFound || 544 | [string rangeOfString:@"tun"].location != NSNotFound || 545 | [string rangeOfString:@"ipsec"].location != NSNotFound || 546 | [string rangeOfString:@"ppp"].location != NSNotFound) 547 | { 548 | flag = YES; 549 | break; 550 | } 551 | temp_addr = temp_addr->ifa_next; 552 | } 553 | } 554 | 555 | // Free memory 556 | freeifaddrs(interfaces); 557 | } 558 | 559 | if (_vpnFlag != flag) 560 | { 561 | // reset flag 562 | _vpnFlag = flag; 563 | 564 | // post notification 565 | __weak __typeof(self)weakSelf = self; 566 | dispatch_async(dispatch_get_main_queue(), ^{ 567 | __strong __typeof(weakSelf)strongSelf = weakSelf; 568 | [[NSNotificationCenter defaultCenter] postNotificationName:kRRVPNStatusChangedNotification 569 | object:strongSelf]; 570 | }); 571 | } 572 | 573 | return flag; 574 | } 575 | 576 | @end 577 | 578 | -------------------------------------------------------------------------------- /testRealReachability.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 8EE0B6331C40F71900CBABCA /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EE0B6321C40F71900CBABCA /* main.m */; }; 11 | 8EE0B6361C40F71900CBABCA /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EE0B6351C40F71900CBABCA /* AppDelegate.m */; }; 12 | 8EE0B6391C40F71900CBABCA /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EE0B6381C40F71900CBABCA /* ViewController.m */; }; 13 | 8EE0B63C1C40F71900CBABCA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8EE0B63A1C40F71900CBABCA /* Main.storyboard */; }; 14 | 8EE0B63E1C40F71900CBABCA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8EE0B63D1C40F71900CBABCA /* Assets.xcassets */; }; 15 | 8EE0B6411C40F71900CBABCA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8EE0B63F1C40F71900CBABCA /* LaunchScreen.storyboard */; }; 16 | 8EF6153420CFEDD100D425CD /* NSObject+SimpleKVO.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EF6153320CFEDD100D425CD /* NSObject+SimpleKVO.m */; }; 17 | 8EFA08DA1C50E25800F6D790 /* LocalConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08C01C50E25800F6D790 /* LocalConnection.m */; }; 18 | 8EFA08DB1C50E25800F6D790 /* FSMEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08C41C50E25800F6D790 /* FSMEngine.m */; }; 19 | 8EFA08DC1C50E25800F6D790 /* FSMStateUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08C61C50E25800F6D790 /* FSMStateUtil.m */; }; 20 | 8EFA08DD1C50E25800F6D790 /* ReachState.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08C81C50E25800F6D790 /* ReachState.m */; }; 21 | 8EFA08DE1C50E25800F6D790 /* ReachStateLoading.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08CA1C50E25800F6D790 /* ReachStateLoading.m */; }; 22 | 8EFA08DF1C50E25800F6D790 /* ReachStateUnloaded.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08CC1C50E25800F6D790 /* ReachStateUnloaded.m */; }; 23 | 8EFA08E01C50E25800F6D790 /* ReachStateUnReachable.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08CE1C50E25800F6D790 /* ReachStateUnReachable.m */; }; 24 | 8EFA08E11C50E25800F6D790 /* ReachStateWIFI.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08D01C50E25800F6D790 /* ReachStateWIFI.m */; }; 25 | 8EFA08E21C50E25800F6D790 /* ReachStateWWAN.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08D21C50E25800F6D790 /* ReachStateWWAN.m */; }; 26 | 8EFA08E31C50E25800F6D790 /* PingFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08D51C50E25800F6D790 /* PingFoundation.m */; }; 27 | 8EFA08E41C50E25800F6D790 /* PingHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08D71C50E25800F6D790 /* PingHelper.m */; }; 28 | 8EFA08E51C50E25800F6D790 /* RealReachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 8EFA08D91C50E25800F6D790 /* RealReachability.m */; }; 29 | /* End PBXBuildFile section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | 8EE0B62E1C40F71900CBABCA /* testRealReachability.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = testRealReachability.app; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | 8EE0B6321C40F71900CBABCA /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 34 | 8EE0B6341C40F71900CBABCA /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 35 | 8EE0B6351C40F71900CBABCA /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 36 | 8EE0B6371C40F71900CBABCA /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 37 | 8EE0B6381C40F71900CBABCA /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 38 | 8EE0B63B1C40F71900CBABCA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 39 | 8EE0B63D1C40F71900CBABCA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 40 | 8EE0B6401C40F71900CBABCA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 41 | 8EE0B6421C40F71900CBABCA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 42 | 8EF6153220CFEDD100D425CD /* NSObject+SimpleKVO.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SimpleKVO.h"; sourceTree = ""; }; 43 | 8EF6153320CFEDD100D425CD /* NSObject+SimpleKVO.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SimpleKVO.m"; sourceTree = ""; }; 44 | 8EFA08BF1C50E25800F6D790 /* LocalConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocalConnection.h; sourceTree = ""; }; 45 | 8EFA08C01C50E25800F6D790 /* LocalConnection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocalConnection.m; sourceTree = ""; }; 46 | 8EFA08C21C50E25800F6D790 /* FSMDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSMDefines.h; sourceTree = ""; }; 47 | 8EFA08C31C50E25800F6D790 /* FSMEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSMEngine.h; sourceTree = ""; }; 48 | 8EFA08C41C50E25800F6D790 /* FSMEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSMEngine.m; sourceTree = ""; }; 49 | 8EFA08C51C50E25800F6D790 /* FSMStateUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSMStateUtil.h; sourceTree = ""; }; 50 | 8EFA08C61C50E25800F6D790 /* FSMStateUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSMStateUtil.m; sourceTree = ""; }; 51 | 8EFA08C71C50E25800F6D790 /* ReachState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachState.h; sourceTree = ""; }; 52 | 8EFA08C81C50E25800F6D790 /* ReachState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachState.m; sourceTree = ""; }; 53 | 8EFA08C91C50E25800F6D790 /* ReachStateLoading.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateLoading.h; sourceTree = ""; }; 54 | 8EFA08CA1C50E25800F6D790 /* ReachStateLoading.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateLoading.m; sourceTree = ""; }; 55 | 8EFA08CB1C50E25800F6D790 /* ReachStateUnloaded.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateUnloaded.h; sourceTree = ""; }; 56 | 8EFA08CC1C50E25800F6D790 /* ReachStateUnloaded.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateUnloaded.m; sourceTree = ""; }; 57 | 8EFA08CD1C50E25800F6D790 /* ReachStateUnReachable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateUnReachable.h; sourceTree = ""; }; 58 | 8EFA08CE1C50E25800F6D790 /* ReachStateUnReachable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateUnReachable.m; sourceTree = ""; }; 59 | 8EFA08CF1C50E25800F6D790 /* ReachStateWIFI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateWIFI.h; sourceTree = ""; }; 60 | 8EFA08D01C50E25800F6D790 /* ReachStateWIFI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateWIFI.m; sourceTree = ""; }; 61 | 8EFA08D11C50E25800F6D790 /* ReachStateWWAN.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReachStateWWAN.h; sourceTree = ""; }; 62 | 8EFA08D21C50E25800F6D790 /* ReachStateWWAN.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReachStateWWAN.m; sourceTree = ""; }; 63 | 8EFA08D41C50E25800F6D790 /* PingFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PingFoundation.h; sourceTree = ""; }; 64 | 8EFA08D51C50E25800F6D790 /* PingFoundation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PingFoundation.m; sourceTree = ""; }; 65 | 8EFA08D61C50E25800F6D790 /* PingHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PingHelper.h; sourceTree = ""; }; 66 | 8EFA08D71C50E25800F6D790 /* PingHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PingHelper.m; sourceTree = ""; }; 67 | 8EFA08D81C50E25800F6D790 /* RealReachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RealReachability.h; sourceTree = ""; }; 68 | 8EFA08D91C50E25800F6D790 /* RealReachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RealReachability.m; sourceTree = ""; }; 69 | /* End PBXFileReference section */ 70 | 71 | /* Begin PBXFrameworksBuildPhase section */ 72 | 8EE0B62B1C40F71800CBABCA /* Frameworks */ = { 73 | isa = PBXFrameworksBuildPhase; 74 | buildActionMask = 2147483647; 75 | files = ( 76 | ); 77 | runOnlyForDeploymentPostprocessing = 0; 78 | }; 79 | /* End PBXFrameworksBuildPhase section */ 80 | 81 | /* Begin PBXGroup section */ 82 | 8EE0B6251C40F71800CBABCA = { 83 | isa = PBXGroup; 84 | children = ( 85 | 8EE0B6301C40F71900CBABCA /* testRealReachability */, 86 | 8EE0B62F1C40F71900CBABCA /* Products */, 87 | ); 88 | sourceTree = ""; 89 | }; 90 | 8EE0B62F1C40F71900CBABCA /* Products */ = { 91 | isa = PBXGroup; 92 | children = ( 93 | 8EE0B62E1C40F71900CBABCA /* testRealReachability.app */, 94 | ); 95 | name = Products; 96 | sourceTree = ""; 97 | }; 98 | 8EE0B6301C40F71900CBABCA /* testRealReachability */ = { 99 | isa = PBXGroup; 100 | children = ( 101 | 8EF6153120CFEDD100D425CD /* KVO */, 102 | 8EFA08BD1C50E25800F6D790 /* RealReachability */, 103 | 8EE0B6341C40F71900CBABCA /* AppDelegate.h */, 104 | 8EE0B6351C40F71900CBABCA /* AppDelegate.m */, 105 | 8EE0B6371C40F71900CBABCA /* ViewController.h */, 106 | 8EE0B6381C40F71900CBABCA /* ViewController.m */, 107 | 8EE0B63A1C40F71900CBABCA /* Main.storyboard */, 108 | 8EE0B63D1C40F71900CBABCA /* Assets.xcassets */, 109 | 8EE0B63F1C40F71900CBABCA /* LaunchScreen.storyboard */, 110 | 8EE0B6421C40F71900CBABCA /* Info.plist */, 111 | 8EE0B6311C40F71900CBABCA /* Supporting Files */, 112 | ); 113 | path = testRealReachability; 114 | sourceTree = ""; 115 | }; 116 | 8EE0B6311C40F71900CBABCA /* Supporting Files */ = { 117 | isa = PBXGroup; 118 | children = ( 119 | 8EE0B6321C40F71900CBABCA /* main.m */, 120 | ); 121 | name = "Supporting Files"; 122 | sourceTree = ""; 123 | }; 124 | 8EF6153120CFEDD100D425CD /* KVO */ = { 125 | isa = PBXGroup; 126 | children = ( 127 | 8EF6153220CFEDD100D425CD /* NSObject+SimpleKVO.h */, 128 | 8EF6153320CFEDD100D425CD /* NSObject+SimpleKVO.m */, 129 | ); 130 | path = KVO; 131 | sourceTree = ""; 132 | }; 133 | 8EFA08BD1C50E25800F6D790 /* RealReachability */ = { 134 | isa = PBXGroup; 135 | children = ( 136 | 8EFA08BE1C50E25800F6D790 /* Connection */, 137 | 8EFA08C11C50E25800F6D790 /* FSM */, 138 | 8EFA08D31C50E25800F6D790 /* Ping */, 139 | 8EFA08D81C50E25800F6D790 /* RealReachability.h */, 140 | 8EFA08D91C50E25800F6D790 /* RealReachability.m */, 141 | ); 142 | path = RealReachability; 143 | sourceTree = SOURCE_ROOT; 144 | }; 145 | 8EFA08BE1C50E25800F6D790 /* Connection */ = { 146 | isa = PBXGroup; 147 | children = ( 148 | 8EFA08BF1C50E25800F6D790 /* LocalConnection.h */, 149 | 8EFA08C01C50E25800F6D790 /* LocalConnection.m */, 150 | ); 151 | path = Connection; 152 | sourceTree = ""; 153 | }; 154 | 8EFA08C11C50E25800F6D790 /* FSM */ = { 155 | isa = PBXGroup; 156 | children = ( 157 | 8EFA08C21C50E25800F6D790 /* FSMDefines.h */, 158 | 8EFA08C31C50E25800F6D790 /* FSMEngine.h */, 159 | 8EFA08C41C50E25800F6D790 /* FSMEngine.m */, 160 | 8EFA08C51C50E25800F6D790 /* FSMStateUtil.h */, 161 | 8EFA08C61C50E25800F6D790 /* FSMStateUtil.m */, 162 | 8EFA08C71C50E25800F6D790 /* ReachState.h */, 163 | 8EFA08C81C50E25800F6D790 /* ReachState.m */, 164 | 8EFA08C91C50E25800F6D790 /* ReachStateLoading.h */, 165 | 8EFA08CA1C50E25800F6D790 /* ReachStateLoading.m */, 166 | 8EFA08CB1C50E25800F6D790 /* ReachStateUnloaded.h */, 167 | 8EFA08CC1C50E25800F6D790 /* ReachStateUnloaded.m */, 168 | 8EFA08CD1C50E25800F6D790 /* ReachStateUnReachable.h */, 169 | 8EFA08CE1C50E25800F6D790 /* ReachStateUnReachable.m */, 170 | 8EFA08CF1C50E25800F6D790 /* ReachStateWIFI.h */, 171 | 8EFA08D01C50E25800F6D790 /* ReachStateWIFI.m */, 172 | 8EFA08D11C50E25800F6D790 /* ReachStateWWAN.h */, 173 | 8EFA08D21C50E25800F6D790 /* ReachStateWWAN.m */, 174 | ); 175 | path = FSM; 176 | sourceTree = ""; 177 | }; 178 | 8EFA08D31C50E25800F6D790 /* Ping */ = { 179 | isa = PBXGroup; 180 | children = ( 181 | 8EFA08D41C50E25800F6D790 /* PingFoundation.h */, 182 | 8EFA08D51C50E25800F6D790 /* PingFoundation.m */, 183 | 8EFA08D61C50E25800F6D790 /* PingHelper.h */, 184 | 8EFA08D71C50E25800F6D790 /* PingHelper.m */, 185 | ); 186 | path = Ping; 187 | sourceTree = ""; 188 | }; 189 | /* End PBXGroup section */ 190 | 191 | /* Begin PBXNativeTarget section */ 192 | 8EE0B62D1C40F71800CBABCA /* testRealReachability */ = { 193 | isa = PBXNativeTarget; 194 | buildConfigurationList = 8EE0B6451C40F71900CBABCA /* Build configuration list for PBXNativeTarget "testRealReachability" */; 195 | buildPhases = ( 196 | 8EE0B62A1C40F71800CBABCA /* Sources */, 197 | 8EE0B62B1C40F71800CBABCA /* Frameworks */, 198 | 8EE0B62C1C40F71800CBABCA /* Resources */, 199 | ); 200 | buildRules = ( 201 | ); 202 | dependencies = ( 203 | ); 204 | name = testRealReachability; 205 | productName = testRealReachability; 206 | productReference = 8EE0B62E1C40F71900CBABCA /* testRealReachability.app */; 207 | productType = "com.apple.product-type.application"; 208 | }; 209 | /* End PBXNativeTarget section */ 210 | 211 | /* Begin PBXProject section */ 212 | 8EE0B6261C40F71800CBABCA /* Project object */ = { 213 | isa = PBXProject; 214 | attributes = { 215 | LastUpgradeCheck = 0710; 216 | ORGANIZATIONNAME = QCStudio; 217 | TargetAttributes = { 218 | 8EE0B62D1C40F71800CBABCA = { 219 | CreatedOnToolsVersion = 7.1.1; 220 | DevelopmentTeam = K93K8C6UUM; 221 | }; 222 | }; 223 | }; 224 | buildConfigurationList = 8EE0B6291C40F71800CBABCA /* Build configuration list for PBXProject "testRealReachability" */; 225 | compatibilityVersion = "Xcode 3.2"; 226 | developmentRegion = English; 227 | hasScannedForEncodings = 0; 228 | knownRegions = ( 229 | en, 230 | Base, 231 | ); 232 | mainGroup = 8EE0B6251C40F71800CBABCA; 233 | productRefGroup = 8EE0B62F1C40F71900CBABCA /* Products */; 234 | projectDirPath = ""; 235 | projectRoot = ""; 236 | targets = ( 237 | 8EE0B62D1C40F71800CBABCA /* testRealReachability */, 238 | ); 239 | }; 240 | /* End PBXProject section */ 241 | 242 | /* Begin PBXResourcesBuildPhase section */ 243 | 8EE0B62C1C40F71800CBABCA /* Resources */ = { 244 | isa = PBXResourcesBuildPhase; 245 | buildActionMask = 2147483647; 246 | files = ( 247 | 8EE0B6411C40F71900CBABCA /* LaunchScreen.storyboard in Resources */, 248 | 8EE0B63E1C40F71900CBABCA /* Assets.xcassets in Resources */, 249 | 8EE0B63C1C40F71900CBABCA /* Main.storyboard in Resources */, 250 | ); 251 | runOnlyForDeploymentPostprocessing = 0; 252 | }; 253 | /* End PBXResourcesBuildPhase section */ 254 | 255 | /* Begin PBXSourcesBuildPhase section */ 256 | 8EE0B62A1C40F71800CBABCA /* Sources */ = { 257 | isa = PBXSourcesBuildPhase; 258 | buildActionMask = 2147483647; 259 | files = ( 260 | 8EFA08E31C50E25800F6D790 /* PingFoundation.m in Sources */, 261 | 8EFA08DE1C50E25800F6D790 /* ReachStateLoading.m in Sources */, 262 | 8EFA08DD1C50E25800F6D790 /* ReachState.m in Sources */, 263 | 8EFA08E41C50E25800F6D790 /* PingHelper.m in Sources */, 264 | 8EE0B6391C40F71900CBABCA /* ViewController.m in Sources */, 265 | 8EE0B6361C40F71900CBABCA /* AppDelegate.m in Sources */, 266 | 8EE0B6331C40F71900CBABCA /* main.m in Sources */, 267 | 8EFA08DA1C50E25800F6D790 /* LocalConnection.m in Sources */, 268 | 8EFA08E51C50E25800F6D790 /* RealReachability.m in Sources */, 269 | 8EFA08DC1C50E25800F6D790 /* FSMStateUtil.m in Sources */, 270 | 8EFA08DB1C50E25800F6D790 /* FSMEngine.m in Sources */, 271 | 8EFA08E21C50E25800F6D790 /* ReachStateWWAN.m in Sources */, 272 | 8EF6153420CFEDD100D425CD /* NSObject+SimpleKVO.m in Sources */, 273 | 8EFA08E01C50E25800F6D790 /* ReachStateUnReachable.m in Sources */, 274 | 8EFA08E11C50E25800F6D790 /* ReachStateWIFI.m in Sources */, 275 | 8EFA08DF1C50E25800F6D790 /* ReachStateUnloaded.m in Sources */, 276 | ); 277 | runOnlyForDeploymentPostprocessing = 0; 278 | }; 279 | /* End PBXSourcesBuildPhase section */ 280 | 281 | /* Begin PBXVariantGroup section */ 282 | 8EE0B63A1C40F71900CBABCA /* Main.storyboard */ = { 283 | isa = PBXVariantGroup; 284 | children = ( 285 | 8EE0B63B1C40F71900CBABCA /* Base */, 286 | ); 287 | name = Main.storyboard; 288 | sourceTree = ""; 289 | }; 290 | 8EE0B63F1C40F71900CBABCA /* LaunchScreen.storyboard */ = { 291 | isa = PBXVariantGroup; 292 | children = ( 293 | 8EE0B6401C40F71900CBABCA /* Base */, 294 | ); 295 | name = LaunchScreen.storyboard; 296 | sourceTree = ""; 297 | }; 298 | /* End PBXVariantGroup section */ 299 | 300 | /* Begin XCBuildConfiguration section */ 301 | 8EE0B6431C40F71900CBABCA /* Debug */ = { 302 | isa = XCBuildConfiguration; 303 | buildSettings = { 304 | ALWAYS_SEARCH_USER_PATHS = NO; 305 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 306 | CLANG_CXX_LIBRARY = "libc++"; 307 | CLANG_ENABLE_MODULES = YES; 308 | CLANG_ENABLE_OBJC_ARC = YES; 309 | CLANG_WARN_BOOL_CONVERSION = YES; 310 | CLANG_WARN_CONSTANT_CONVERSION = YES; 311 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 312 | CLANG_WARN_EMPTY_BODY = YES; 313 | CLANG_WARN_ENUM_CONVERSION = YES; 314 | CLANG_WARN_INT_CONVERSION = YES; 315 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 316 | CLANG_WARN_UNREACHABLE_CODE = YES; 317 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 318 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 319 | COPY_PHASE_STRIP = NO; 320 | DEBUG_INFORMATION_FORMAT = dwarf; 321 | ENABLE_STRICT_OBJC_MSGSEND = YES; 322 | ENABLE_TESTABILITY = YES; 323 | GCC_C_LANGUAGE_STANDARD = gnu99; 324 | GCC_DYNAMIC_NO_PIC = NO; 325 | GCC_NO_COMMON_BLOCKS = YES; 326 | GCC_OPTIMIZATION_LEVEL = 0; 327 | GCC_PREPROCESSOR_DEFINITIONS = ( 328 | "DEBUG=1", 329 | "$(inherited)", 330 | ); 331 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 332 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 333 | GCC_WARN_UNDECLARED_SELECTOR = YES; 334 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 335 | GCC_WARN_UNUSED_FUNCTION = YES; 336 | GCC_WARN_UNUSED_VARIABLE = YES; 337 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 338 | MTL_ENABLE_DEBUG_INFO = YES; 339 | ONLY_ACTIVE_ARCH = YES; 340 | SDKROOT = iphoneos; 341 | }; 342 | name = Debug; 343 | }; 344 | 8EE0B6441C40F71900CBABCA /* Release */ = { 345 | isa = XCBuildConfiguration; 346 | buildSettings = { 347 | ALWAYS_SEARCH_USER_PATHS = NO; 348 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 349 | CLANG_CXX_LIBRARY = "libc++"; 350 | CLANG_ENABLE_MODULES = YES; 351 | CLANG_ENABLE_OBJC_ARC = YES; 352 | CLANG_WARN_BOOL_CONVERSION = YES; 353 | CLANG_WARN_CONSTANT_CONVERSION = YES; 354 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 355 | CLANG_WARN_EMPTY_BODY = YES; 356 | CLANG_WARN_ENUM_CONVERSION = YES; 357 | CLANG_WARN_INT_CONVERSION = YES; 358 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 359 | CLANG_WARN_UNREACHABLE_CODE = YES; 360 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 361 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 362 | COPY_PHASE_STRIP = NO; 363 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 364 | ENABLE_NS_ASSERTIONS = NO; 365 | ENABLE_STRICT_OBJC_MSGSEND = YES; 366 | GCC_C_LANGUAGE_STANDARD = gnu99; 367 | GCC_NO_COMMON_BLOCKS = YES; 368 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 369 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 370 | GCC_WARN_UNDECLARED_SELECTOR = YES; 371 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 372 | GCC_WARN_UNUSED_FUNCTION = YES; 373 | GCC_WARN_UNUSED_VARIABLE = YES; 374 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 375 | MTL_ENABLE_DEBUG_INFO = NO; 376 | SDKROOT = iphoneos; 377 | VALIDATE_PRODUCT = YES; 378 | }; 379 | name = Release; 380 | }; 381 | 8EE0B6461C40F71900CBABCA /* Debug */ = { 382 | isa = XCBuildConfiguration; 383 | buildSettings = { 384 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 385 | DEVELOPMENT_TEAM = K93K8C6UUM; 386 | INFOPLIST_FILE = testRealReachability/Info.plist; 387 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 388 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 389 | PRODUCT_BUNDLE_IDENTIFIER = qc.testRealReachability; 390 | PRODUCT_NAME = "$(TARGET_NAME)"; 391 | }; 392 | name = Debug; 393 | }; 394 | 8EE0B6471C40F71900CBABCA /* Release */ = { 395 | isa = XCBuildConfiguration; 396 | buildSettings = { 397 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 398 | DEVELOPMENT_TEAM = K93K8C6UUM; 399 | INFOPLIST_FILE = testRealReachability/Info.plist; 400 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 401 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 402 | PRODUCT_BUNDLE_IDENTIFIER = qc.testRealReachability; 403 | PRODUCT_NAME = "$(TARGET_NAME)"; 404 | }; 405 | name = Release; 406 | }; 407 | /* End XCBuildConfiguration section */ 408 | 409 | /* Begin XCConfigurationList section */ 410 | 8EE0B6291C40F71800CBABCA /* Build configuration list for PBXProject "testRealReachability" */ = { 411 | isa = XCConfigurationList; 412 | buildConfigurations = ( 413 | 8EE0B6431C40F71900CBABCA /* Debug */, 414 | 8EE0B6441C40F71900CBABCA /* Release */, 415 | ); 416 | defaultConfigurationIsVisible = 0; 417 | defaultConfigurationName = Release; 418 | }; 419 | 8EE0B6451C40F71900CBABCA /* Build configuration list for PBXNativeTarget "testRealReachability" */ = { 420 | isa = XCConfigurationList; 421 | buildConfigurations = ( 422 | 8EE0B6461C40F71900CBABCA /* Debug */, 423 | 8EE0B6471C40F71900CBABCA /* Release */, 424 | ); 425 | defaultConfigurationIsVisible = 0; 426 | defaultConfigurationName = Release; 427 | }; 428 | /* End XCConfigurationList section */ 429 | }; 430 | rootObject = 8EE0B6261C40F71800CBABCA /* Project object */; 431 | } 432 | -------------------------------------------------------------------------------- /testRealReachability.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /testRealReachability.xcodeproj/xcshareddata/xcschemes/testRealReachability.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 | -------------------------------------------------------------------------------- /testRealReachability/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Dustturtle 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright © 2016 Dustturtle. 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 | -------------------------------------------------------------------------------- /testRealReachability/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Dustturtle 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "RealReachability.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | // Override point for customization after application launch. 21 | GLobalRealReachability.hostForPing = @"www.baidu.com"; 22 | GLobalRealReachability.hostForCheck = @"www.apple.com"; 23 | [GLobalRealReachability startNotifier]; 24 | return YES; 25 | } 26 | 27 | - (void)applicationWillResignActive:(UIApplication *)application { 28 | // 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. 29 | // 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. 30 | } 31 | 32 | - (void)applicationDidEnterBackground:(UIApplication *)application { 33 | // 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. 34 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 35 | } 36 | 37 | - (void)applicationWillEnterForeground:(UIApplication *)application { 38 | // 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. 39 | } 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // 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. 43 | } 44 | 45 | - (void)applicationWillTerminate:(UIApplication *)application { 46 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /testRealReachability/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 | } -------------------------------------------------------------------------------- /testRealReachability/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 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /testRealReachability/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 | 31 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /testRealReachability/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 | -------------------------------------------------------------------------------- /testRealReachability/KVO/NSObject+SimpleKVO.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+SimpleKVO.h 3 | // ExMobi 4 | // 5 | // Created by achen on 16/7/1. 6 | // Simply modified from NSObject+YYAddForKVO. 7 | // 8 | // 一个从YYAddForKVO修改而来的极致简化的KVO封装(从API参数设计到调用方式) 9 | // 10 | // 注意:这里监听的是值的变化,其值确实改变后才会触发回调; 11 | // 其行为和原始KVO以及YYAddForKVO都不同。 12 | // 后两者的行为是只要被赋值就会触发(由KVO的实现原理决定的,而simpleKVO作了特别处理), 13 | // 使其更易用于某些业务场景的需求实现。 14 | // 15 | // 单元测试覆盖率100/100。 16 | // 17 | // Thanks to ibireme! 18 | // Original project: 19 | // YYKit (author ibireme): 20 | // 21 | 22 | #import 23 | 24 | #define ENABLE_SWIZZ_IN_SIMPLEKVO 25 | 26 | @interface NSObject (SimpleKVO) 27 | 28 | - (void)addKVOForPath:(NSString*)path withBlock:(void (^)(id newValue))block; 29 | 30 | - (void)removeKVOForPath:(NSString *)path; 31 | 32 | - (void)removeAllKVOs; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /testRealReachability/KVO/NSObject+SimpleKVO.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+SimpleKVO.m 3 | // ExMobi 4 | // 5 | // Created by achen on 16/7/1. 6 | // 7 | // 8 | 9 | #import "NSObject+SimpleKVO.h" 10 | #import 11 | 12 | static const int block_key; 13 | 14 | @interface SimpleKVOBlockTarget : NSObject 15 | 16 | @property (nonatomic, copy) void (^block)(id newVal); 17 | 18 | - (id)initWithBlock:(void (^)(id newValue))block; 19 | 20 | @end 21 | 22 | @implementation SimpleKVOBlockTarget 23 | 24 | - (id)initWithBlock:(void (^)(id newValue))block 25 | { 26 | self = [super init]; 27 | if (self) 28 | { 29 | self.block = block; 30 | } 31 | 32 | return self; 33 | } 34 | 35 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 36 | { 37 | if (self.block != nil) 38 | { 39 | id oldValue = [change objectForKey:NSKeyValueChangeOldKey]; 40 | if (oldValue == [NSNull null]) 41 | { 42 | oldValue = nil; 43 | } 44 | 45 | id newValue = [change objectForKey:NSKeyValueChangeNewKey]; 46 | if (newValue == [NSNull null]) 47 | { 48 | newValue = nil; 49 | } 50 | 51 | if (oldValue == nil && newValue == nil) 52 | { 53 | return; 54 | } 55 | 56 | if (oldValue == nil || newValue == nil) 57 | { 58 | self.block(newValue); 59 | } 60 | // 根据测试发现这里所有的基本类型值都被系统自动转换成了NSXXX,因此可以对其使用isEqual进行比较。 61 | else if (![oldValue isEqual:newValue]) 62 | { 63 | self.block(newValue); 64 | } 65 | } 66 | } 67 | 68 | @end 69 | 70 | @implementation NSObject (SimpleKVO) 71 | 72 | - (void)addKVOForPath:(NSString *)path withBlock:(void (^)(id newValue))block 73 | { 74 | if ([path length] <= 0 || block == nil) 75 | { 76 | return; 77 | } 78 | 79 | SimpleKVOBlockTarget *target = [[SimpleKVOBlockTarget alloc] initWithBlock:block]; 80 | NSMutableDictionary *dic = [self simpleKVOBlocksLazyLoad]; 81 | NSMutableArray *blockTargetsForPath = dic[path]; 82 | if (!blockTargetsForPath) 83 | { 84 | blockTargetsForPath = [NSMutableArray new]; 85 | dic[path] = blockTargetsForPath; 86 | } 87 | [blockTargetsForPath addObject:target]; 88 | [self addObserver:target forKeyPath:path options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; 89 | } 90 | 91 | - (void)removeKVOForPath:(NSString *)path 92 | { 93 | if ([path length] > 0) 94 | { 95 | NSMutableDictionary *dic = [self simpleKVOBlocks]; 96 | 97 | if (dic == nil) 98 | { 99 | return; 100 | } 101 | 102 | NSMutableArray *arr = dic[path]; 103 | [arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { 104 | [self removeObserver:obj forKeyPath:path]; 105 | }]; 106 | 107 | [dic removeObjectForKey:path]; 108 | } 109 | } 110 | 111 | - (void)removeAllKVOs 112 | { 113 | NSMutableDictionary *dic = [self simpleKVOBlocks]; 114 | 115 | if (dic == nil) 116 | { 117 | return; 118 | } 119 | 120 | [dic enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSArray *arr, BOOL *stop) { 121 | [arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) { 122 | [self removeObserver:obj forKeyPath:key]; 123 | }]; 124 | }]; 125 | 126 | [dic removeAllObjects]; 127 | 128 | objc_setAssociatedObject(self, &block_key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 129 | } 130 | 131 | - (NSMutableDictionary *)simpleKVOBlocksLazyLoad 132 | { 133 | NSMutableDictionary *targets = objc_getAssociatedObject(self, &block_key); 134 | 135 | if (!targets) 136 | { 137 | targets = [NSMutableDictionary new]; 138 | objc_setAssociatedObject(self, &block_key, targets, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 139 | } 140 | 141 | return targets; 142 | } 143 | 144 | - (NSMutableDictionary *)simpleKVOBlocks 145 | { 146 | return objc_getAssociatedObject(self, &block_key); 147 | } 148 | 149 | #ifdef ENABLE_SWIZZ_IN_SIMPLEKVO 150 | 151 | + (void)load 152 | { 153 | static dispatch_once_t onceToken; 154 | dispatch_once(&onceToken, ^{ 155 | NSString *selString = @"dealloc"; 156 | NSString *kvoSelString = [@"simpleKVO_" stringByAppendingString:selString]; 157 | Method originalDealloc = class_getInstanceMethod(self, NSSelectorFromString(selString)); 158 | Method kvoDealloc = class_getInstanceMethod(self, NSSelectorFromString(kvoSelString)); 159 | method_exchangeImplementations(originalDealloc, kvoDealloc); 160 | }); 161 | } 162 | 163 | - (BOOL)isSimpleKVO 164 | { 165 | if (objc_getAssociatedObject(self, &block_key) != nil) 166 | { 167 | return YES; 168 | } 169 | else 170 | { 171 | return NO; 172 | } 173 | } 174 | 175 | - (void)simpleKVO_dealloc 176 | { 177 | 178 | if ([self isSimpleKVO]) 179 | { 180 | [self removeAllKVOs]; 181 | } 182 | 183 | [self simpleKVO_dealloc]; 184 | } 185 | 186 | #endif 187 | 188 | @end 189 | 190 | 191 | -------------------------------------------------------------------------------- /testRealReachability/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /testRealReachability/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // RealReachability 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright © 2016 Dustturtle. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "RealReachability.h" 11 | #import "NSObject+SimpleKVO.h" 12 | 13 | @interface ViewController () 14 | 15 | @property (nonatomic, weak) IBOutlet UILabel *flagLabel; 16 | 17 | @property (nonatomic, strong) UIAlertView *alert; 18 | 19 | @end 20 | 21 | @implementation ViewController 22 | 23 | - (void)viewDidLoad 24 | { 25 | [super viewDidLoad]; 26 | 27 | [[NSNotificationCenter defaultCenter] addObserver:self 28 | selector:@selector(networkChanged:) 29 | name:kRealReachabilityChangedNotification 30 | object:nil]; 31 | 32 | [[NSNotificationCenter defaultCenter] addObserver:self 33 | selector:@selector(VPNStatusChanged:) 34 | name:kRRVPNStatusChangedNotification 35 | object:nil]; 36 | 37 | ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus]; 38 | NSLog(@"Initial reachability status:%@",@(status)); 39 | 40 | [self setupFlagLabelWithStatus:status 41 | isVPNOn:[GLobalRealReachability isVPNOn] 42 | accessType:[GLobalRealReachability currentWWANtype]]; 43 | 44 | self.alert = [[UIAlertView alloc] initWithTitle:@"RealReachability" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 45 | } 46 | 47 | - (void)dealloc 48 | { 49 | [[NSNotificationCenter defaultCenter] removeObserver:self]; 50 | } 51 | 52 | - (IBAction)testAction:(id)sender 53 | { 54 | // NSLog(@"begin"); 55 | // // performance test of this method; ok. 56 | // for (NSInteger i=0; i<10000; i++) 57 | // { 58 | // [GLobalRealReachability isVPNOn]; 59 | // } 60 | // NSLog(@"end"); 61 | 62 | [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) { 63 | switch (status) 64 | { 65 | case RealStatusNotReachable: 66 | { 67 | self.alert.message = @"Nothing to do! offlineMode"; 68 | [self.alert show]; 69 | 70 | break; 71 | } 72 | 73 | case RealStatusViaWiFi: 74 | { 75 | self.alert.message = @"Do what you want! free!"; 76 | [self.alert show]; 77 | 78 | break; 79 | } 80 | 81 | case RealStatusViaWWAN: 82 | { 83 | self.alert.message = @"Take care of your money! You are in charge!"; 84 | break; 85 | } 86 | 87 | default: 88 | { 89 | self.alert.message = @"Error status, needs debugging!"; 90 | break; 91 | } 92 | } 93 | 94 | [self.alert show]; 95 | }]; 96 | } 97 | 98 | - (void)networkChanged:(NSNotification *)notification 99 | { 100 | RealReachability *reachability = (RealReachability *)notification.object; 101 | ReachabilityStatus status = [reachability currentReachabilityStatus]; 102 | ReachabilityStatus previousStatus = [reachability previousReachabilityStatus]; 103 | NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus)); 104 | 105 | [self setupFlagLabelWithStatus:status 106 | isVPNOn:[GLobalRealReachability isVPNOn] 107 | accessType:[GLobalRealReachability currentWWANtype]]; 108 | } 109 | 110 | - (void)VPNStatusChanged:(NSNotification *)notification 111 | { 112 | // refreshing the status. 113 | [self setupFlagLabelWithStatus:[GLobalRealReachability currentReachabilityStatus] 114 | isVPNOn:[GLobalRealReachability isVPNOn] 115 | accessType:[GLobalRealReachability currentWWANtype]]; 116 | } 117 | 118 | - (void)setupFlagLabelWithStatus:(ReachabilityStatus)status 119 | isVPNOn:(BOOL)isVPNOn 120 | accessType:(WWANAccessType)accessType 121 | { 122 | NSMutableString *labelStr = [@"" mutableCopy]; 123 | 124 | switch (status) 125 | { 126 | case RealStatusNotReachable: 127 | { 128 | [labelStr appendString:@"Network unreachable! "]; 129 | break; 130 | } 131 | 132 | case RealStatusViaWiFi: 133 | { 134 | [labelStr appendString:@"Network wifi! Free! "]; 135 | break; 136 | } 137 | 138 | case RealStatusViaWWAN: 139 | { 140 | [labelStr appendString:@"WWAN in charge! "]; 141 | break; 142 | } 143 | 144 | case RealStatusUnknown: 145 | { 146 | [labelStr appendString:@"Unknown status! Needs debugging! "]; 147 | break; 148 | } 149 | 150 | default: 151 | { 152 | [labelStr appendString:@"Status error! Needs debugging! "]; 153 | break; 154 | } 155 | } 156 | 157 | if (isVPNOn) 158 | { 159 | [labelStr appendString:@"VPN On! "]; 160 | } 161 | 162 | if (status == RealStatusViaWWAN) 163 | { 164 | NSString *descStr; 165 | if (accessType == WWANType2G) 166 | { 167 | descStr = @"2G"; 168 | } 169 | else if (accessType == WWANType3G) 170 | { 171 | descStr = @"3G"; 172 | } 173 | else if (accessType == WWANType4G) 174 | { 175 | descStr = @"4G"; 176 | } 177 | else 178 | { 179 | descStr = @"Unknown Status, might be iOS6"; 180 | } 181 | 182 | [labelStr appendString:descStr]; 183 | } 184 | 185 | self.flagLabel.text = [labelStr copy]; 186 | } 187 | 188 | @end 189 | -------------------------------------------------------------------------------- /testRealReachability/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Dustturtle 4 | // 5 | // Created by Dustturtle on 16/1/9. 6 | // Copyright © 2016 Dustturtle. 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 | --------------------------------------------------------------------------------