In iOS, Reachability is used to detect the network type of the mobile phone. Wi-Fi and 2 3 4G, iosreachability
If you want to provide a Reeder In the iOS program, or provide offline mode (Evernote) in the absence of a network ). Then you will use the Reachability for network detection.
Purpose of writing this article
- Understand what Reachability can do
- Detection 3 network environment
- How to Use notifications
- Single controller
- Multiple Controllers
- Simple functions:
Reachability Overview
Reachablity is a library used for iOS device network environment detection.
- Monitor whether the target network is available
- Monitor the connection mode of the current network
- Monitor connection mode changes
Doc officially provided by Apple
Http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
Github documentation
Https://github.com/tonymillion/Reachability
Install
Because Reachability is very common. Add it directly to Supporting Files/networ-Prefix.pch:
C code
- # Import <Reachability/Reachability. h>
If you do not know what cocoaspod is, check the following:
Http://witcheryne.iteye.com/blog/1873221
Use
There is an answer in stackoverflow to explain the reachability usage.
Http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability
- Generally, a Reachability instance is OK.
- Only one Reachability is required for a Controller.
Block
C code
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
- DLog (@ "enable www.apple.com network detection ");
- Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"];
- DLog (@ "-- current status: % @", reach. currentReachabilityString );
- // Start the notifier which will cause the reachability object to retain itself!
- [[Nsicationcenter center defacenter center] 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];
- }
Notification
C code
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
- DLog (@ "enable www.apple.com network detection ");
- Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"];
- DLog (@ "-- current status: % @", reach. currentReachabilityString );
- // Start the notifier which will cause the reachability object to retain itself!
- [[Nsicationcenter center defacenter center] addObserver: self
- Selector: @ selector (reachabilityChanged :)
- Name: kReachabilityChangedNotification
- Object: nil];
- [Reach startNotifier];
- }
- -(Void) reachabilityChanged: (NSNotification *) note {
- Reachability * reach = [note object];
- If (! [Reach isReachable])
- {
- Self. icationicationlabel. text = @ "Network unavailable ";
- Self. icationicationlabel. backgroundColor = [UIColor redColor];
- Self. wifiOnlyLabel. backgroundColor = [UIColor redColor];
- Self. wwanOnlyLabel. backgroundColor = [UIColor redColor];
- Return;
- }
- Self. icationicationlabel. text = @ "network available ";
- Self. icationicationlabel. 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 is not enabled and cannot be used ";
- }
- If (reach. isReachableViaWWAN ){
- Self. wwanOnlyLabel. backgroundColor = [UIColor greenColor];
- Self. wwanOnlyLabel. text = @ "currently connected through 2g or 3g ";
- } Else {
- Self. wwanOnlyLabel. backgroundColor = [UIColor redColor];
- Self. wwanOnlyLabel. text = @ "2g or 3g network not used ";
- }
- }
Appendix demo description enable wifi
Disable wifi status
Legacy problems