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:
C code
- #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
C code
- -(void) Viewdidload
- {
- [Super Viewdidload];
- DLog (@"Turn on www.apple.com Network Detection");
- reachability* reach = [reachability reachabilitywithhostname:@"www.apple.com"];
- DLog (@"--Current status:%@", reach.currentreachabilitystring);
- //Start the notifier which would 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 = @"network available";
- Self.blockLabel.backgroundColor = [Uicolor Greencolor];
- });
- };
- Reach.unreachableblock = ^ (reachability * reachability)
- {
- Dispatch_async (Dispatch_get_main_queue (), ^{
- Self.blockLabel.text = @"Network unavailable";
- Self.blockLabel.backgroundColor = [Uicolor Redcolor];
- });
- };
- [Reach Startnotifier];
- }
How to use notification
C code
- -(void) Viewdidload
- {
- [Super Viewdidload];
- DLog (@"Turn on www.apple.com Network Detection");
- reachability* reach = [reachability reachabilitywithhostname:@"www.apple.com"];
- DLog (@"--Current status:%@", reach.currentreachabilitystring);
- //Start the notifier which would 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 = @"Network unavailable";
- Self.notificationLabel.backgroundColor = [Uicolor Redcolor];
- Self.wifiOnlyLabel.backgroundColor = [Uicolor Redcolor];
- Self.wwanOnlyLabel.backgroundColor = [Uicolor Redcolor];
- return;
- }
- Self.notificationLabel.text = @"network available";
- Self.notificationLabel.backgroundColor = [Uicolor Greencolor];
- if (reach.isreachableviawifi) {
- Self.wifiOnlyLabel.backgroundColor = [Uicolor Greencolor];
- Self.wifiOnlyLabel.text = @"Currently connected via WiFi";
- } Else {
- Self.wifiOnlyLabel.backgroundColor = [Uicolor Redcolor];
- Self.wifiOnlyLabel.text = @"WiFi not turned on, not available";
- }
- if (Reach.isreachableviawwan) {
- Self.wwanOnlyLabel.backgroundColor = [Uicolor Greencolor];
- Self.wwanOnlyLabel.text = @"Currently connected via 2g or 3g";
- } Else {
- Self.wwanOnlyLabel.backgroundColor = [Uicolor Redcolor];
- Self.wwanOnlyLabel.text = @"2g or 3g network not in use";
- }
- }
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.
Use reachability detection network in iOS to differentiate cellular type WiFi and 2 3 4 G