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

Source: Internet
Author: User

Network Monitoring

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) Viewdidload2 {3 [Super Viewdidload];4     5     //listen for notification of changes in network status6[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: KreachabilitychangednotificationObject: nil];7     8     //get Reachability Object9Self.reachability =[reachability reachabilityforinternetconnection];Ten     //Start monitoring the network One [self.reachability Startnotifier]; A  - //    //1. Get Reachability object - //reachability *wifi = [reachability Reachabilityforlocalwifi]; the //     - //    //2. Get the current network status of the Reachability object - //networkstatus wifistatus = wifi.currentreachabilitystatus; - //if (wifistatus! = notreachable) { + //NSLog (@ "WiFi"); - //    } + } A  at- (void) Dealloc - { - [self.reachability Stopnotifier]; - [[Nsnotificationcenter Defaultcenter] removeobserver:self]; - } -  in- (void) Networkstatechange - { toNSLog (@"the network state has changed."); + [self checknetworkstate]; - } the  *- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event $ {Panax Notoginseng [self checknetworkstate]; - } the  + /** A * Monitor network status the  */ +- (void) Checknetworkstate - { $     if([Icocosnetworktool Isenablewifi]) { $NSLog (@"WiFi Environment"); -}Else if([Icocosnetworktool isenable3g]) { -NSLog (@"mobile phone comes with network"); the}Else { -NSLog (@"No network");Wuyi     } the}

Judge

1 //whether WiFi2+(BOOL) Isenablewifi {3     return([[Reachability Reachabilityforlocalwifi] currentreachabilitystatus]! =notreachable);4 }5 6 //is 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) Viewdidload2 {3 [Super Viewdidload];4     5     //additional setup after loading the view, typically from a nib.6     7Afnetworkreachabilitymanager *mgr =[Afnetworkreachabilitymanager Sharedmanager];8[Mgr setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {9         //Call this block when the network state changesTen         Switch(status) { One              CaseAfnetworkreachabilitystatusreachableviawifi: ANSLog (@"WIFI"); -                  Break; -                  the              CaseAfnetworkreachabilitystatusreachableviawwan: -NSLog (@"bring your Own network"); -                  Break; -                  +              Caseafnetworkreachabilitystatusnotreachable: -NSLog (@"No network"); +                  Break; A                  at              CaseAfnetworkreachabilitystatusunknown: -NSLog (@"Unknown Network"); -                  Break; -             default: -                  Break; -         } in     }]; -     //Start Monitoring to [Mgr Startmonitoring]; +      - //[[reachability reachabilityforlocalwifi] currentreachabilitystatus] =! Notreachable the //[[reachability reachabilityforinternetconnection] currentreachabilitystatus] =! notreachable; *      $[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reacgabilitychange:) Name: KreachabilitychangednotificationObject: nil];Panax Notoginseng      - } the  +- (void) Dealloc A { the [[Afnetworkreachabilitymanager Sharedmanager] stopmonitoring]; +}

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.