Use of runtime for iOS development-get the current network status

Source: Internet
Author: User

I have previously written something about runtime. This time, I used runtime to get something that Apple officially didn't want you to get, such as the control attributes inside the status bar. This article uses runtime to show you the control that displays the network status in the status bar step by step, and then monitors the properties of the control to obtain the current precise network status, for example, 2G/3G/4G/WIFI. First, we need to get the status bar, and then explore the internal structure of the status bar through runtime. 1. import the runtime header file # import <objc/message. h> 2. Compile the runtime code and obtain all member variables of the current application. Copy the Code 1 # import "ViewController. h "2 # import <objc/message. h> 3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidAppear :( BOOL) animated11 {12 // the status bar is controlled by the current app. First, obtain the current app13 UIApplication * app = [UIApplication sharedApplication]; 14. // traverse all attributes of the current app, locate 16 unsigned int outCount = 0; 17 18 Iva on the status bar R * ivars = class_copyIvarList (app. class, & outCount); 19 20 for (int I = 0; I <outCount; I ++) {21 Ivar ivar = ivars [I]; 22 printf ("| % s", ivar_getName (ivar); 23} 24} 25 26 @ end copy the code and run it directly. The printed result is: 3. You can see that the app does have a member variable about the status bar. We can use KVC to retrieve it and copy the code 1-(void) viewDidAppear :( BOOL) animated 2 {3 // the status bar is controlled by the current app. First, obtain the current app 4 UIApplication * app = [UIApplication sharedApplication]; 5 6 id statusBar = [app valueForKeyPa Th: @ "statusBar"]; 7 8 // traverse all members of the Status Bar 9 unsigned int outCount = 0; 10 Ivar * ivars = class_copyIvarList ([statusBar class], & outCount ); 11 12 for (int I = 0; I <outCount; I ++) {13 Ivar ivar = ivars [I]; 14 printf ("| % s ", ivar_getName (ivar); 15} 16} after the code is copied, the print result is displayed as 4. The foregroundView Member is in the status bar, which should represent all currently displayed views, use KVC to retrieve all the sub-views in it and copy code 1 // the status bar is controlled by the current app. first obtain the current app2 UIApplication * app = [UIApplication sharedApplicat Ion]; 3 4 NSArray * children = [[app valueForKeyPath: @ "statusBar"] valueForKeyPath: @ "foregroundView"] subviews]; 5 6 for (id child in children) {7 NSLog (@ "-- % @", [child class]); 8} copy the code and print the result as 5. traverse the array and retrieve the view for displaying the network status, and traverse all the member variables in it. Copy code 1 // the status bar is controlled by the current app. First, obtain the current app 2 UIApplication * app = [UIApplication sharedApplication]; 3 4 NSArray * children = [[app valueForKeyPath: @ "statusBar"] valueForKeyPath: @ "forexhaust OundView "] subviews]; 5 6 for (id child in children) {7 if ([child isKindOfClass: NSClassFromString (@" UIStatusBarDataNetworkItemView ")]) {8 // traverse all attributes of the Current Status Bar and find 9 unsigned int outCount = 0; 10 Ivar * ivars = class_copyIvarList ([child class], & outCount ); 11 12 for (int I = 0; I <outCount; I ++) {13 Ivar ivar = ivars [I]; 14 printf ("| % s ", ivar_getName (ivar); 15} 16} 17} copy the code and print the result as 6. Use KVC to retrieve the dataNet WorkType 1 if ([child isKindOfClass: NSClassFromString (@ "UIStatusBarDataNetworkItemView")]) {2 id type = [child valueForKeyPath: @ "dataNetworkType"]; 3 NSLog (@ "_ dataNetworkType class is % @, value is % @", [type class], type); 4} print the result: visible, dataNetworkType is NSNumber, the value is 5. [All of the above are simulator tests] after testing, it is found that the possible values are 1, 2, 3, and 5, respectively, the corresponding network status is 2G, 3G, 4G, and WIFI. When there is no network, the UIStatusBarDataNetworkItemView is hidden and the dataNetworkType value cannot be obtained. The following code is complete and tested on a real machine: Copy code 1-(void) viewDidAppear :( BOOL) animated 2 {3 // the status bar is controlled by the current app. First, obtain the current app 4 UIApplication * app = [UIApplication sharedApplication]; 5 6 NSArray * children = [[app valueForKeyPath: @ "statusBar"] valueForKeyPath: @ "foregroundView"] subviews]; 7 8 int type = 0; 9 for (id child in children) {10 if ([child isKindOfClass: NSClassFromString (@ "UIStatusBarDataNetworkItemView")]) {11 type = [[child valueForKeyPath: @ "dataNetworkType"] intValue]; 12} 13} 14 NSLog (@ "---- % d ", type); 15} the network status corresponding to the type number printed by the copy code is: 0-No network; 1-2G; 2-3G; 3-4G; 5-WIFI

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.