Detecting Network status

Source: Internet
Author: User

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

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.