码迷,mamicode.com
首页 > 编程语言 > 详细

Swift - 获取状态栏一些信息

时间:2017-03-22 14:14:43      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:UI   状态   iap   services   cas   bar   service   key   keyword   

 

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

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 - 获取状态栏一些信息

标签:UI   状态   iap   services   cas   bar   service   key   keyword   

原文地址:http://www.cnblogs.com/gongyuhonglou/p/6599544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!