In the network application, the network status of the user device needs to be monitored in real time to
Let users know their network status, to prevent some misunderstanding (such as strange application is not good)
Intelligent processing based on the user's network status, saving user traffic and improving the user experience
WIFI\3G network: Automatically download HD images
Low-speed network: Download thumbnails only
No network: Show only cached data that is offline
1. Using the afnetworking framework
-(void ) afnmonitoring { Afnetworkreachabilitymanager *manager = [Afnetworkreachabilitymanager Sharedmanager]; [Manager setreachabilitystatuschangeblock: ^ @ " ------current network status%ZD ,status); }]; [Manager startmonitoring];}
typedef ns_enum (Nsinteger, afnetworkreachabilitystatus) { Afnetworkreachabilitystatusunknown =- 1,// Unknown network afnetworkreachabilitystatusnotreachable 0,// No network 1,// cellular network 2,//WiFi network }
2. Use Apple's official reachability
:
Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
- (void) Getnetworkstatus {if([reachability reachabilityforlocalwifi].currentreachabilitystatus! =notreachable) {NSLog (@"is WiFi network"); }Else if([reachability reachabilityforinternetconnection].currentreachabilitystatus! =notreachable) {NSLog (@"It's a cellular network ."); }Else{NSLog (@"Network Failure"); }}
Long-term monitoring of network status
-(void) applymonitoring { // Listener notification object: nil]; // Start monitoring the network self.reachability = [reachability reachabilityforinternetconnection]; [Self.reachability startnotifier];}
Cancel a task
-(void) dealloc{ [self.netreachability stopnotifier]; Object : nil];}
Detecting Network status