In the APP, it is sometimes necessary to monitor the network status of user devices in real time, so that users can know their own network status and perform intelligent processing according to the network status of users, so as to save user traffic and improve user experience. There are multiple ways to check network status. The most common one is That Apple officially provides a network called Reachability and AFNetworing to check network status. However, careful friends do not know whether they have noticed that the status bar of the mobile phone will generally display the current network status, because the status bar is a system-level View, which is protected by Apple, and can not be changed or blocked casually. If these apple specifications are violated, the APP may not be available. Therefore, the status bar is rarely operated, but it is not impossible to operate the status bar. The following shows the simple use of the status bar. Ios development of the status bar operation is relatively few, the most common development of the status bar operation is to set the status bar style, such as:

 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Copy the code

(This style has white text in the status bar, so it is used when the navigation bar is dark, such as black or red.)

The default status bar style is:

  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
Copy the code

(This style is used when the status bar is black, so it is used when the navigation bar is light colored, such as white.) If you set it and the status bar does not respond, you need to add a key to info.plist. The key is View Controller-based status bar appearance, and the corresponding value is NO. So you can style the status bar in any VC.

View controller-based status bar appearance :NO
Copy the code

We can get the status bar this UIView object, can change the status bar color, backgroundColor, size position (frame) and so on.

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *bg= [self statusBarView]; bg.backgroundColor = [UIColor redColor]; bg.frame = CGRectMake(0, 100, self.view.frame.size.width, 60); } - (UIView*)statusBarView; { UIView *statusBar = nil; NSData *data = [NSData dataWithBytes:(unsigned char []){0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x61, 0x72} length:9];  NSString *key = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; id object = [UIApplication sharedApplication];if ([object respondsToSelector:NSSelectorFromString(key)]) statusBar = [object valueForKey:key];
    return statusBar;
}
Copy the code

The above is the simple operation and setting of the status bar, which is not usually used, because the height and color of the status bar will not be arbitrarily changed in real project development, but the information on the status bar is usually useful. Let’s look at the code

3. Operate the status bar to get the network type

UIApplication *app = [UIApplication sharedApplication]; id statusBar = [app valueForKeyPath:@"statusBar"]; Unsigned int outCount =0; Ivar *ivars = class_copyIvarList([statusBar class], &outCount);for (int i =0; i < outCount; i++) {
    Ivar ivar = ivars[i];
    printf("%s\n",ivar_getName(ivar));
}
Copy the code

Print result:

_statusBarServer _inProcessProvider _showsForeground _backgroundView _foregroundView _doubleHeightLabel _doubleHeightLabelContainer _currentDoubleHeightText _currentRawData _interruptedAnimationCompositeViews _newStyleBackgroundView _newStyleForegroundView _slidingStatusBar _requestedStyle _styleOverrides _styleAttributes _orientation _hidden _suppressesHiddenSideEffects _foreground _registered _waitingOnCallbackAfterChangingStyleOverridesLocally _suppressGlow _translucentBackgroundAlpha _showOnlyCenterItems _foregroundViewShouldIgnoreStatusBarDataDuringAnimation _localDataOverrides _tintColor _lastUsedBackgroundColor _nextTintTransition _overrideHeight _disableRasterizationReasons _persistentAnimationsEnabled _simulatesLegacyAppearance  _serverUpdatesDisabled _timeHidden _homeItemsDisabled _statusBarWindow _styleDelegate _dockDataProvider __transitionCoordinator _foregroundColor _legibilityStyleCopy the code
  1. Enter the foregroundView element and iterate over all of its children
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
for (id child in children) {
    NSLog(@"% @", [child class]);
}
Copy the code

Print result:

