iOS状态栏获取网络类型,运营商,电池电量,显示的系统时间等信息

iOS状态栏获取网络类型,运营商,电池电量,显示的系统时间等信息

此篇文章讲述iOS中的状态栏

iOS开发中对状态栏的操作比较少,因为状态栏是系统级别的View,是受苹果保护的,不可以随便更改,不可以随便遮挡住。如果违反这些苹果的规范,那么APP将有可能上不了架。笔者就增写过一个APP,因状态栏被提示框遮挡而被拒(杯具)了。

iOS开发中最常见的对状态栏的操作就是设置状态栏的样式,比如: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];(此样式的状态栏中的字为白色,所以用在导航栏为深色的情况下,例如黑色或者红色的导航栏)

还有 
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];(此样式的状态栏中的字为黑色色,所以用在导航栏为浅色色的情况下,比如白色的导航栏) 
如果你设置了,发现状态栏没反应,那么你要去info.plist中加一个key,这个key为View controller-based status bar appearance,对应的value为NO。这样每个vc中就可以随便设置状态栏的样式了。

那么实际上我们可以拿到状态栏这个UIView对象。为了不让苹果看到我们用了某某私有API,我们这样做: 
- (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; 

调用这个方法就可以拿到系统的状态栏对象,是个UIView,那么UIView我们最熟悉了,我们可以改变状态栏的颜色,大小位置(frame)等等。

下面我们要从状态栏中获取我们想要的信息 
定义一个工具类StatusBarTool,用来获取网络类型,运营商,电池电量,显示的系统时间等信息。那么直接看代码: 
.h文件 
#import < Foundation/Foundation.h > 
#import < UIKit/UIKit.h >

//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;

@end 
.m实现文件(很重要) 
#import “StatusBarTool.h”

@ implementation StatusBarTool

+(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 @”“; 
}  
+(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 *)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 @”“; 
}

+(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 @”“; 
}  
@end

用StatusBarTool这个类的时候

NSString *company = [StatusBarTool serviceCompany];  
NSLog(@”company = %@”,company);  
company = 中国移动  
NetWorkType type = [StatusBarTool currentNetworkType];  
NSLog(@”type = %ld”,type);  
type = 5(5代表Wi-Fi)  
/*typedef NS_ENUM(NSUInteger, NetWorkType) {  
NetWorkTypeNone=0,  
NetWorkType2G=1,  
NetWorkType3G=2,  
NetWorkType4G=3,  
NetWorkTypeWiFI=5,  
};*/  
NSString *batteryPercent = [StatusBarTool currentBatteryPercent];  
NSLog(@”batteryPercent = %@”,batteryPercent);  
batteryPercent �� = 96%

NSString *timeString = [StatusBarTool currentTimeString];  
NSLog(@”timeString = %@”,timeString);  
timeString = 12:57

好了,到此我们已经拿到了网络状态,运营商,电池电量百分比,时间显示等信息了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值