美文网首页good
Swift - 获取状态栏一些信息

Swift - 获取状态栏一些信息

作者: BeijingIamback | 来源:发表于2016-08-07 21:18 被阅读596次

    OC 实现参考文章

    来获取状态栏的,运营商,网络,时间,电量的信息。

    swift代码上代码:
    //
    //  StatusBarInfo.swift
    //  swift_show
    //
    //  Created by Deki on 16/8/7.
    //  Copyright © 2016年 Deki. All rights reserved.
    //  获取状态栏的各种信息 :网络类型,运营商,电池电量,显示的系统时间等信息
    
    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 - 获取状态栏一些信息

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