美文网首页
(私有API)iOS状态栏获取网络类型,运营商,电池电量,显示的

(私有API)iOS状态栏获取网络类型,运营商,电池电量,显示的

作者: 爵笙彦 | 来源:发表于2016-04-06 11:50 被阅读624次

    首先创建StatusBarTool文件
    再拷贝下面的代码
    此方法用到了私有API(不建议使用)
    .h文件代码

    #import <UIKit/UIKit.h>
    typedef enum {
        NetWorkTypeNone = 0,    //未知网络或没有网络
        NetWorkType2G   = 1,    //2G网络
        NetWorkType3G   = 2,    //3G网络
        NetWorkType4G   = 3,    //4G网络
        NetWorkTypeWiFi = 5,    //WiFi网络
        
    } NetWorkType;
    
    @interface StatusBarTool : NSObject
    
    /** 获取当前网络状态 */
    + (NetWorkType)currentNetWorkType;
    
    /** 获取当前网络运营商 */
    + (NSString *)serviceCompany;
    
    /** 获取当前电池电量 */
    + (NSString *)currentBatteryPercent;
    
    /** 获取当前时间 */
    + (NSString *)currentTimeString;
    
    @end
    
    

    .m文件代码

    #import "StatusBarTool.h"
    
    @implementation StatusBarTool
    
    + (NSString *)currentBatteryPercent
    {
        NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        for (id info in infoArray) {
            if ([info isKindOfClass:NSClassFromString(@"UIStatusBarBatteryPercentItemView")]) {
                NSString *percentString = [info valueForKey:@"percentString"];
                NSLog(@"电量为: %@",percentString);
                return percentString;
            }
        }
        return @"";
    }
    
    + (NetWorkType)currentNetWorkType
    {
        NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        NetWorkType type;
        for (id info in infoArray) {
            if ([info isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
                type = [[info valueForKey:@"dataNetworkType"] intValue];
                NSLog(@"网络状态: %d",type);
                return type;
            }
        }
        return NetWorkTypeNone;
    }
    
    + (NSString *)currentTimeString
    {
        NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        
        for (id info in infoArray) {
            if ([info isKindOfClass:NSClassFromString(@"UIStatusBarTimeItemView")]) {
                NSString *timeString = [info valueForKey:@"timeString"];
                NSLog(@"当前时间为:%@",timeString);
                return timeString;
            }
            
        }
        return @"";
    }
    
    + (NSString *)serviceCompany
    {
        NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        
        for (id info in infoArray) {
            if ([info isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) {
                NSString *serviceString = [info valueForKey:@"serviceString"];
                NSLog(@"运营商:%@",serviceString);
                return serviceString;
            }
        }
        return @"";
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:(私有API)iOS状态栏获取网络类型,运营商,电池电量,显示的

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