UIStatusBarSignalStrengthItemView
UIStatusBarServiceItemView
UIStatusBarDataNetworkItemView
UIStatusBarBatteryItemView
UIStatusBarBluetoothItemView
UIStatusBarIndicatorItemView
UIStatusBarTimeItemView
Copy the code
  1. Enter UIStatusBarServiceItemView UIStatusBarDataNetworkItemView/UIStatusBarSignalStrengthItemView element and traverse all of its child elements
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
for (id child in children) {
    if ([child isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) {
        unsigned int outCount =0;
        Ivar *ivars = class_copyIvarList([child class], &outCount);
        for (int i =0; i < outCount; i++) {
            Ivar ivar = ivars[i];
            printf("%s\n",ivar_getName(ivar)); }}}Copy the code

Print result:

_serviceString
_crossfadeString
_crossfadeStep
_maxWidth
_serviceWidth
_crossfadeWidth
_contentType
_loopingNecessaryForString
_loopNowIfNecessary
_loopingNow
_letterSpacing
Copy the code
  1. Obtain the carrier name from serviceString
    NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
    for (id child in children) {
    if ([child isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) {// Unsigned int outCount =0; Ivar *ivars = class_copyIvarList([child class], &outCount);for (int i =0; i < outCount; i++) {
                Ivar ivar = ivars[i];
                printf("%s\n",ivar_getName(ivar));
            }
            
            //1._serviceString
            id type = [child valueForKeyPath:@"serviceString"];
            NSLog(@"_serviceString: %@".type);

        }
        
 if ([child isKindOfClass:NSClassFromString(@"UIStatusBarBatteryItemView")]) {
            unsigned int outCount =0;
            Ivar *ivars = class_copyIvarList([child class], &outCount);
            for (int i =0; i < outCount; i++) {
                Ivar ivar = ivars[i];
                printf("%s\n",ivar_getName(ivar));
            }
            id cachedCapacity = [child valueForKeyPath:@"cachedCapacity"];
            NSLog(@"cachedCapacity: %@", cachedCapacity);
        }
        
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {// Obtain the network type NSInteger netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            NSLog(@"netType: %ld", netType); Int signalStrength = [[Child valueForKeyPath:@"wifiStrengthBars"]intValue];
            NSLog(@"netType: %d", signalStrength); }}Copy the code

Print result:

UIStatusBarDataNetworkItemView results: _dataNetworkType _wifiStrengthRaw _wifiStrengthBars _wifiLinkWarning _enableRSSI _showRSSI UIStatusBarBatteryItemView results:  _capacity _state _batterySaverModeActive _accessoryView _cachedImageHasAccessoryImage _cachedCapacity _cachedImageSet _cachedBatteryStyleCopy the code

Ok, so now that we have the network state, there are other useful information that can be obtained by traversing the UIStatusBar. Here are some common ways to obtain information, wrapped in a “NetworkTools” tool class

NetworkTools. H file

// // networkTools. h // Demo // // Created by Allison on 2017/7/13. // Copyright © 2017 Allison. //#import <Foundation/Foundation.h>@interface NetworkTools: NSObject // Get device UUID + (NSString*)getUUID; // Device MAC address + (NSString *) macAddress; // Version + (NSString *)getVersion; // Carrier (company) to which the SIM card belongs +(NSString *)serviceCompany; +(NSDictionary *)getDinfo; @endCopy the code

NetworkTools. M file

//
//  NetworkTools.m
//  Demo
//
//  Created by Allison on 2017/7/13.
//  Copyright © 2017年 Allison. All rights reserved.
//

#import "NetworkTools.h"
#import <sys/sysctl.h>
#import <net/if.h>
#import <net/if_dl.h>
#import <UIKit/UIKit.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import 
      
        // The real machine must import this header file
      

/*
 * Top-level identifiers
 */
#define CTL_NET 4 /* network, see socket.h */
#define AF_ROUTE 17 /* Internal Routing Protocol */
#define AF_LINK 18 /* Link layer interface */
#define NET_RT_IFLIST2 6 /* interface list with addresses */
#define NET_RT_IFLIST 3 /* survey interface list */

#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"// Get the screen width#define screenWide [UIScreen mainScreen].bounds.size.width// Get the screen height#define screenHeight [UIScreen mainScreen].bounds.size.height@implementation NetworkTools // get device uuid + (NSString*)getUUID {CFUUIDRef puuid = CFUUIDCreate(nil); CFStringRef uuidString = CFUUIDCreateString( nil, puuid ); NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));returnresult; } // Device MAC address + (NSString *) macAddress {int MIB [6]; size_t len; char *buf; unsigned char *ptr; struct if_msghdr *ifm; struct sockaddr_dl *sdl; mib[0] = CTL_NET; mib[1] = AF_ROUTE; mib[2] = 0; mib[3] = AF_LINK; mib[4] = NET_RT_IFLIST;if ((mib[5] = if_nametoindex("en0")) = = 0) {printf("Error: if_nametoindex error/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }
    
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error! /n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    NSLog(@"outString:%@", outstring);
    
    free(buf);
    
    return [outstring uppercaseString];
}

