获取当前状态栏及其它

获取状态栏

- (UIView*)statusBarView{
    UIView *statusBar = nil;
    NSData *data = [NSData dataWithBytes:(unsigned char []){0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x61, 0x72} length:9];
    NSString *key = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    id object = [UIApplication sharedApplication];
    if ([object respondsToSelector:NSSelectorFromString(key)]) statusBar = [object valueForKey:key];
    return statusBar;
}

获取电量

+ (NSString *)currentBatteryPercent{
    NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    for (id info in infoArray){
        
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarBatteryPercentItemView")]) {
            
            NSString *percentString = [info valueForKeyPath:@"percentString"];
            NSLog(@"电量为:%@",percentString);
            return percentString;
        }
        }
    return @"";
}

当前时间

    NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    for (id info in infoArray) {
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarTimeItemView:")]){
    NSString *timeString = [info valueForKeyPath:@"timeString"];
        NSLog(@"当前显示时间为:%@",timeString);
        return timeString;
    }
    }
    return @"";
}

当前运营商

+ (NSString *)serviceCompany{
    NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    for (id info in infoArray){
        if ([info isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]){
        NSString *serviceString = [info valueForKeyPath:@"serviceString"];
        NSLog(@"公司为:%@",serviceString);
        return serviceString;
        }
        
    }
    return @"";
}

你可能感兴趣的:(获取当前状态栏及其它)