iOS状态栏的各种操作,状态栏颜色,当前网络状态,电池,时间,WiFi信号强度、名称,ip地址

//此样式的状态栏中的字为白色,所以用在导航栏为深色的情况下,例如黑色或者红色的导航栏
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//此样式的状态栏中的字为黑色色,所以用在导航栏为浅色色的情况下,比如白色的导航栏
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];
//设置了,发现状态栏没反应,那么你要去info.plist中加一个key,这个key为View controller-based status bar appearance,对应的value为NO。这样每个vc中就可以随便设置状态栏的样式了。
#import 
//#import 
/*
 - (NSString *)getIPAddress
 -(NSString *)getWifiName
 
 所需 一下3个库
 */
#import 
#import 
#import 

//0 - 无网络 ; 1 - 2G ; 2 - 3G ; 3 - 4G ; 5 - WIFI
typedef NS_ENUM(NSUInteger, NetWorkType) {
    NetWorkTypeNone=0,
    NetWorkType2G=1,
    NetWorkType3G=2,
    NetWorkType4G=3,
    NetWorkTypeWiFI=5,
};
@interface StatusBarTool : NSObject
/** * * *  @return 当前网络类型 */
+ (NetWorkType )currentNetworkType;
/** * * *  @return SIM卡所属的运营商(公司) */
+ (NSString *)serviceCompany;
/** * * *  @return 当前电池电量百分比 */
+ (NSString *)currentBatteryPercent;
/** * * *  @return 当前时间显示的字符串 */
+ (NSString *)currentTimeString;

/** * * * @return 获取操作栏对象 改变状态栏的颜色,背景颜色(backgroundColor)、大小位置(frame)等等 */
+ (UIView *)statusBarView;
/** * * * @return 获取WiFi信号强度  */
- (int)getSignalStrength;

/** * * * @return 获取ip地址名称  */
+ (NSString *)getIPAddress;
/** * * * @return 获取WiFi名称  */
+ (NSString *)getWifiName;
@end
********************************************************
#import "StatusBarTool.h"
@implementation StatusBarTool
+ (NetWorkType )currentNetworkType
{
    NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    NetWorkType type;
    for (id info in infoArray){
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            type = [[info valueForKeyPath:@"dataNetworkType"] integerValue];
            NSLog(@"----%lu", (unsigned long)type);
            return (NetWorkType)type;
        }
    }
    return NetWorkTypeNone;
}
+ (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(@"公司为:%@",serviceString);
            return serviceString;
        }
    }
    return @"";
}
+ (NSString *)currentBatteryPercent
{
    NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    for (id info in infoArray){
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarBatteryPercentItemView")]){
            NSString *percentString = [info valueForKeyPath:@"percentString"];
            NSLog(@"电量为:%@",percentString);
            return percentString;
        }
    }
    return @"";
}
+ (NSString *)currentTimeString
{
    NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    for (id info in infoArray){
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarTimeItemView")]){
            NSString *timeString = [info valueForKeyPath:@"timeString"];
            NSLog(@"当前显示时间为:%@",timeString);
            return timeString;
        }
    }
    return @"";
}
+ (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;
}
- (int)getSignalStrength
{
    NSLog(@"======获取Wifi信号强度======");
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    NSString *dataNetworkItemView = nil;
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }
    int signalStrength = [[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];
    NSLog(@"signal %d", signalStrength);
    return signalStrength;
}
+ (NSString *)getIPAddress
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    
    return address;
}
+ (NSString *)getWifiName
{
    NSString *wifiName = nil;
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    if (!wifiInterfaces) {
        return nil;
    }
    NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
    for (NSString *interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
        if (dictRef) {
            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
            NSLog(@"network info -> %@", networkInfo);
            wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
            CFRelease(dictRef);
        }
    }
    CFRelease(wifiInterfaces);
    return wifiName;
}
@end

你可能感兴趣的:(iOS状态栏的各种操作,状态栏颜色,当前网络状态,电池,时间,WiFi信号强度、名称,ip地址)