Two ways to detect network state in IOS _ios

Source: Internet
Author: User

Generally there are two ways, are the third party frame, the wheel, you can use the first, the back to optimize.

One: reachability

1. First add the header file "Reachability.h" to the AppDelegate.h, and import the frame systemconfiguration.frame.

2. In APPDELEGATE.M, this is achieved:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{// Turn on the network status Monitor//To subscribe to real-time network status change notifications. Import the Reachability.h header file, and then register an object to subscribe to the information of the network state change, and the information name of the network state change is kreachabilitychanged-notification [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) name:kreachabilitychangednotification
Object:nil];
Determine if the current network is available by checking whether a host can access it: Self.hostreach = [reachability reachabilitywithhostname:@ "www.baidu.com"];
Start listening and start a run loop [Self.hostreach Startnotifier];
}-(void) reachabilitychanged: (nsnotification *) note{reachability *currreach = [Note Object];
Nsparameterassert ([Currreach iskindofclass:[reachability class]);
Respond to connection changes networkstatus status = [Currreach currentreachabilitystatus];
Pop-up alert if there is no connection to the network self.isreachable = YES; if (status = = notreachable) {Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Network connection exception" Message:nil Delegate:nil CA
ncelbuttontitle:@ "determine" otherbuttontitles:nil];
[Alert show]; [AlertRelease];
self.isreachable = NO;
Return } if (status==kreachableviawifi| | Status==kreachableviawwan) {Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Network connection Information" message:@ "Network connection normal"
Delegate:nil cancelbuttontitle:@ "determine" otherbuttontitles:nil];
[Alert show];
[Alert release];
self.isreachable = YES; }
}

Then on each page the Viewwillappear: Plus:

-(void) Viewwillappear: (BOOL) animated{
[Super Viewwillappear:yes];
Appdelegate *appdlg = (appdelegate *) [[uiapplication sharedapplication] delegate];
if (appdlg.isreachable) {
NSLog (@ "Network Connected");//code when the network is normal
else{
NSLog (@ network connection exception);//code to execute network exception
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Network connection exception" Message:nil delegate:nil \ cancelbuttontitle:@ "OK "Otherbuttontitles:nil];
[Alert show];
[Alert release];
}

This allows you to check for abrupt interruptions and connections to the network when you run the program. The Reachability class is actually an apple encapsulation of the Scnetworkreachability API, which is defined in the Systemconfigure.framework library. If there are other special needs, you can also use this native scnetworkreachability class directly.

II: afnetworking Monitoring

1. Import frame, and header file #import <AFNetworkReachabilityManager.h>

2. Code:

-(void) afn{
//1. Create Network State monitoring Manager
Afnetworkreachabilitymanager *manger = [Afnetworkreachabilitymanager Sharedmanager];
Open the monitor, remember to open, otherwise do not go block
[Manger startmonitoring];
2. Monitoring changes
[Manger setreachabilitystatuschangeblock:^ (Afnetworkreachabilitystatus status) {/
*
Afnetworkreachabilitystatusunknown =-1,
afnetworkreachabilitystatusnotreachable = 0,
Afnetworkreachabilitystatusreachableviawwan = 1,
Afnetworkreachabilitystatusreachableviawifi = 2,
*/
switch (status) {case
Afnetworkreachabilitystatusunknown:
NSLog (@ "Unknown");
break;
Case afnetworkreachabilitystatusnotreachable:
NSLog (@ "no network");
break;
Case Afnetworkreachabilitystatusreachableviawwan:
NSLog (@ "3g|4g");
break;
Case Afnetworkreachabilitystatusreachableviawifi:
NSLog (@ "WiFi");
break;
Default: Break;}}
];

The above is a small set to introduce the iOS detection network status of two methods, we hope to help, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.