博客> iOS 13 APP闪退
iOS 13 APP闪退
2019-12-10 23:57 评论:0 阅读:131 IT喃
ios
定位问题:

发现是发现状态栏时,之前获取到的[statusBar valueForKeyPath:@"statusBar"]数据,现在是空,直接上代码吧


+ (BOOL)getNetworkType
{
    UIApplication *app = [UIApplication sharedApplication];
    id statusBar = nil;
    NSString *network = @"";

    if (@available (iOS 13.0,*)) {
//        statusBar = app.keyWindow.windowScene.statusBarManager;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
        UIStatusBarManager *statusBarManager = app.keyWindow.windowScene.statusBarManager;
        if ([statusBarManager respondsToSelector:@selector(createLocalStatusBar)]) {
            UIView *_localStatusBar = [statusBarManager performSelector:@selector(createLocalStatusBar)];
            if ([_localStatusBar respondsToSelector:@selector(statusBar)]) {
                statusBar = [_localStatusBar performSelector:@selector(statusBar)];
            }
        }

#pragma clang diagnostic pop
        if (statusBar) {
            // _UIStatusBarDataCellularEntry
            id currentData = [[statusBar valueForKeyPath:@"_statusBar"] valueForKeyPath:@"currentData"];
            id _wifiEntry = [currentData valueForKeyPath:@"wifiEntry"];
            id _cellularEntry = [currentData valueForKeyPath:@"cellularEntry"];
            if (_wifiEntry && [[_wifiEntry valueForKeyPath:@"isEnabled"] boolValue]) {
                // If wifiEntry is enabled, is wifi
                network = @"WIFI";
            }
            else if (_cellularEntry && [[_cellularEntry valueForKeyPath:@"isEnabled"] boolValue]) {
                NSNumber *type = [_cellularEntry valueForKeyPath:@"type"];
                if (type) {
                    switch (type.integerValue) {
                        case 5:
                            network = @"4G";
                            break;
                        case 4:
                            network = @"3G";
                            break;
                        case 1:
                            network = @"1G";
                            break;
                        case 0:
                            network = @"NONE";
                            break;

                        default:
                            network = @"NONE";
                            break;
                    }
                }
            }
        }
    }
    else {
        statusBar = [app valueForKeyPath:@"statusBar"];

        if (KIsiPhoneX) {
            //        iPhone X
            id statusBarView = [statusBar valueForKeyPath:@"statusBar"];
            UIView *foregroundView = [statusBarView valueForKeyPath:@"foregroundView"];

            NSArray *subviews = [[foregroundView subviews][2] subviews];

            for (id subview in subviews) {
                if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarWifiSignalView")]) {
                    network = @"WIFI";
                }else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarStringView")]) {
                    network = [subview valueForKeyPath:@"originalText"];
                } else if ([subview isKindOfClass:NSClassFromString(@"_UIStatusBarImageView")]) { // 手机热点
                    network = @"hotspot";
                }
            }
        } else {
            //        非 iPhone X
            UIView *foregroundView = [statusBar valueForKeyPath:@"foregroundView"];
            NSArray *subviews = [foregroundView subviews];

            for (id subview in subviews) {
                if ([subview isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
                    int networkType = [[subview valueForKeyPath:@"dataNetworkType"] intValue];
                    switch (networkType) {
                        case 0:
                            network = @"NONE"; // 无网络
                            break;
                        case 1:
                            network = @"2G";
                            break;
                        case 2:
                            network = @"3G";
                            break;
                        case 3:
                            network = @"4G";
                            break;
                        case 5:
                            network = @"WIFI";
                            break;
                        case 6: // 其他手机开放的热点
                            network = @"hotspot";
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
    if ([network isEqualToString:@""]) {
        network = @"NO DISPLAY";
    }
    if ([network isEqualToString:@"NONE"] || [network isEqualToString:@"NO DISPLAY"]) {
        return NO;
    } else {
        return YES;
    }
}
收藏
0
sina weixin mail 回到顶部