在iOS 11上计算或获得Swift上的WiFi强度的方法

时间:2017-11-30 01:05:55

标签: swift wifi ios11 key-value-observing uistatusbar

寻找获取或计算设备的wifi强度的方法?已经看到了抓取状态栏但无法在iOS 11上运行的示例:

func getSignalStrength() -> Int {
    let application = UIApplication.shared
    let statusBarView = application.value(forKey: "statusBar") as! UIView
    let foregroundView = statusBarView.value(forKey: "foregroundView") as! UIView
    let foregroundViewSubviews = foregroundView.subviews

    var dataNetworkItemView:UIView!

    for subview in foregroundViewSubviews {
        if subview.isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
            dataNetworkItemView = subview
            print("NONE")
            break
        } else {
            print("NONE")

            return 0 //NO SERVICE
        }
    }

    return dataNetworkItemView.value(forKey: "signalStrengthBars") as! Int
}

任何聪明的想法?

不需要苹果批准

2 个答案:

答案 0 :(得分:2)

您拥有的代码适用于移动网络(2G / 3G / 4G等),而不适用于WiFi网络。

Swift4 中尝试以下代码,它适用于我的iOS11:

app/home/app/assets/images/AzureAppServiceLogo.png

如何在iPhoneX上获得WiFi强度,请在另一篇文章中查看我的答案:Using Private API To read WiFi RSSI Value

答案 1 :(得分:0)

您可以在所有设备(包括所有新iphone)上使用此代码

class DeviceType {

static let deviceType = UIDevice.current.modelName
class func getDevice() -> Devices{
    switch deviceType {
    case "iPhone10,3", "iPhone10,6":
        print("iPhoneX")
        return Devices.iPhoneX
    case "iPhone11,2":
        print("iPhone XS")
        return Devices.iPhoneXS
    case "iPhone11,4":
        print("iPhone XS Max")
        return Devices.iPhoneXSMax
    case "iPhone11,6":
        print("iPhone XS Max China")
        return Devices.iPhoneXSMaxChina
    case "iPhone11,8":
        print("iPhone XR")
        return Devices.iPhoneXR
    default:
        return Devices.none
    }
}

}

extension UIDevice {
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier
    }
}


enum Devices {
    case iPhoneX
    case iPhoneXS
    case iPhoneXSMax
    case iPhoneXSMaxChina
    case iPhoneXR
    case none
}

func getSignalStrength() -> Int {


var classNameForStatusBar = ""
var valueForSignalValue = ""

classNameForStatusBar = "UIStatusBarSignalStrengthItemView"
valueForSignalValue = "signalStrengthBars"

let app = UIApplication.shared
let statusBarView = app.value(forKey: "statusBar") as! UIView
if UIDevice().userInterfaceIdiom == .phone {
    switch UIScreen.main.nativeBounds.height {
    case 2436, 2688, 1792:
        print("iPhone X, XS")
        return checkStatus(statusBarView)
    default:
        if(statusBarView.frame.width == 1024.0 ||  statusBarView.frame.width == 1112.0 || statusBarView.frame.width == 1024.0 || statusBarView.frame.width == 768.0){
            if let viewStatus = statusBarView.value(forKey: "statusBar") as? UIView{
                if let foregroundView = viewStatus.value(forKey: "foregroundView") as? UIView{
                    let subViews = foregroundView.subviews
                    var dataNetworkItemView:UIView?
                    for subview in subViews {

                        if subview.isKind(of: NSClassFromString(classNameForStatusBar)!){
                            dataNetworkItemView = subview;
                            break
                        }
                    }
                    guard let signal = dataNetworkItemView?.value(forKey: valueForSignalValue) as? Int else { return 0 }
                    return signal
                }
            }
            return 0
        }
        print("Unknown")
        if let foregroundView = statusBarView.value(forKey: "foregroundView") as? UIView{
            let subViews = foregroundView.subviews

            var dataNetworkItemView:UIView?
            for subview in subViews {

                if subview.isKind(of: NSClassFromString(classNameForStatusBar)!){
                    dataNetworkItemView = subview;
                    break
                }
            }
            guard let signal = dataNetworkItemView?.value(forKey: valueForSignalValue) as? Int else { return 0 }
            return signal
        }
    }
}
return 0

}

private func checkStatus(_ statusBarView: UIView) -> Int{
    if DeviceType.getDevice() == .iPhoneX{
        if let viewStatus = statusBarView.value(forKey: "statusBar") as? UIView{
            if let foregroundView = viewStatus.value(forKey: "foregroundView") as? UIView{
                let subViews = foregroundView.subviews
                var dataNetworkItemView:UIView?
                for subview in subViews {
                    for localSubView in subview.subviews{
                        if localSubView.isKind(of: NSClassFromString("_UIStatusBarCellularSignalView")!){
                            dataNetworkItemView = localSubView
                            break
                        }
                    }
                }
                if dataNetworkItemView == nil{
                    return 0
                }else{
                    return 1
                }
//                guard let signal = dataNetworkItemView?.value(forKey: "signalStrengthBars") as? Int else { return 0 }
//                return signal
            }
        }
    }else{
        if let viewStatus = statusBarView.value(forKey: "statusBar") as? UIView{
            if let foregroundView = viewStatus.value(forKey: "foregroundView") as? UIView{
                let subViews = foregroundView.subviews
                var dataNetworkItemView:UIView?
                print(foregroundView)
                print(viewStatus)
                print(statusBarView)
                for subview in subViews {
                    for localSubview in subview.subviews{
                        if localSubview.isKind(of: NSClassFromString("_UIStatusBarDualCellularSignalView")!){
                            dataNetworkItemView = subview;
                            break
                        }else if localSubview.isKind(of: NSClassFromString("_UIStatusBarCellularSignalView")!){
                            print("Check this")
                        }
                    }
                }
                if dataNetworkItemView == nil{
                    return 0
                }else{
                    return 1
                }
            }
        }
    }
    return 0
}

只需将此代码粘贴到任何快速文件中即可。