iOS Development Network-monitoring network status

Source: Internet
Author: User

iOS Development Network-monitoring network status

I. Description

In the network application, the network status of the user device needs to be monitored in real time for two purposes:

(1) Let users know their network status, to prevent some misunderstandings (such as the application of incompetence)

(2) According to the user's network status intelligent processing, save user traffic, improve 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

Apple officially provides a sample program called Reachability, which allows developers to detect network status

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

Second, monitoring network status

Reachability Steps to use

Add Frame Systemconfiguration.framework

Add source code

 

Include header file

#import "Reachability.h"

code example:

1 #import "YYViewController.h"2 #import "Reachability.h"3 4 @interfaceYyviewcontroller ()5@property (nonatomic, strong) reachability *Conn;6 @end7 8 @implementationYyviewcontroller9 Ten- (void) Viewdidload One { A [Super Viewdidload]; -      -[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: KreachabilitychangednotificationObject: nil]; theSelf.conn =[reachability reachabilityforinternetconnection]; - [Self.conn Startnotifier]; - } -  +- (void) Dealloc - { + [Self.conn Stopnotifier]; A [[Nsnotificationcenter Defaultcenter] removeobserver:self]; at } -  -- (void) Networkstatechange - { - [self checknetworkstate]; - } in  -- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event to { +  - } the  *- (void) Checknetworkstate $ {Panax Notoginseng     //1. Detect WiFi status -reachability *wifi =[reachability Reachabilityforlocalwifi]; the      +     //2. Check whether the mobile phone can be on the network (wifi\3g\2.5g) Areachability *conn =[reachability reachabilityforinternetconnection]; the      +     //3. Determine network status -     if([WiFi currentreachabilitystatus]! = notreachable) {//have wifi $NSLog (@"have wifi"); $          -}Else if([conn currentreachabilitystatus]! = notreachable) {//no WiFi, Internet access using your phone's own network -NSLog (@"Use your phone to bring your own network to the Internet"); the          -}Else{//No networkWuyi          theNSLog (@"No network"); -     } Wu } - @end About  $ //with WiFi - //[WiFi currentreachabilitystatus]! = notreachable - //[conn currentreachabilitystatus]! = notreachable -  A //no wifi, only mobile phone network + //[WiFi currentreachabilitystatus] = = Notreachable the //[conn currentreachabilitystatus]! = notreachable -  $ //No network the //[WiFi currentreachabilitystatus] = = Notreachable the //[conn Currentreachabilitystatus] = = Notreachable

iOS Development Network-monitoring 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.