Monitor network status

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.