iOS Programming-Network monitoring

Source: Internet
Author: User

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"

#import "QYViewController.h"

#import "Reachability.h"

@interface qyviewcontroller ()

@property (nonatomic,strong) reachability *conn;

@end


@implementation Qyviewcontroller


-(void) viewdidload

{

[superviewdidload];

Additional setup after loading the view, typically from a nib.

    [[ nsnotificationcenter Defaultcenter] addobserver: self selector: @selector (networkstatechange) name: kreachabilitychangednotification object: nil ];

self. Conn = [reachabilityreachabilityforinternetconnection];

[self. ConnStartnotifier];

}


-(void) Dealloc

{

[self. ConnStopnotifier];

[[nsnotificationcenterdefaultcenter] removeobserver:self];

}


-(void) Networkstatechange

{

[self checknetworkstate];

}


-(void) checknetworkstate

{

//1. detecting wifi status

reachability *wifi = [reachabilityreachabilityforlocalwifi];

//2. detect if the phone can be on the network (wifi\3g\2.5g)

reachability *conn = [reachabilityreachabilityforinternetconnection];


//3. Determine network status

if ([WiFicurrentreachabilitystatus]! = notreachable) {// Have WiFi

NSLog (@ " wifi");

} elseif ([conn currentreachabilitystatus]! = notreachable ) {// do not use WiFi, Use your phone to bring your own network to the Internet

NSLog (@ " use your phone to bring your own network to the internet ");

}Else { // No network

NSLog (@ " no network ");

}

}


iOS Programming-Network monitoring

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.