// Version
+ (NSString *)getVersion {
    
    NSString *string = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:string];
    NSString *version = [dic objectForKey:@"CFBundleVersion"];
    returnversion; } // operator (company) to which the SIM card belongs +(NSString *)serviceCompany{NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    
    for (id info in infoArray)
    {
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")])
        {
            NSString *serviceString = [info valueForKeyPath:@"serviceString"];
            NSLog(@"Company: %@",serviceString);
            returnserviceString; }}return @"";
}


+ (NSDictionary *)getDinfo {
    NSString *mac = [self macaddress];
    NSString *udid = [self getUUID];
    NSString *ip = [self ipAddressIsV4:YES];
    NSString *p =  [self getDeviceInfo];
    NSString *jb = [NSString stringWithFormat:@"%i",[self jailbroken]];
    NSString *rw = [NSString stringWithFormat:@"%f",screenWide];
    NSString *rh = [NSString stringWithFormat:@"%f",screenHeight];
    NSString *o = [self chinaMobileModel];
    NSString *m = [self deviceName];
    NSString *osv = [self getSystemVersion];
    NSString *cv = [NSString stringWithFormat:@"%ld",[self version]];
    NSString *n = [self getNetWorkStates];
    
    NSDictionary *dinfo = @{
                            @"os": @"i"The @"osv": osv,
                            @"osvc": @"17"The @"udid": udid,
                            @"ip":ip,
                            @"p":p,
                            @"jb":jb,
                            @"rw":rw,
                            @"rh":rh,
                            @"o":o,
                            @"m":m,
                            @"cv":cv,
                            @"cvc": @"14"The @"mac": mac,
                            @"n":n,
                            @"phone": @""The @"imsi": @""The @"imei": @""
                            };
    returndinfo; } // Check the current network connection status +(NSString *)getNetWorkStates{UIApplication *app = [UIApplication sharedApplication]; NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews]; NSString *state = [[NSString alloc]init]; int netType = 0; // Get the network return codefor (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {// Obtain the status bar netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            NSLog(@"netType:%d",netType);
            switch (netType) {
                case 0:
                    //                    state = @"No network"; // 5
                    state = @"5"; // Netless modebreak;
                case 1:
                    //                    state = @"2G"; // 1
                    state = @"1";
                    break;
                case 2:
                    //                    state = @"3G"; // 2
                    state = @"3";
                    break;
                case 3:
                    //                    state = @"4G"; //3
                    state = @"4";
                    break;
                case 5:
                    //                    state = @"WIFI"; //5
                    state = @"5";
                    break;
                default:
                    break; }}} // Select according to the statusreturn state;
}


