IOS can obtain the network type in four ways, and ios can obtain the network type in four ways.

Source: Internet
Author: User

IOS can obtain the network type in four ways, and ios can obtain the network type in four ways.

ReachabilityOnly the WIFI and WWAN types can be distinguished, but the 2G and 3G networks cannot be distinguished.

There are also some methods on the Internet, but there are bugs.

After searching for materials and testing on the Internet, the following methods are summarized:

 

1. Use the navigation bar: (Private API)

Code:

typedef enum {    NetWorkType_None = 0,    NetWorkType_WIFI,    NetWorkType_2G,    NetWorkType_3G,} NetWorkType;

UIApplication *application = [UIApplication sharedApplication];NSArray *subviews = [[[application valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];NSNumber *dataNetWorkItemView = nil;for (id subView in subviews) {  if ([subView isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {            dataNetWorkItemView = subView;            break;        }    }    NetWorkType networkType = NetWorkType_None;    switch ([[dataNetWorkItemView valueForKey:@"dataNetworkType"] integerValue]) {        case 0:            NSLog(@"No wifi or cellular");            networkType = NetWorkType_None;            break;                    case 1:            NSLog(@"2G");            networkType = NetWorkType_2G;            break;                   case 2:            NSLog(@"3G");            networkType = NetWorkType_3G;            break;                  default:                       NSLog(@"Wifi");            networkType = NetWorkType_WIFI;            break;    }
return networkType;

  

To use this method, make sure that the navigation bar is not hidden. If the navigation bar is hidden, no value is obtained. In addition, this method has a Bug. When the network type changes through the Reachability class, the code is executed again. The obtained network type data is not updated. If the program enters the background and then goes to the foreground, you can obtain the latest network type data again when you re-execute the code. In this way, the user experience is poor. When the user is moving, the network type changes and the user cannot obtain the latest network type, the page data cannot be updated. (PS: if anyone knows how to refresh the data value, please kindly advise. Thank you !)

 

2. Use the SCNetworkReachability class

Code:

Struct sockaddr_in zeroAddress; bzero (& zeroAddress, sizeof (zeroAddress); zeroAddress. sin_len = sizeof (zeroAddress); zeroAddress. sin_family = AF_INET; SCNetworkReachabilityRef defaultRouteReachability = upper (NULL, (struct sockaddr *) & zeroAddress); // reference for creating test connections: lower flags; lower (defaultRouteReachability, & flags ); if (fl Ags & kSCNetworkReachabilityFlagsReachable) = 0) {return NetWorkType_None;} NetWorkType retVal = NetWorkType_None; if (flags & tags) = 0) {retVal = NetWorkType_WIFI ;} if (flags & kscnetworkreachabilityflagsconnectiondemand )! = 0) | (flags & kscnetworkreachabilityflagsconnectiontraffic )! = 0) {if (flags & extensions) = 0) {retVal = NetWorkType_WIFI;} if (flags & kSCNetworkReachabilityFlagsIsWWAN) = kSCNetworkReachabilityFlagsIsWWAN) {if (flags & tags) = kSCNetworkReachabilityFlagsReachable) {if (flags & tags) = Response) {retVal = NetWorkType_3G; if (flags & tags) = response) {retVal = NetWorkType_2G ;}}} return retVal;

This method is the same as the Reachability method, and cannot distinguish between 2G network and 3G network. However, some people can test it on the Internet. If someone knows the reason, I hope to correct it. Thank you!

 

3. Use SoftwareUpdateService. framework (Private API)

Preparations:

Export the header file Declaration for generating private APIs
When using private or undisclosed APIs, you must first export the corresponding header file and include the declaration of related functions in the header file.
Tools:
Class-dump
Class-dump can extract the corresponding data structure and function declaration from the compiled Objective-C binary file.
Usage:
To use the class-dump command in any directory, we recommend that you copy the class-dump file to the/user/local/bin/directory, then you can execute the following command in any directory:
Class-dump/Developer/Platforms/iPhoneSimulator. platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/Frameworks/SoftwareUpdateServices. framework/> SUKit. h

However, when bulidsetting sets the search path of the framework, it is necessary to set the actual private library path, because we need to use its executable file, only the header file will not work.

Code:

NSBundle *b =  [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/SoftwareUpdateServices.framework"];     if ([b load]) {        // load Class from STRING      SUNetworkMonitor *networkMonitor = [SUNetworkMonitor sharedInstance];         int netType = [networkMonitor currentNetworkType];         NetWorkType networkType = NetWorkType_None;         switch ( netType ) {             case 0:                 NSLog(@"No wifi or cellular");                 networkType = NetWorkType_None;                 break;             case 1:                 NSLog(@"WIFI");                 networkType = NetWorkType_WIFI;                break;             case 2:                 NSLog(@"2G");                 networkType = NetWorkType_2G;                break;                             case 3:                 NSLog(@"3G");                 networkType = NetWorkType_3G;                 break;             default:                 break;        }
return networkType; } return NetWorkType_None;

  

This method can obtain the network type and obtain the latest network type when the network type changes. No bugs have been found, but the preparation phase is troublesome.

 

4. The last method is to use the API provided by IOS7. However, the above method is required to obtain the network type for systems earlier than iOS 7.

Related Article

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.