美文网首页iOS零碎知识iOS学习iOS菜鸟级开发
iOS状态栏获取网络类型,运营商,电池电量,显示的系统时间等信息

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

作者: jsonzhou | 来源:发表于2016-01-25 20:23 被阅读697次

iOS开发中对状态栏的操作比较少,因为状态栏是系统级别的View,是受苹果保护的,不可以随便更改,不可以随便遮挡住。

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 = [NSDatadataWithBytes:(unsignedchar[]){0x73,0x74,0x61,0x74,0x75,0x73,0x42,0x61,0x72}length:9];

NSString*key = [[NSStringalloc]initWithData:dataencoding:NSASCIIStringEncoding];

idobject = [UIApplicationsharedApplication];

if([objectrespondsToSelector:NSSelectorFromString(key)]) statusBar = [objectvalueForKey:key];

returnstatusBar;

}

调用这个方法就可以拿到系统的状态栏对象,是个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

相关文章

网友评论

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

    本文链接:https://www.haomeiwen.com/subject/omachttx.html