IOS Network -- Check Network Status: Reachability, iosreachability

Source: Internet
Author: User

IOS Network -- Check Network Status: Reachability, iosreachability

1. the iOS platform is designed based on the concept of network connection. With this feature, developers have created many excellent third-party applications. Most iOS applications need to be connected to the Internet. Even some applications are heavily dependent on the network and cannot work normally without the network.

2. before your application tries to obtain data through the network, you need to know whether the current device is connected to the network, you may even need to know whether the current network is provided by Wi-Fi or mobile cellular networks.

3. "When network access fails, the app does not make a proper prompt" is a common reason why Apple's iOS Review Team rejects an app. Apple requires that you first check the network connection status. When the network is unavailable, inform the user in some way, or use other elegant methods for processing.

 

***********************

Reachability class:

1. This class is used to check the current network status. It is not part of the SDK and can be found in the iOS Developer Library.

Download the reachability.zip file from the apple site and decompress it.

2. Reuse the Reachability class

(1) drag the Reachability. h and Reachability. m files to the project.

(2) Add the framework SystemConfiguration. framework.

3. Synchronized Reachability

(1) The synchronization method is relatively simple. Import the Reachability. h header file, and then check the network through the Code:

# Import "Reachability. h"

... Some code omitted...

Reachability * reach = [Reachability reachabilityForInternetConnection];

NetworkStatus status = [reach currentReachabilityStatus];

(2) check whether a host can be accessed to determine whether the current network is available:

Reachability * reach = [Reachability reachabilityWithHostName: @ "www.apple.com"];

NetworkStatus status = [reach currentReachabilityStatus];

(3) case:

Create a project, add Reachability. h and Reachability. m to the project, and link SystemConfiguration. framework.

Import Reachability. h In the header file of AppDelegate. h and add an instance method.

In AppDelegate. m, perform the following operations:

4. asynchronous Reachability

(1) The Asynchronous Method is slightly complicated, but this method can be used to subscribe to real-time network status change notifications. Import the Reachability. h header file, and register an object to subscribe to the network status change information. The network status change information name is kReachabilityChanged-Notification:

[[Nsicationcenter center defacenter center] addObserver: self

Selector: @ selector (reachabilityChanged :)

Name: kReachabilityChangedNotification

Object: nil];

(2) You need to create a Reachability object instance and start to publish a message about the network status change:

Reachability * reach = [[Reachability reachabilityWithHostName: @ "www.apple.com"] retain];

[Reach startNotifier];

(3) When the network status changes, the Reachability object will call the reachabilityChanged: method. You can obtain the current network status in this method and perform corresponding processing.

-(Void) reachabilityChanged :( NSNotification *) notification {

Reachability * reach = [notification object];

If ([reach isKindOfClass: [Reachability class]) {

NetworkStatus status = [reach currentReachabilityStatus];

// Insert your code here

}

}

 

****************************

5. Native Reachability API

The previous Reachability class is actually Apple's encapsulation of the SCNetworkReachability API, which is defined in the SystemConfigure. framework library. If you have other special requirements, you can also directly use the native SCNetworkReachability class.

 

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.