+ (NSString *)getSystemVersion {
    UIDevice *device = [[UIDevice alloc] init];
    NSString *systemVersion = device.systemVersion;
    returnsystemVersion; } // IP address + (NSString *)ipAddressIsV4:(BOOL)v4 {NSArray *searchArray = v4? @[ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
    @[ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
    
    NSDictionary *addresses = [self addressInfo];
    
    __block NSString *address;
    [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
     {
         address = addresses[key];
         if(address) *stop = YES; }];return address ? address : @"0.0.0.0"; } // Device info Product name + (NSString *)getDeviceInfo {UIDevice *device = [[UIDevice alloc] init]; NSString *name = device.name; NSString *model = device.name; // Get the device category NSString *type= device.localizedModel; NSString *systemName = device.systemname; NSString *systemName = device.systemname; NSString *systemVersion = device.systemVersion; NSString *systemVersion = device.systemVersion; // Get the current system version NSLog(@"-----name : %@,model : %@,type : %@,systemName :%@,systemVersion %@",name,model,type,systemName,systemVersion);
    returnmodel; } // Jailbreak + (BOOL)jailbroken {#if ! TARGET_IPHONE_SIMULATOR
    
    //Apps and System check list
    BOOL isDirectory;
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"Cyd"The @"ia.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"bla"The @"ckra1n.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"Fake"The @"Carrier.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"Ic"The @"y.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"Inte"The @"lliScreen.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"MxT"The @"ube.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"Roc"The @"kApp.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"SBSet"The @"ttings.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @"The @"App"The @"lic"The @"ati"The @"ons/"The @"Wint"The @"erBoard.a"The @"pp"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"pr"The @"iva"The @"te/v"The @"ar/l"The @"ib/a"The @"pt/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"pr"The @"iva"The @"te/v"The @"ar/l"The @"ib/c"The @"ydia/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"pr"The @"iva"The @"te/v"The @"ar/mobile"The @"Library/SBSettings"The @"Themes/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"pr"The @"iva"The @"te/v"The @"ar/t"The @"mp/cyd"The @"ia.log"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @"The @"pr"The @"iva"The @"te/v"The @"ar/s"The @"tash/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"us"The @"r/l"The @"ibe"The @"xe"The @"c/cy"The @"dia/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @"The @"us"The @"r/b"The @"in"The @"s"The @"shd"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @"The @"us"The @"r/sb"The @"in"The @"s"The @"shd"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"us"The @"r/l"The @"ibe"The @"xe"The @"c/cy"The @"dia/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"us"The @"r/l"The @"ibe"The @"xe"The @"c/sftp-"The @"server"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @"The @"/Syste"The @"tem/Lib"The @"rary/Lau"The @"nchDae"The @"mons/com.ike"The @"y.bbot.plist"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @ % @ % @ % @"The @"/Sy"The @"stem/Lib"The @"rary/Laun"The @"chDae"The @"mons/com.saur"The @"ik.Cy"The @"@dia.Star"The @"tup.plist"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @"The @"/Libr"The @"ary/Mo"The @"bileSubstra"The @"te/MobileSubs"The @"trate.dylib"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @"The @"/va"The @"r/c"The @"ach"The @"e/a"The @"pt/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @"The @"/va"The @"r/l"The @"ib"The @"/apt/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @"The @"/va"The @"r/l"The @"ib/c"The @"ydia/"] isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @"The @"/va"The @"r/l"The @"og/s"The @"yslog"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @"The @"/bi"The @"n/b"The @"ash"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @"The @"/b"The @"in/"The @"sh"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @"The @"/et"The @"c/a"The @"pt/"]isDirectory:&isDirectory]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @"The @"/etc/s"The @"sh/s"The @"shd_config"]]
        || [defaultManager fileExistsAtPath:[NSString stringWithFormat:@"/ % @ % @ % @ % @ % @"The @"/us"The @"r/li"The @"bexe"The @"c/ssh-k"The @"eysign"]])
        
    {
        returnYES; } // SandBox Integrity Check int pid = fork(); The child process returns 0, the parent process returns the child process ID, and -1 if there is an errorif(! pid){exit(0);
    }
    if(pid>=0)
    {
        return YES;
    }
    
    //Symbolic link verification
    struct stat s;
    if(lstat("/Applications", &s) || lstat("/var/stash/Library/Ringtones", &s) || lstat("/var/stash/Library/Wallpaper", &s)
       || lstat("/var/stash/usr/include", &s) || lstat("/var/stash/usr/libexec", &s)  || lstat("/var/stash/usr/share", &s)
       || lstat("/var/stash/usr/arm-apple-darwin9", &s))
    {
        if(s.st_mode & S_IFLNK){
            return YES;
        }
    }
    
    //Try to write file in private
    NSError *error;
    [[NSString stringWithFormat:@"Jailbreak test string"] writeToFile:@"/private/test_jb.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
    
    if(nil==error){
        //Writed
        return YES;
    } else {
        [defaultManager removeItemAtPath:@"/private/test_jb.txt" error:nil];
    }
    
#endif
    return NO;
}


