How to determine the current network connection status (network is OK)

Source: Internet
Author: User

As we all know, in the development of the app, when involved in the network connection, will be thinking ahead of time to determine the current network connection status, if there is no network, no longer request the URL, eliminating unnecessary steps, so, this how to judge? It's actually very simple.

Premise: Project add: Systemconfiguration.framework

Then include the header file in the class that needs to be judged:

#import "Reachability.h"

Here's a way I wrote:

#pragma mark-Detect network Connections

+ (BOOL) isconnectionavailable

{

BOOL isexistencenetwork = YES;

Ddreachability *reach = [ddreachability reachabilitywithhostname:@ "www.apple.com"];

switch ([reach Currentreachabilitystatus]) {

Case Notreachable:

Isexistencenetwork = NO;

NSLog (@ "notreachable");

Break

Case Reachableviawifi:

Isexistencenetwork = YES;

NSLog (@ "WIFI");

Break

Case Reachableviawwan:

Isexistencenetwork = YES;

NSLog (@ "3G");

Break

}

if (!isexistencenetwork) {

Uialertview *alertview = [[Uialertview alloc] Initwithtitle:nil message:@ "The current network is not available, please check the network connection!" Delegate:nil cancelbuttontitle:@ "determine" otherbuttontitles:nil];

[Alertview show];

return NO;

}else{

return isexistencenetwork;

}

}

Then in the place where you need to judge directly:

Can be networked to request data

if ([self isconnectionavailable]) {

}

You see, it's so simple.

So extrapolate, if you are not only to judge whether the network unobstructed, but to judge is WiFi or 3G, and then write a Isenablewifi method, the specific method of judgment will not have to repeat it, currentreachabilitystatus judgment.

How to determine the current network connection status (network is OK)

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.