How IOS Gets the network type Rollup _ios

Source: Internet
Author: User

The Reachability class can only differentiate between WiFi and Wwan types, but cannot differentiate between 2G and 3G networks.

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

Through the Internet to find data and testing, basically summed up the following methods:

1, the way to use the navigation bar: (Private API)

Code:

Copy Code code as follows:

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 Cellul
      Ar ");
      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; 

Use this method to make sure that the navigation bar is not hidden and that if the navigation bar is hidden, the value is not taken. In addition, the method has a bug, through the Reachability class, when the network type changes, and then executes the code, the obtained network type data is not updated. If the program goes backstage and goes to the foreground, the code can be rerun to retrieve the latest network type data. So the user experience is not good, when the user in the mobile, the network type changes, cannot get the latest network type, the page data cannot be updated. PS: If the great God knows, what method can refresh the data value, look at the generous enlighten, thank you! )

2, through the Scnetworkreachability class

Code:

struct sockaddr_in zeroaddress;
 Bzero (&zeroaddress, sizeof (zeroaddress));
 Zeroaddress.sin_len = sizeof (zeroaddress);
 zeroaddress.sin_family = af_inet; Scnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress (NULL, struct sockaddr *) &zeroaddress);
 To create a reference to a test connection: scnetworkreachabilityflags flags;
Scnetworkreachabilitygetflags (defaultroutereachability, &flags);  
 if ((Flags & kscnetworkreachabilityflagsreachable) = = 0) {return networktype_none;
 } networktype retVal = Networktype_none;
 if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = 0) {retVal = Networktype_wifi;  
    if (((Flags & Kscnetworkreachabilityflagsconnectionondemand)!= 0) | | (Flags & Kscnetworkreachabilityflagsconnectionontraffic)!= 0)) {if (Flags & Kscnetworkreachabilityflagsinte  
   rventionrequired) = = 0) {retVal = Networktype_wifi; } if ((Flags & kscnetworkreachabilityflagsiswwan) = = KscnetworkreAchabilityflagsiswwan) {if ((Flags & kscnetworkreachabilityflagsreachable) = = kscnetworkreachabilityflagsreachable) {if ((Flags & kscnetworkreachabilityflagstransientconnection) = = KSCNetwor
       kreachabilityflagstransientconnection) {retVal = networktype_3g;
         if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = kscnetworkreachabilityflagsconnectionrequired) {
       RetVal = networktype_2g; 
 }}} return retVal;

This method is the same as the Reachability method, can not distinguish between 2G and 3g nets, but people on the internet can be differentiated, if someone knows the reason, hope to correct, thank you!

3. Use Softwareupdateservice.framework (Private API)

Preparatory work:

To export a header file declaration that generates a private API
Using a private or Non-public API, you first need to export its corresponding header file, which has a 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.
How to use:
In order to be able to use the Class-dump command in any directory, it is recommended that you copy the Class-dump file to the/user/local/bin/directory, and then 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

But bulidsetting sets the true private library path when setting the framework's search path, because we want to use its executable file, only the header file is not.

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
         ;
    } <br> return
    networktype;
  }
  return networktype_none; 

This method can get the network type and get the latest network type when the network type changes. No bugs have been found yet, but the preparation phase of the work is more cumbersome.

4, the last method, is to use the IOS7 system with the API to obtain. Only the system version below IOS7 requires the method above to get the network type.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.