Method One (official):
Reachability
================================================================================
Frame Address: Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
How to use
1> Adding a networked status listener object
@property (nonatomic, strong) reachability *reach;
2> Instantiating listener objects
Self.reach = [reachability reachabilitywithhostname:@ "www.baidu.com"];
3> Determining the status of networking
-(void) netwrokstatuschanged {
Switch (self.reach.currentReachabilityStatus) {
Case Notreachable:
Sjlog (@ "No Internet connection");
Break
Case Reachableviawifi:
Sjlog (@ "internet via Wi-Fi");
Break
Case Reachableviawwan:
Sjlog (@ "via 3G Internet");
Break
Default
Break
}
}
4> real-time monitoring of network status using notification hubs
[[Nsnotificationcenter Defaultcenter] Addobserver:selfselector: @selector (netwrokstatuschanged) Name: Kreachabilitychangednotification Object:nil];
[Self.reach Startnotifier];
-(void) Dealloc {
Registers the specified notification listener
[[Nsnotificationcenter Defaultcenter] removeObserver:selfname:kReachabilityChangedNotification Object:nil];
}
method Two (AFN):
Managers who have access to network monitoring
Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanager Sharedmanager];
Set up processing after a change in network status
[Mgr setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {
Switch (status) {
Case Afnetworkreachabilitystatusunknown://Unknown network
Sjlog (@ "Unknown network");
Break
Case afnetworkreachabilitystatusnotreachable://No network (off-grid)
Sjlog (@ "No network detected");
Break
Case Afnetworkreachabilitystatusreachableviawwan://Phone comes with network
Sjlog (@ "via 3G Internet");
Break
Case Afnetworkreachabilitystatusreachableviawifi://WIFI
Sjlog (@ "internet via Wi-Fi");
Break
}
}];
Start monitoring
[Mgr Startmonitoring];
iOS Network monitoring method