Open-source network programming Reachability application developed for IOS

Source: Internet
Author: User

First look at Reachability. h found

# Import <Foundation/Foundation. h>

# Import <SystemConfiguration/SystemConfiguration. h>

# Import <netinet/in. h>

So if we need to use this class in the project, we need to introduce SystemConfiguration. framework.

In ios network development, you can determine the network environment and connection conditions (no network connection, 3G, WIFI, GPRS)

Enum {// DDG NetworkStatus Constant Names.

KNotReachable = 0,
// Apple's code depends upon 'notreachable' being the same value as 'no '.

KReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.

KReachableViaWiFi

};

Define three network types:

I. kNotReachable no network connection

Ii. kReachableViaWWAN uses GPRS or 3G network connection

Iii. kReachableViaWiFi connection

I used the following statement in the project to determine whether a network connection exists.

BOOL reachable = [[Reachability
ReachabilityForInternetConnection] isReachable];

If (! Reachable ){

UIAlertView * alertView = [[UIAlertView
Alloc] initWithTitle: @ "this function can be used only when it is connected to the network. Check your network connection status." message: nil delegate: nil cancelButtonTitle: @ "OK"
OtherButtonTitles: nil]
Autorelease];

[AlertView show];

Return;

}

When there is a network request, the method in the class can return the current network connection status

Example: Reachable * reachable = [Reachable reachabilityWithHostName: @ "http://blog.csdn.net"];

Then we apply

// These are the status tests.

-(Networkstatus) currentreachabilitystatus; returns the network connection status.

Switch ([reachable currentreachabilitystatus]) {

/* [Reachable currentreachabilitystatus] contains three values

I. knotreachable no network connection

Ii. kreachableviawwan uses GPRS or 3G network connection

Iii. kreachableviawifi connection */

}

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.