iOS Development Practice Network detection reachability

Source: Internet
Author: User

in the development of network applications, it is sometimes necessary to monitor the network status of the user's devices in real time, so that users can be friendly or different logic (such as video playback app, automatically switch video definition according to the current network condition). Using reachability to realize network detection.

Apple's official offer reachability 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 common methods:

/*! * 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 listening-(void) stopnotifier; Stop listening//current network connection Status-(NetworkStatus) currentreachabilitystatus;

3. Monitor network changes: Kreachabilitychangednotification

3.1, registration network status Notification

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

3.2. get reachability object

self . reachability = [reachabilityreachabilityforinternetconnection];

3.3. Start listening for network changes

[self. reachability Startnotifier] ;

3.4. Turn off notifications and release objects

-(void) dealloc{

[self. reachability stopnotifier];

[[nsnotificationcenterdefaultcenter]removeobserver :self];

}


4. reachability the use steps

4.1. Add frame systemconfiguration.framework (automatically added after Xocde5)

4.2. Introduction Source Code

4.3. import header file

#import "Reachability.h"


4.4. If reachability run the report arc error. The source Set ARC compilation environment (currently the latest download reachability is arc mode).

If your project uses a non-arc mode, add the-FOBJC-ARC tag to the ARC mode code file.

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



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.