Monitor network status
Transfer from http://www.cnblogs.com/wendingding/p/3950114.html#
Some modifications are permitted.
#import "YYViewController.h"
#import "Reachability.h"
@interface Yyviewcontroller ()
@property (nonatomic, strong) reachability *conn;
@end
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: Kreachabilitychangednotification Object:nil];
Self.conn = [reachability reachabilityforinternetconnection];
[Self.conn Startnotifier];
[[Nsnotificationcenter Defaultcenter] postnotificationname:kreachabilitychangednotification object:nil];//notice (Increase)
}
-(void) dealloc
{
[Self.conn Stopnotifier];
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}
-(void) Networkstatechange
{
[Self checknetworkstate];
}
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
}
-(void) checknetworkstate
{
1. Detect WiFi Status
reachability *wifi = [reachability Reachabilityforlocalwifi];
2. Check whether the mobile phone can be on the network (wifi\3g\2.5g)
reachability *conn = [reachability reachabilityforinternetconnection];
3. Determine network status
if ([WiFi currentreachabilitystatus]! = notreachable) {//WiFi available
NSLog (@ "WiFi");
} else if ([conn currentreachabilitystatus]! = notreachable) {//not using WiFi, use your phone to bring your own network to the Internet
NSLog (@ "Use your phone to bring your own network to the Internet");
} else {//no network
NSLog (@ "no network");
Uialertview *alert = [[Uialertview alloc] Initwithtitle:nil
message:@ "No Network Please check network Settings"
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alert show];
alert = nil;
}
}
@end
With WiFi
[WiFi currentreachabilitystatus]! = notreachable
[Conn Currentreachabilitystatus]! = notreachable
No wifi, only mobile phone network
[WiFi currentreachabilitystatus] = = notreachable
[Conn Currentreachabilitystatus]! = notreachable
No network
[WiFi currentreachabilitystatus] = = notreachable
[conn Currentreachabilitystatus] = = notreachable
This is only detected when the network state changes, but in the first time there is no network in the state of the need to judge and then prompt information
Monitor network status