Use Reachability in iOS to detect network and 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>
#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];
}
-(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! [[Nsicationicationcenter 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 ";
}
}
-(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: jsonobject: 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 not enabled, not available";} 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
Ios development determines whether the network is enabled
Go to the official website to download his demo. Reachability. Here, Reachability. h and Reachability. m are imported to your project. Hope to adopt
IOS oc checks whether mobile network and Wi-Fi are enabled
Use currentReachabilityStatus, A Reachability method, to obtain the enumerated type value and determine whether it is 2. If it is not 2, it is not WiFi.
Enum {
// DDG NetworkStatus Constant Names.
KNotReachable = 0, // Apple's code depends upon 'notreachable' being the same value as 'no '.
KReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
KReachableViaWiFi
};
Typedef uint32_t NetworkStatus;