Swift-Get status bar some information

Source: Internet
Author: User

Get information about the status bar: network type, operator, battery level, displayed system time, and moreImport UIKitEnumNetworktype {CaseNetworkstatesnoneNo networkCasenetworkstates2g2GCasenetworkstates3g3GCasenetworkstates4g4GCaseNetworkstateswifiWIFI}ClassStatusbarinfo:NSObject {Get Carrier nameClassFuncServicecompany ()String {Let array:Nsarray = (Uiapplication.sharedapplication (). Valueforkeypath ("StatusBar")? Valueforkeypath ("Foregroundview")? Subviews)!For infoIn array {If Info.iskindofclass (Nsclassfromstring ("Uistatusbarserviceitemview")!) {Let Serverstr:String = Info.valueforkeypath ("Servicestring")as!StringReturn SERVERSTR}}Return"" }Show System timeClassFuncCurrenttimestring ()String {Let array:Nsarray = (Uiapplication.sharedapplication (). Valueforkeypath ("StatusBar")? Valueforkeypath ("Foregroundview")? Subviews)!For infoIn array {If Info.iskindofclass (Nsclassfromstring ("Uistatusbartimeitemview")!) {Let Timestr:String = Info.valueforkeypath ("TimeString")as!StringReturn TIMESTR}}Return"" }Detecting Network statusClassFuncCurrentnetworktype ()Networktype {Let array:Nsarray = (Uiapplication.sharedapplication (). Valueforkeypath ("StatusBar")? Valueforkeypath ("Foregroundview")? Subviews)!var states:Networktype =.NetworkstatesnoneFor infoIn array {If Info.iskindofclass (Nsclassfromstring ("Uistatusbardatanetworkitemview")!) {Let Networktype = Info.valueforkeypath ("Datanetworktype")!. IntegerValueSwitch (networktype) {Case0:states =.Networkstatesnone;No network modeBreakCase1:states =.networkstates2g;BreakCase2:states =.networkstates3g;BreakCase3:states =.networkstates4g;BreakCase5:states =.Networkstateswifi;BreakDefaultBreak } } }Return states}Get current battery informationClassFuncCurrentbatterypercent ()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-Get status bar some information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.