1.AFN in the frame: Afnetworkreachabilitymanager
AFN Judging the network
-(void) getinternetstatue{
1. Managers who have access to network monitoring
Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanagersharedmanager];
2. Set up the processing after the network status change
[Mgr setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatusstatus) {
When the network state changes, the block is called
Switch (status) {
caseafnetworkreachabilitystatusunknown://Unknown Network
NSLog (@ "Unknown network");
Break
caseafnetworkreachabilitystatusnotreachable://No network (off-grid)
NSLog (@ "No network (off-Grid)");
Break
caseafnetworkreachabilitystatusreachableviawwan://mobile phone comes with network
NSLog (@ "Mobile phone comes with network");
Break
caseafnetworkreachabilitystatusreachableviawifi://WIFI
NSLog (@ "WIFI");
Break
}
if (Status ==afnetworkreachabilitystatusreachableviawwan | | status ==afnetworkreachabilitystatusreachableviawifi)
{
[Email protected] "network";
NSLog (@ "NET");
}else
{
[Email protected] "no network";
NSLog (@ "no net");
Uialertview *alert = [[uialertviewalloc]initwithtitle:@] network loses connection "message:nildelegate:selfcancelbuttontitle:@" Cancel " Otherbuttontitles:nil,nil];
Alert.delegate =self;
[Alert show];
}
}];
3. Start monitoring
[Mgr Startmonitoring];
}
========= 2. Apple comes with reachability =========
1. Enumeration representing the state of the network:
typedef enum:nsinteger { notreachable = 0,//no network available Reachableviawifi,//wifi connection Reachableviawwan//Unlimited WAN connection} NetworkStatus;
2, the following is the relevant interface and comments
/*! * To detect if a network request can reach the specified hostname */+ (instancetype) Reachabilitywithhostname: (NSString *) hostname;/*! * To detect if a network request can reach the given IP address */+ (instancetype) reachabilitywithaddress: (const struct sockaddr_in *) hostaddress;/*! * Check if the default router is valid. For apps that aren't connected to a specific host. */+ (instancetype) reachabilityforinternetconnection;/*! * Detects if the local WiFi connection is valid */+ (instancetype) reachabilityforlocalwifi;/*! * Start listening for notifications in the current runloop. */-(BOOL) startnotifier;-(void) stopnotifier;//Get network Status-(NetworkStatus) currentreachabilitystatus;/*! * Connection Requirement */-(BOOL) connectionrequired;
3. Notification identification when the network connection status changes
NSString *kreachabilitychangednotification = @ "Knetworkreachabilitychangednotification";
You need to copy the Reachability.h and REACHABILITY.M from the project to your project, and you need to add systemconfiguration.framework to the project,
Create reachability
reachability *reach = [reachability reachabilityforinternetconnection];
reach = [reachability reachabilitywithhostname:@ "www.baidu.com"];
Start monitoring the network (notify Kreachabilitychangednotification once the network status changes)
[Reach Startnotifier];
Turn on notification---way one:
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange:) Name: Kreachabilitychangednotification Object:nil];
Handling Network status changes
-(void) Networkstatechange: (nsnotification *) note{
reachability * reach = [Noti object];
NetworkStatus status = [reach Currentreachabilitystatus];
3. Determine network status
if (status== notreachable) {
NSLog (@ "no net");
} else if (Status==reachableviawwan) {
NSLog (@ "Use your phone to bring your own network to the Internet");
} else if (Status==reachableviawifi) {
NSLog (@ "via wifi");
}}
Registration Notice----WAY two:
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (reachabilitychanged:)
Name:kreachabilitychangednotification
object:nil];//Network Status Monitoring
-(void) reachabilitychanged: (nsnotification *) Noti {
reachability * reach = [Noti object];
if ([reach isreachable]) {
[Self setmbprogresswithnumtip:@ "network Connected"];
NSLog (@ "Network is connected");
[Lycommanmanager sharedmanager].isreachable=[reachisreachable];
[[nsnotificationcenterdefaultcenter]postnotificationname:@ "startloading" object:nil];//send notification start loading web page
} else {
NSLog (@ "Network not Connected");
[Self setmbprogresswithnumtip:@ "network not Connected"];
[Lycommanmanager sharedmanager].isreachable=[reachisreachable];
[[nsnotificationcenterdefaultcenter]postnotificationname:@ "endloading" object:nil];//send notification end load
}
}
-(void) viewdidoad{
if ([Lycommanmanager sharedmanager].isreachable) {
[Self setmbprogresswithnum:@ "Loading ..."];
NSLog (@ "Currently there is a network de");
}else {
[Self setmbprogresswithnumtip:@ "no network Connection"];
NSLog (@ "currently no Network");
}
[[Nsnotificationcenter Defaultcenter]addobserver:selfselector: @selector (finishloading) name:@ "Endloading" object: NIL];
[[Nsnotificationcenter Defaultcenter]addobserver:selfselector: @selector (startloading) name:@ "StartLoading" Object : nil];
}
Re-load Web pages after networking
-(void) startloading{
[Self settrainticketwithmkweb];
}
End Load Prompt
-(void) finishloading{
[Mbprogresshud HideHUDForView:self.view Animated:yes];
}
=========3. Judging the network ========= through the status bar
To get the network type from the status bar, the code is as follows:
-(NSString *) getnetworkstates{
UIApplication *app = [Uiapplicationsharedapplication];
Nsarray *children = [[[appvalueforkeypath:@ "StatusBar"]valueforkeypath:@ "Foregroundview"]subviews];
NSLog (@ "---%@---", children);
NSString *state = [[Nsstringalloc]init];
NSLog (@ "--empty string 0-%@", state);
int netType = 0;
Get to the network return code
For (ID Childin children) {
if ([Childiskindofclass:nsclassfromstring (@ "Uistatusbardatanetworkitemview")]) {
Get to status bar
NetType = [[Child valueforkeypath:@ "Datanetworktype"]intvalue];
NSLog (@ "NetType---%d", netType);
Switch (netType) {
Case 0:
state = @ "No network";
No network mode
Break
Case 1:
State = @ "2G";
Break
Case 2:
State = @ "3G";
Break
Case 3:
State = @ "4G";
Break
Case 5:
{
state = @ "WiFi";
NSLog (@ "5");
Break
Default
Break
}
}
}
Select by Status
}
return state;
}
Several methods of ios-network detection