Content tip:Offline mode (Evernote) is available below. Then you will use the reachability to implement network detection. The purpose of this article is to understand what reachability can do. Detection of 3 in the network environment 2g/3g WiFi No network How to use the notification single controller multiple controller simple function: Only use the reachability introduction Reacha Blity is an iOS ...
If you want to provide an iOS program only for use under WiFi network (Reeder), or to provide offline mode (Evernote) in the absence of network status. Then you will use the reachability to implement network detection.
The purpose of writing this article
- Learn what reachability can do
- Detecting the network environment in 3
- How to use Notifications
- Single Controller
- Multiple controllers
- Simple features:
reachability Introduction
Reachablity is a library of iOS device network environments that detects iOS devices.
- Monitoring whether the target network is available
- Monitor how your current network is connected
- Monitoring changes in connection mode
The official doc provided by Apple
http://developer.apple.com/library/ios/#samplecode/reachability/introduction/intro.html
Documentation on GitHub
Https://github.com/tonymillion/Reachability
Installation
- Create a network project (network is the demo project I created, which can be downloaded in the attachment)
- Installing dependencies using Cocoaspod
- Add a systemconfiguration.framework library to your project
Because reachability is very common. Add it directly to the supporting FILES/NETWOR-PREFIX.PCH:
| 1 |
#import <Reachability/Reachability.h> |
If you don't yet know what cocoaspod is, look here:
http://witcheryne.iteye.com/blog/1873221
Use
There is an answer on the StackOverflow, which explains the use of reachability very well.
Http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability
- In general, a reachability instance is ok.
- A controller needs only one reachability.
Block mode use
| 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 |
- (void)viewDidLoad { [super viewDidLoad]; DLog(@"开启 www.apple.com 的网络检测"); Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"]; DLog(@"-- current status: %@", reach.currentReachabilityString); // start the notifier which will cause the reachability object to retain itself! [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; reach.reachableBlock = ^(Reachability * reachability) { dispatch_async(dispatch_get_main_queue(), ^{ self.blockLabel.text = @"网络可用"; self.blockLabel.backgroundColor = [UIColor greenColor]; }); }; reach.unreachableBlock = ^(Reachability * reachability) { dispatch_async(dispatch_get_main_queue(), ^{ self.blockLabel.text = @"网络不可用"; self.blockLabel.backgroundColor = [UIColor redColor]; }); }; [reach startNotifier]; } |
How to use notification
| 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
- (void)viewDidLoad { [super viewDidLoad]; DLog(@"开启 www.apple.com 的网络检测"); Reachability* reach = [Reachability reachabilityWithHostname:@"www.apple.com"]; DLog(@"-- current status: %@", reach.currentReachabilityString); // start the notifier which will cause the reachability object to retain itself! [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; [reach startNotifier]; }- (void) reachabilityChanged: (NSNotification*)note { Reachability * reach = [note object]; if(![reach isReachable]) { self.notificationLabel.text = @"网络不可用"; self.notificationLabel.backgroundColor = [UIColor redColor]; self.wifiOnlyLabel.backgroundColor = [UIColor redColor]; self.wwanOnlyLabel.backgroundColor = [UIColor redColor]; return; } self.notificationLabel.text = @"网络可用"; self.notificationLabel.backgroundColor = [UIColor greenColor]; if (reach.isReachableViaWiFi) { self.wifiOnlyLabel.backgroundColor = [UIColor greenColor]; self.wifiOnlyLabel.text = @"当前通过wifi连接"; } else { self.wifiOnlyLabel.backgroundColor = [UIColor redColor]; self.wifiOnlyLabel.text = @"wifi未开启,不能用"; } if (reach.isReachableViaWWAN) { self.wwanOnlyLabel.backgroundColor = [UIColor greenColor]; self.wwanOnlyLabel.text = @"当前通过2g or 3g连接"; } else { self.wwanOnlyLabel.backgroundColor = [UIColor redColor]; self.wwanOnlyLabel.text = @"2g or 3g网络未使用"; } } |
Accessories demo instructions turn on WiFi status
Turn off the WiFi status
Legacy issues
- How to share a reachability before multiple controllers (attachment demo is a controller one reachability instance)
- What should be used to stop the detection of reachability.
Ext.: http://www.rrzhai.com/p/7396