iOS network-afnetworking detect network status

Source: Internet
Author: User

可以使用AFN框架中的AFNetworkReachabilityManager来监听网络状态的改变,也可以利用苹果提供的Reachability来监听。建议在开发中直接使用AFN框架处理。

The sample code is as follows:

-(void) afn{//1. Create a network status monitoring managerAfnetworkreachabilitymanager *manger =[Afnetworkreachabilitymanager Sharedmanager]; //2. Monitoring Changes[Manger setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {/*Afnetworkreachabilitystatusunknown =-1, afnetworkreachabilitystatusnotreachable = 0, Afnetworkreachabilitystatusreachableviawwan = 1, Afnetworkreachabilitystatusreachableviawifi = 2, 
     */        Switch(status) { CaseAfnetworkreachabilitystatusunknown:nslog (@"Unknown");  Break;  CaseAfnetworkreachabilitystatusnotreachable:nslog (@"No network");  Break;  CaseAfnetworkreachabilitystatusreachableviawwan:nslog (@"3g|4g");  Break;  CaseAfnetworkreachabilitystatusreachableviawifi:nslog (@"WiFi");  Break; default:                 Break; }    }];

Reachablity is a library of iOS device network environments that detects iOS devices.

Apple's official doc,: http://developer.apple.com/library/ios/#samplecode/reachability/introduction/intro.html

The sample code is as follows:because reachablity can only listen to the status of the network status, can not listen to its changes, so the way to use the notification
-(void) viewdidload{[Super Viewdidload]; //Registration Notice[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitystatuschange) Name: KreachabilitychangednotificationObject: nil]; //Creating Objectsreachability *r =[reachability reachabilityforinternetconnection];    [R Startnotifier]; SELF.R=R;}-(void) reachabilitystatuschange{if([reachability Reachabilityforinternetconnection].currentreachabilitystatus = =Reachableviawwan) {NSLog (@"3g|4g"); return; }    if([reachability Reachabilityforlocalwifi].currentreachabilitystatus = =Reachableviawifi) {NSLog (@"WiFi"); return; } NSLog (@"Unknown");}-(void) dealloc{[[Nsnotificationcenter defaultcenter]removeobserver:self]; [SELF.R stopnotifier];}

iOS network-afnetworking detect 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.