iOS reachability and AFN determine network connection status

Source: Internet
Author: User

1. Reachability//Notification of monitoring network status changes[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: KreachabilitychangednotificationObject: nil];//Create reachabilitySelf.conn =[reachability reachabilityforinternetconnection];//Start monitoring the network (notify Kreachabilitychangednotification once the network status changes)[Self.conn Startnotifier];//Handling Network status changes- (void) networkstatechange{//1. Detect WiFi statusreachability *wifi =[reachability Reachabilityforlocalwifi]; //2. Check whether the mobile phone can be on the network (wifi\3g\2.5g)reachability *conn =[reachability reachabilityforinternetconnection]; //3. Determine network status    if([WiFi currentreachabilitystatus]! = notreachable) {//have wifiNSLog (@"have wifi"); } Else if([conn currentreachabilitystatus]! = notreachable) {//no WiFi, Internet access using your phone's own networkNSLog (@"Use your phone to bring your own network to the Internet"); } Else{//No networkNSLog (@"No network"); }}2. AFN//1. Managers who have access to network monitoringAfnetworkreachabilitymanager *mgr =[Afnetworkreachabilitymanager Sharedmanager];//2. Set up the processing after the network status change[Mgr setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {//when the network state changes, the block is called    Switch(status) { CaseAfnetworkreachabilitystatusunknown://Unknown NetworkNSLog (@"Unknown Network");  Break;  CaseAfnetworkreachabilitystatusnotreachable://No network (off-grid)NSLog (@"No network (off-grid)");  Break;  CaseAfnetworkreachabilitystatusreachableviawwan://mobile phone comes with networkNSLog (@"mobile phone comes with network");  Break;  CaseAfnetworkreachabilitystatusreachableviawifi://WIFINSLog (@"WIFI");  Break; }}];//3. Start monitoring[Mgr Startmonitoring];

iOS reachability and AFN determine network connection 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.