The 1.iOS platform is designed according to the idea that there is always a network connection, and developers use this feature to create a number of excellent third-party applications. Most iOS apps need to be networked, and even some applications are heavily dependent on the network, and no network is working properly.
2. Before your app tries to get data over the network, you need to know whether the current device is connected to the network, or even when you might want to know if the current network is provided by WiFi or mobile cellular networks.
3. "The application did not make the appropriate prompt when the network access failed" is a common reason for Apple's iOS audit team to reject an app. Apple requires that you check the status of your network connection, notify the user in some way when the network is unavailable, or handle it in an elegant way.
***********************
Reachability class:
1. This class is used to detect the current network status, which is not part of the SDK and can be found in the iOS Developer library.
Download the Reachability.zip file from the Apple website and unzip it.
2. Reusing reachability classes
(1) Drag the Reachability.h and reachability.m files to the project.
(2) Add frame: systemconfiguration.framework.
3. Synchronization of Reachability
(1) The way to use synchronization is relatively simple, import the Reachability.h header file, and then check the network through code:
#import "Reachability.h"
。。。 Some code omitted ...
reachability *reach = [reachability reachabilityforinternetconnection];
NetworkStatus status = [reach Currentreachabilitystatus];
(2) Determine if the current network is available by checking that a host can access it:
reachability *reach = [reachability reachabilitywithhostname:@ "www.apple.com"];
NetworkStatus status = [reach Currentreachabilitystatus];
(3) Case:
Create a project and add Reachability.h and REACHABILITY.M to the project, and link the systemconfiguration.framework.
Import the Reachability.h in the AppDelegate.h header file and add an instance method.
This is achieved in APPDELEGATE.M:
4. Asynchronous reachability
(1) The asynchronous approach is slightly more complex, but in this way you can subscribe to real-time network status change notifications. Import the Reachability.h header file, and then register an object to subscribe to the network status change information, the network status change information name is kreachabilitychanged-notification.
[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (reachabilitychanged:)
Name:kreachabilitychangednotification
Object:nil];
(2) You need to create a Reachability object instance and start publishing messages to the network state changes:
reachability *reach = [[reachability reachabilitywithhostname:@ "www.apple.com"] retain];
[Reach Startnotifier];
(3) When the network state changes, the Reachability object will call the Reachabilitychanged: method, you can get the current network state in this method, and then do the 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 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.
iOS network--Detect network status: reachability