iOS Development--Network Programming OC Chapter & (v) Network monitoring

Source: Internet
Author: User

With the mobile network upgrade: 2G->3G->4G even the development of 5G, the network speed is more and more fast, but this flow is like flowing water generally rushing away.

Online is not a joke: sleep forgot to close the flow, the next day the house will be moved!

This is an exaggeration to say, but as a good app developer, we need to give the most appropriate hints in the app.

Give some hints when you need to spend a lot of traffic (for example: video, downloads, etc.).

When users switch the network during user video, give the prompt to continue watching!

The following author gives a real-time monitoring of the network status change Method!

I use the network to spread the widest network monitoring class: Reachability

Click here to download this demo: Click to download

Unzip the files downloaded above and add the Networkreachability folder to the project you need to add.

Example Description: This example demonstrates how to use the Systemconfiguratio Reachalility program interface to determine the associated network connection status and state changes, so use the reachability in this example. (h/m) file in its own project is premised on the need to introduce systemconfiguration.framework.

One: How to use reachability

 1-(void) Viewdidload 2 {3 [Super Viewdidload]; 4 5//monitor network status change Notification 6 [[Nsnotificationcenter Defaultce Nter] addobserver:self selector: @selector (networkstatechange) name:kreachabilitychangednotification Object:nil]; 7 8//Get Reachability object 9 self.reachability = [reachability reachabilityforinternetconnection];10//Start monitoring Network one by one [self.reachability startnotifier];12 13////1. Get Reachability object//reachability *wifi = [reachability re ACHABILITYFORLOCALWIFI];15//16////2. Gets the current network status of the Reachability object.//NetworkStatus Wifistatus = WIFI.CURRENTREAC HABILITYSTATUS;18/if (wifistatus! = notreachable) {//NSLog (@ "WiFi");//}21}22-(void) dealloc2 4 {self.reachability stopnotifier];26 [[Nsnotificationcenter Defaultcenter] removeobserver:self];27}28 29-( void) NetworkStateChange30 {NSLog (@ "network status changed"); [Self checknetworkstate];33}34-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) EVent36 {PNs [self checknetworkstate];38}39 40/**41 * Monitor network status */43-(void) checkNetworkState44 {if ([icocosn Etworktool Isenablewifi]) {NSLog (@ "Wi-Fi Environment"); (+) else if ([Icocosnetworktool isenable3g]) {NSLog (@ "Mobile phone comes with network"); NSLog} else {(@ "no network"); 51}52}

Judge

1//whether WIFI2 + (BOOL) Isenablewifi {3     return ([[[Reachability Reachabilityforlocalwifi] currentreachabilitystatus]! = notreachable); 4}5 6//whether 3g7 + (BOOL) isenable3g {8     return ([[Reachability reachabilityforinternetconnection] Currentreachabilitystatus]! = notreachable); 9}

Second: The use of AFN: (AFN is actually using the system from the reachability to achieve)

 1-(void) Viewdidload 2 {3 [Super Viewdidload]; 4 5//Do no additional setup after loading the view, Typi Cally from a nib. 6 7 Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanager Sharedmanager];         8 [Mgr setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {9//Call this block10 when the network status changes) Switch (status) {one case afnetworkreachabilitystatusreachableviawifi:12 NSLog (@ "WIFI");                 BREAK;14 Case Afnetworkreachabilitystatusreachableviawwan:16 NSLog (@ "Bring your Own Network"); break;18 case Afnetworkreachabilitystatusnotreachabl E:20 NSLog (@ "No network"), Break;22 case Afnetworkreachabilitys          Tatusunknown:24 NSLog (@ "Unknown network"); break;26 default:27 break;28 }29}];30//Start Supervisorcontrol [Mgr Startmonitoring];32//[[reachability reachabilityforlocalwifi] currentreachabilitystatus] =! NotReachable34//[[reachability reachabilityforinternetconnection] currentreachabilitystatus] =! notreachable;35 [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (reacgabilitychange:) Name:kreachabilitychangednotification object:nil];37}39-(void) dealloc41 {[Afnetworkreachabilitymanage R Sharedmanager] Stopmonitoring];43}

With the mobile network upgrade: 2G->3G->4G even the development of 5G, the network speed is more and more fast, but this flow is like flowing water generally rushing away.

Online is not a joke: sleep forgot to close the flow, the next day the house will be moved!

This is an exaggeration to say, but as a good app developer, we need to give the most appropriate hints in the app.

Give some hints when you need to spend a lot of traffic (for example: video, downloads, etc.).

When users switch the network during user video, give the prompt to continue watching!

The following author gives a real-time monitoring of the network status change Method!

I use the network to spread the widest network monitoring class: Reachability

Click here to download this demo: Click to download

Unzip the files downloaded above and add the Networkreachability folder to the project you need to add.

Example Description: This example demonstrates how to use the Systemconfiguratio Reachalility program interface to determine the associated network connection status and state changes, so use the reachability in this example. (h/m) file in its own project is premised on the need to introduce systemconfiguration.framework.

One: How to use reachability

 1-(void) Viewdidload 2 {3 [Super Viewdidload]; 4 5//monitor network status change Notification 6 [[Nsnotificationcenter Defaultce Nter] addobserver:self selector: @selector (networkstatechange) name:kreachabilitychangednotification Object:nil]; 7 8//Get Reachability object 9 self.reachability = [reachability reachabilityforinternetconnection];10//Start monitoring Network one by one [self.reachability startnotifier];12 13////1. Get Reachability object//reachability *wifi = [reachability re ACHABILITYFORLOCALWIFI];15//16////2. Gets the current network status of the Reachability object.//NetworkStatus Wifistatus = WIFI.CURRENTREAC HABILITYSTATUS;18/if (wifistatus! = notreachable) {//NSLog (@ "WiFi");//}21}22-(void) dealloc2 4 {self.reachability stopnotifier];26 [[Nsnotificationcenter Defaultcenter] removeobserver:self];27}28 29-( void) NetworkStateChange30 {NSLog (@ "network status changed"); [Self checknetworkstate];33}34-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) EVent36 {PNs [self checknetworkstate];38}39 40/**41 * Monitor network status */43-(void) checkNetworkState44 {if ([icocosn Etworktool Isenablewifi]) {NSLog (@ "Wi-Fi Environment"); (+) else if ([Icocosnetworktool isenable3g]) {NSLog (@ "Mobile phone comes with network"); NSLog} else {(@ "no network"); 51}52}

Judge

1//whether WIFI2 + (BOOL) Isenablewifi {3     return ([[[Reachability Reachabilityforlocalwifi] currentreachabilitystatus]! = notreachable); 4}5 6//whether 3g7 + (BOOL) isenable3g {8     return ([[Reachability reachabilityforinternetconnection] Currentreachabilitystatus]! = notreachable); 9}

Second: The use of AFN: (AFN is actually using the system from the reachability to achieve)

 1-(void) Viewdidload 2 {3 [Super Viewdidload]; 4 5//Do no additional setup after loading the view, Typi Cally from a nib. 6 7 Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanager Sharedmanager];         8 [Mgr setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {9//Call this block10 when the network status changes) Switch (status) {one case afnetworkreachabilitystatusreachableviawifi:12 NSLog (@ "WIFI");                 BREAK;14 Case Afnetworkreachabilitystatusreachableviawwan:16 NSLog (@ "Bring your Own Network"); break;18 case Afnetworkreachabilitystatusnotreachabl E:20 NSLog (@ "No network"), Break;22 case Afnetworkreachabilitys          Tatusunknown:24 NSLog (@ "Unknown network"); break;26 default:27 break;28 }29}];30//Start Supervisorcontrol [Mgr Startmonitoring];32//[[reachability reachabilityforlocalwifi] currentreachabilitystatus] =! NotReachable34//[[reachability reachabilityforinternetconnection] currentreachabilitystatus] =! notreachable;35 [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (reacgabilitychange:) Name:kreachabilitychangednotification object:nil];37}39-(void) dealloc41 {[Afnetworkreachabilitymanage R Sharedmanager] Stopmonitoring];43}

iOS Development--Network Programming OC Chapter & (v) 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.