iOS Development Practice Network detection reachability

Source: Internet
Author: User

in the development of Web applications. In some cases, real-time monitoring of the network status of the user device is required. So that users are friendly to the user or according to different network status of the same logic (such as video playback app, according to the current network conditions to actively switch video clarity, etc.). Using reachability to realize network detection.

Apple's official offer reachability Demo sample program to enable developers to detect network status

Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip


1. Network Status Enumeration NetworkStatus:

notreachable = 0, // No network

Reachableviawifi, //wi-fi Network

Reachableviawwan // Mobile Network (non- Wi-Fi )


2. reachability Frequent usage:

/*! * via host instantiation reachability */+ (instancetype) Reachabilitywithhostname: (NSString *) hostname;/*! * Instantiation of Reachability */+ (instancetype) reachabilitywithaddress by IP address: (const struct sockaddr_in *) hostaddress;/*! * Get network Connection object */+ (instancetype) reachabilityforinternetconnection;/*! * Get Wi-Fi link object */+ (instancetype) reachabilityforlocalwifi;/*! * Monitoring Network Change method */-(BOOL) startnotifier; Start monitoring-(void) stopnotifier; Stop listening//current network connection Status-(NetworkStatus) currentreachabilitystatus;

3. Monitor network changes: Kreachabilitychangednotification

3.1. Notification of network status

[[ nsnotificationcenter defaultcenter]addobserver : Self selector: @selector ( Networkstatuschange)name:kreachabilitychangednotification Object : Nil ];

3.2. get reachability object

self . reachability = [reachability reachabilityforinternetconnection];

3.3. Start listening for network changes

[self. reachability Startnotifier] ;

3.4. Turn off notifications and release objects

-(void) dealloc{

[self. reachability stopnotifier];

[[nsnotificationcenter defaultcenter]removeobserver :self];

}


4. reachability the use steps

4.1. Join the framework Systemconfiguration.framework (Xocde5 to join after the initiative)

4.2. Introduction Source Code

4.3. import header file

#import "Reachability.h"


4.4. assume that reachability performs an arc error.

The source code sets the ARC compilation environment (now the latest download reachability is arc mode).

assume that your project uses a non-ARC mode. The-FOBJC-ARC tag is added for the ARC mode code file.

If your project uses ARC mode, add the-FNO-OBJC-ARC tag to the code file for non-ARC mode.



5. Chestnuts:

networktool.m

#import "NetWorkTool.h" #import "Reachability.h" @implementation networktool//Check if Wi-Fi network + (BOOL) isenablewifi{   return  [[reachability Reachabilityforlocalwifi] currentreachabilitystatus]! = notreachable; Check if mobile network + (BOOL) isenablewwan{    //return [[reachability Reachabilityforlocalwifi] currentreachabilitystatus]! = notreachable;    return [[reachability reachabilityforinternetconnection] currentreachabilitystatus] = = Reachableviawwan;} @end

Viewcontroller.m

#import "ViewController.h" #import "Reachability.h" #import "NetWorkTool.h" @interface Viewcontroller () @property ( Nonatomic,strong) reachability *reachability, @end @implementation viewcontroller-(void) Viewdidload {[Super        Viewdidload]; Register network status Notification [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (networkstatuschange) Name:        Kreachabilitychangednotification Object:nil];        Get reachability Object self.reachability = [reachability reachabilityforinternetconnection]; Start listening for network changes [self.reachability Startnotifier];}    Turn off notifications and release objects-(void) dealloc{[self.reachability Stopnotifier]; [[Nsnotificationcenter Defaultcenter] removeobserver:self];}    Network Change-(void) networkstatuschange{NSLog (@ "The current network has changed"); [Self checkcurnetwork];}    Detect network-(void) checkcurnetwork{if ([Networktool Isenablewifi]) {NSLog (@ "Current network is a Wi-Fi network");    }else if ([Networktool Isenablewwan]) {NSLog (@ "Current network is mobile network");    }else{NSLog (@ "No Internet connection"); }}






iOS Development Practice Network detection reachability

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.