標籤:UI 狀態 iap services cas bar service key keyword
// 擷取狀態列的各種資訊 :網路類型,電訊廠商,電池電量,顯示的系統時間等資訊import UIKitenum 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 - 擷取狀態列一些資訊