IOS programming-Network Monitoring

Source: Internet
Author: User

IOS programming-Network Monitoring

 

In network applications, You need to monitor the network status of your devices in real time for two purposes:

(1) let users know their network status and prevent misunderstandings (for example, the application is incompetent)

(2) Smart processing based on the user's network status, saving user traffic and improving user experience

WIFIG Network: Automatically downloads HD Images

Low-speed network: only downloading thumbnails

No network: Only offline cached data is displayed.

Apple officially provides an example program called Reachability to help developers detect the network status.

Www.bkjia.com

 

2. Monitoring Network Status

Reachability usage steps

Add framework SystemConfiguration. framework

Add source code

 

Include header files

# Import Reachability. h

# Import QYViewController. h

# Import Reachability. h

@ Interface QYViewController ()

@ Property (nonatomic, strong) Reachability * conn;

@ End

 

@ Implementation QYViewController

 

-(Void) viewDidLoad

{

[Super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (networkStateChange) name: kReachabilityChangedNotification object: nil];

Self. conn = [Reachability reachabilityForInternetConnection];

[Self. conn startNotifier];

 

}

 

-(Void) dealloc

{

[Self. conn stopNotifier];

[[Nsicationcenter center defacenter center] removeObserver: self];

}

 

-(Void) networkStateChange

{

[Self checkNetworkState];

}


-(Void) checkNetworkState

{

// 1. Check the wifi status

Reachability * wifi = [Reachability reachabilityForLocalWiFi];

// 2. Check whether the mobile phone can be connected to the network (wifig.5 g)

Reachability * conn = [Reachability reachabilityForInternetConnection];

 

// 3. Determine the network status

If ([wifi currentReachabilityStatus]! = NotReachable) {// wifi available

NSLog (@ wifi available );

 

} Else if ([conn currentReachabilityStatus]! = NotReachable) {// no wifi is used. Use the network provided by your mobile phone to access the Internet.

NSLog (@ use your mobile phone to access the Internet );

 

} Else {// No network

NSLog (@ No network );

}

}


Related Article

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.