+ (NSDictionary *)addressInfo
{
    NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
    
    // retrieve the current interfaces - returns 0 on success
    struct ifaddrs *interfaces;
    if(! getifaddrs(&interfaces)) { // Loop through linked list of interfaces struct ifaddrs *interface;for(interface=interfaces; interface; interface=interface->ifa_next) {
            if(! (interface->ifa_flags & IFF_UP) || (interface->ifa_flags & IFF_LOOPBACK)) {continue; // deeply nested code harder to read
            }
            const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
            if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
                NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                char addrBuf[INET6_ADDRSTRLEN];
                if(inet_ntop(addr->sin_family, &addr->sin_addr, addrBuf, sizeof(addrBuf))) {
                    NSString *key = [NSString stringWithFormat:@"% @ / % @", name, addr->sin_family == AF_INET ? IP_ADDR_IPv4 : IP_ADDR_IPv6];
                    addresses[key] = [NSString stringWithUTF8String:addrBuf];
                }
            }
        }
        // Free memory
        freeifaddrs(interfaces);
    }
    return[addresses count] ? addresses : nil; } // Operator + (NSString *)chinaMobileModel {CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init]; CTCarrier *carrier = [info subscriberCellularProvider];if (carrier == nil) {
        //        return @"Do not recognize";
        return @"0";
    }
    
    NSString *code = [carrier mobileNetworkCode];
    if (code == nil) {
        //        return @"Do not recognize";
        return @"0";
    }
    
    if ([code isEqualToString:@"00"] || [code isEqualToString:@"."] || [code isEqualToString:@"7"])
    {
        //        return @"Mobile";
        return @"1";
    }else if ([code isEqualToString:@"01"] || [code isEqualToString:@"6"])
    {
        //        return @"China unicom";
        return @"2";
    }else if ([code isEqualToString:@"3"] || [code isEqualToString:@"5"])
    {
        //        return @"Telecommunications";
        return @"3";
    }else if ([code isEqualToString:@"20"])
    {
        return @"Iron";
    }
    return @"Do not recognize";
}

+ (NSString *)deviceModel
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *model = malloc(size);
    sysctlbyname("hw.machine", model, &size, NULL, 0);
    NSString *deviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
    free(model);
    returndeviceModel; + (NSString *)deviceName {NSString *platform = [self deviceModel];if ([platform isEqualToString:@"IPhone1, 1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"IPhone1, 2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@IPhone2, 1 ""])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"However, 1"])    return @"iPhone 4 (GSM)";
    if ([platform isEqualToString:@"However, 3"])    return @"iPhone 4 (CDMA)";
    if ([platform isEqualToString:@"The iphone 4, 1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"The iphone 5, 1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"The iphone 5, 2"])    return @"iPhone 5 (CDMA)";
    if ([platform isEqualToString:@"The iphone 5, 3"])    return @"iPhone 5C";
    if ([platform isEqualToString:@"The iphone 5, 4"])    return @"iPhone 5C";
    if ([platform isEqualToString:@"IPhone6, 1"])    return @"iPhone 5S";
    if ([platform isEqualToString:@"IPhone6, 2"])    return @"iPhone 5S";
    if ([platform isEqualToString:@"IPhone7, 1"])    return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"IPhone7, 2"])    return @"iPhone 6";
    
    if ([platform isEqualToString:@"IPod1, 1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"IPod2, 1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"IPod3, 1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"IPod4, 1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"IPod5, 1"])      return @"iPod Touch 5G";
    
    if ([platform isEqualToString:@"IPad1, 1"])      return @"iPad";
    if ([platform isEqualToString:@"And 1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"And 2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"And 3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"And 5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"And 6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"And 7"])      return @"iPad Mini (CDMA)";
    if ([platform isEqualToString:@"IPad3, 1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"IPad3, 2"])      return @"iPad 3 (CDMA)";
    if ([platform isEqualToString:@"IPad3, 3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"IPad3, 4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"Ipad 3, 5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"IPad3, 6"])      return @"iPad 4 (CDMA)";
    
    if ([platform isEqualToString:@"IPad4, 1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"IPad4, 2"])      return @"iPad Air (GSM)";
    if ([platform isEqualToString:@"IPad4, 3"])      return @"iPad Air (CDMA)";
    if ([platform isEqualToString:@"IPad4, 4"])      return @"iPad Mini Retina (WiFi)";
    if ([platform isEqualToString:@"IPad4, 5"])      return @"iPad Mini Retina (CDMA)";
    
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}
@end
Copy the code

Usage:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *infoDic = [NetworkTools getDinfo];
    NSLog(@"% @", infoDic);
  }
Copy the code

The output

{
    cv = 0;
    cvc = 14;
    imei = "";
    imsi = "";
    ip = "192.168.0.110";
    jb = 0;
    m = "iPhone 6S";
    mac = "00:00:00:00:00:02";
    n = 5;
    o = 1;
    os = i;
    osv = "10.3.2";
    osvc = 17;
    p = "\U738b\U59e3\U59e3\U7684 iPhone";
    phone = "";
    rh = "568.000000";
    rw = "320.000000";
    udid = "AAAAAAAA-AAAA-AAAA-AAAA-D215092D9489";
}
Copy the code

Finally, attach the download address :Demo.