首页 > 代码库 > Swift - 获取状态栏一些信息
Swift - 获取状态栏一些信息
// 获取状态栏的各种信息 :网络类型,运营商,电池电量,显示的系统时间等信息
import UIKit
enum NetWorkType {
case NetworkStatesNone // 没有网络
case NetworkStates2G // 2G
case NetworkStates3G // 3G
case NetworkStates4G // 4G
case NetworkStatesWIFI // WIFI
}
class StatusBarInfo: NSObject {
// 获取运营商 名称
class func serviceCompany() -> String {
let array :NSArray = (UIApplication.sharedApplication().valueForKeyPath("statusBar")?.valueForKeyPath("foregroundView")?.subviews)!
for info in array {
if info.isKindOfClass(NSClassFromString("UIStatusBarServiceItemView")!) {
let serverStr:String = info.valueForKeyPath("serviceString") as! String
return serverStr
}
}
return ""
}
// 显示系统时间
class func currentTimeString() -> String {
let array :NSArray = (UIApplication.sharedApplication().valueForKeyPath("statusBar")?.valueForKeyPath("foregroundView")?.subviews)!
for info in array {
if info.isKindOfClass(NSClassFromString("UIStatusBarTimeItemView")!) {
let timeStr:String = info.valueForKeyPath("timeString") as! String
return timeStr
}
}
return ""
}
// 检测网络状态
class func currentNetworkType() -> NetWorkType {
let array :NSArray = (UIApplication.sharedApplication().valueForKeyPath("statusBar")?.valueForKeyPath("foregroundView")?.subviews)!
var states :NetWorkType = .NetworkStatesNone
for info in array {
if info.isKindOfClass(NSClassFromString("UIStatusBarDataNetworkItemView")!) {
let networkType = info.valueForKeyPath("dataNetworkType")!.integerValue
switch (networkType) {
case 0:
states = .NetworkStatesNone;
//无网模式
break;
case 1:
states = .NetworkStates2G;
break;
case 2:
states = .NetworkStates3G;
break;
case 3:
states = .NetworkStates4G;
break;
case 5:
states = .NetworkStatesWIFI;
break;
default:
break;
}
}
}
return states
}
// 获取当前的电量信息
class func currentBatteryPercent() -> String {
let array :NSArray = (UIApplication.sharedApplication().valueForKeyPath("statusBar")?.valueForKeyPath("foregroundView")?.subviews)!
for info in array {
if info.isKindOfClass(NSClassFromString("UIStatusBarBatteryPercentItemView")!) {
let presentStr:String = info.valueForKeyPath("percentString") as! String
return presentStr
}
}
return ""
}
}
Swift - 获取状态栏一些信息
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。