Get network status, differentiate 2g3g4gwifi, etc., non-reachability

Source: Internet
Author: User


+(NSString *) getnetworkstates{
UIApplication*app = [UIApplication Sharedapplication];
Nsarray*children = [[[App Valueforkeypath]:@ "StatusBar"]valueforkeypath:@ "Foregroundview"]subviews];
NSString *state = [[NSStringAllocINIT];
int netType =0;
//Get to the network return code
for (ID ChildIn children) {
If([ChildIskindofclass:Nsclassfromstring(@ "Uistatusbardatanetworkitemview")]) {
//get to status bar
NetType = [[ChildValueforkeypath:@ "Datanetworktype"]Intvalue];

Switch (netType) {
Case0:
State =@"No network";
//No Network mode
Break
Case1:
State =@ "2G";
Break
Case2:
State =@ "3G";
Break
Case3:
State =@ "4G";
Break
Case                 {
      &NB Sp             state = @ "WIFI";
               }
                &NBSP ;   break;
                default:
      &N Bsp             break;
            {
       }
   }
//
    }
To obtain network status according to the status bar, can distinguish 2G, 3G, 4G, WIFI, System method, relatively fast. The only thing that hurts a little bit is if the Wi-Fi is not connected ... You know

Get network status, differentiate 2g3g4gwifi, etc., non-reachability

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.