【问题标题】:IOS 13 Get Wifi and Cellular Network Signal StrengthIOS 13 获取 Wifi 和蜂窝网络信号强度
【发布时间】:2020-06-04 19:56:55
【问题描述】:

我已经尝试了所有可能的方法来获取 wifi 和蜂窝网络信号强度。

    func getSignalStrength() -> Int {
        let application = UIApplication.shared
       //let statusBarView = application.value(forKey: "statusBar") as! UIView
        let statusBarView = UIApplication.shared.statusBarUIView
        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
    }

    private func wifiStrength() -> Int? {
        let app = UIApplication.shared
        var rssi: Int?
        guard let statusBar = app.value(forKey: "statusBar") as? UIView, let foregroundView = statusBar.value(forKey: "foregroundView") as? UIView else {
            return rssi
        }
        for view in foregroundView.subviews {
            if let statusBarDataNetworkItemView = NSClassFromString("UIStatusBarDataNetworkItemView"), view .isKind(of: statusBarDataNetworkItemView) {
                if let val = view.value(forKey: "wifiStrengthRaw") as? Int {
                    //print("rssi: \(val)")

                    rssi = val
                    break
                }
            }
        }
        return rssi
    }


extension UIApplication {
var statusBarUIView: UIView? {

    if #available(iOS 13.0, *) {
        let tag = 38482458

        let keyWindow = UIApplication.shared.connectedScenes
            .map({$0 as? UIWindowScene})
            .compactMap({$0})
            .first?.windows.first

        if let statusBar = keyWindow?.viewWithTag(tag) {
            return statusBar
        } else {

            let statusBarView = UIView(frame: (keyWindow?.windowScene?.statusBarManager!.statusBarFrame)!)
            statusBarView.tag = tag
            statusBarView.layer.zPosition = 999999

            keyWindow?.addSubview(statusBarView)
            return statusBarView
        }

    } else {

        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
    }
    return nil
  }
}

代码在 application.value(forKey: "statusBar") 处崩溃,所以我尝试了扩展,但现在它在 statusBarView?.value(forKey: "foregroundView") 处崩溃,没有 foregroundView 的密钥对值。有没有更好的获取 wifi 或蜂窝信号的方法?

【问题讨论】:

    标签: ios swift ios13


    【解决方案1】:
    if #available(iOS 13.0, *) {
            if let statusBarManager = UIApplication.shared.keyWindow?.windowScene?.statusBarManager,
                let localStatusBar = statusBarManager.value(forKey: "createLocalStatusBar") as? NSObject,
                let statusBar = localStatusBar.value(forKey: "statusBar") as? NSObject,
                let _statusBar = statusBar.value(forKey: "_statusBar") as? UIView,
                let currentData = _statusBar.value(forKey: "currentData")  as? NSObject,
                let celluar = currentData.value(forKey: "cellularEntry") as? NSObject,
                let signalStrength = celluar.value(forKey: "displayValue") as? Int {
                return signalStrength
            } else {
                return nil
            }
        } else {
            var signalStrength = -1
            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
                    break
                } else {
                    signalStrength = -1
                }
            }
            signalStrength = dataNetworkItemView.value(forKey: "signalStrengthBars") as! Int
            if signalStrength == -1 {
                return nil
            } else {
                return signalStrength
            }
        }
        return nil
    

    【讨论】:

    • 虽然此代码可以解决 OP 的问题,但最好包含关于您的代码如何解决 OP 问题的说明。通过这种方式,未来的访问者可以从您的帖子中学习,并将其应用到他们自己的代码中。 SO 不是编码服务,而是知识资源。此外,高质量、完整的答案更有可能得到支持。这些功能,以及所有帖子都是独立的要求,是 SO 作为一个平台的一些优势,使其与论坛区分开来。您可以编辑以添加其他信息和/或使用源文档补充您的解释。
    • 如何获取wifi信号强度?
    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多