Determine network connectivity

Source: Internet
Author: User

Prerequisite: Import ASIHTTPRequest--reachability

This method relies on the ability to open a set URL to determine whether the current network is disconnected or connected and what type of network is being determined
reachability *reachability =[reachability reachabilitywithhostname:@ "www.baidu.com"];

To obtain the current network status:
NetworkStatus status =[reachability Currentreachabilitystatus];
Switch (status) {
Case Knotreachable:
NSLog (@ "No network connection");
Break
Case Kreachableviawwan:
NSLog (@ "mobile network");

Break
Case Kreachableviawifi:
NSLog (@ "WiFi");

Break
Default
Break
}

Online Reprint--never tried.


Get iOS 2G 3G 4G network-like
Nsarray *subviews = [[[[UIApplication Sharedapplication] valueforkey:@ "StatusBar"] valueforkey:@ "Foregroundview"] Subviews];
NSNumber *datanetworkitemview = nil;
//
For (ID subview in subviews) {
if ([Subview iskindofclass:[nsclassfromstring (@ "Uistatusbardatanetworkitemview") class]) {
Datanetworkitemview = Subview;
Break
//        }
//    }
switch ([[Datanetworkitemview valueforkey:@ "Datanetworktype"]integervalue]) {
Case 0:
NSLog (@ "No WiFi or cellular");
Break
//
Case 1:
NSLog (@ "2G");
Break
//
Case 2:
NSLog (@ "3G");
Break
//
Case 3:
NSLog (@ "4G");
Break
//
Case 4:
NSLog (@ "LTE");
Break
//
Case 5:
NSLog (@ "Wifi");
Break
//
Default
Break
//    }

Determine network connectivity

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.