有问题计算Wi-Fi强度

发布于 2025-01-25 20:55:25 字数 1158 浏览 6 评论 0原文

我想获得设备Wi-Fi强度,但是

现在没有找到直接的方法来实现此操作,我已经有了它曾经在iOS 13之前工作的,但是现在我崩溃了。

func wifiStrength() -> Double {
    let app = UIApplication.shared
    let subviews = ((app.value(forKey: "statusBar") as! NSObject).value(forKey: "foregroundView") as! UIView).subviews
    var dataNetworkItemView: UIView?

    for subview in subviews {
        if subview.isKind(of: NSClassFromString("UIStatusBarDataNetworkItemView")!) {
            dataNetworkItemView = subview
            break
        }
    }

    let dBm = (dataNetworkItemView!.value(forKey: "wifiStrengthRaw") as! NSNumber).intValue
    var strength = (Double(dBm) + 90.0) / 60.0
    if strength > 1 {
        strength = 1
    }
    return strength
}

崩溃错误消息。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

终止以未被发现的方式终止NSexception类型,

有人知道如何适应iOS 13上的先前方法吗? 或一种不同的方式来收集Wi-Fi信号强度。

I want to get the device Wi-Fi strength, but found no straightforward way to accomplish this

Right now I have this which used to work before iOS 13 however now I get this crash.

func wifiStrength() -> Double {
    let app = UIApplication.shared
    let subviews = ((app.value(forKey: "statusBar") as! NSObject).value(forKey: "foregroundView") as! UIView).subviews
    var dataNetworkItemView: UIView?

    for subview in subviews {
        if subview.isKind(of: NSClassFromString("UIStatusBarDataNetworkItemView")!) {
            dataNetworkItemView = subview
            break
        }
    }

    let dBm = (dataNetworkItemView!.value(forKey: "wifiStrengthRaw") as! NSNumber).intValue
    var strength = (Double(dBm) + 90.0) / 60.0
    if strength > 1 {
        strength = 1
    }
    return strength
}

The crash error message.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

terminating with uncaught exception of type NSException

Does anyone know how to adapt the previous approach on iOS 13?
Or of a different way to gather Wi-Fi signal strength.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2025-02-01 20:55:25

无法在iOS应用程序上获得WiFi强度。请在Apple开发人员论坛中查看此线程:

一般应用没有支持在iOS上获得Wi-Fi信号强度的支持

使用value(forkey:)< / code>不支持,不稳定,并且可以将您的应用程序从应用程序中拒绝店铺。

There is no way to get wifi strength on an iOS application. Please see this thread in Apple Developer Forums: https://developer.apple.com/forums/thread/107046?answerId=325726022#325726022

there is no supported way for general apps to get Wi-Fi signal strength on iOS

Rooting around internal / private structures using value(forKey:) is unsupported, unstable, and could get your app rejected from the app store.

一场春暖 2025-02-01 20:55:25

我解决了问题,这是

    func wifiStrength() -> Double {
let statusBarManager = UIApplication.shared
.keyWindow?
.windowScene?
.statusBarManager
let hascreateLocalStatusBar = statusBarManager?
.responds(to: Selector("createLocalStatusBar"))

if !(hascreateLocalStatusBar ?? false) {
return -1
}

guard let createLocalStatusBar = statusBarManager?
.perform(Selector("createLocalStatusBar"))
.takeUnretainedValue() as?
UIView else { print("not UIVirw"); return -1};

let hasStatusBar = createLocalStatusBar.responds(to: Selector("statusBar"))
if !hasStatusBar {
return -1
}

let statusBar = (createLocalStatusBar.perform(Selector("statusBar"))
.takeUnretainedValue() as! UIView)

guard let value = (((statusBar.value(forKey: "_statusBar") as? NSObject)?
.value(forKey: "_currentAggregatedData") as? NSObject)?
.value(forKey: "_wifiEntry") as? NSObject)?
.value(forKey: "_rawValue") else { return -1}

print("

I solve the problem this is the updated code which works on iOS 15

    func wifiStrength() -> Double {
        let statusBarManager = UIApplication.shared
            .keyWindow?
            .windowScene?
            .statusBarManager
        let hascreateLocalStatusBar = statusBarManager?
            .responds(to: Selector("createLocalStatusBar"))
        
        if !(hascreateLocalStatusBar ?? false) {
            return -1
        }
        
        guard let createLocalStatusBar = statusBarManager?
            .perform(Selector("createLocalStatusBar"))
            .takeUnretainedValue() as?
            UIView else { print("not UIVirw"); return -1};

        let hasStatusBar = createLocalStatusBar.responds(to:  Selector("statusBar"))
        if !hasStatusBar {
            return -1
        }
        
        let statusBar = (createLocalStatusBar.perform(Selector("statusBar"))
            .takeUnretainedValue() as! UIView)
            
    
        guard let value = (((statusBar.value(forKey: "_statusBar") as? NSObject)?
            .value(forKey: "_currentAggregatedData") as? NSObject)?
            .value(forKey: "_wifiEntry") as? NSObject)?
            .value(forKey: "_rawValue") else { return -1}
                
        print("????\(value)")
        let dBm = value as! Int
        var strength = (Double(dBm) + 90.0) / 60.0
                
        if strength > 1 {
            strength = 1
        }
        return strength
    }

In regards to AppStore legality, I am aware that this practice is not allowed by apple, and want to clarify that I'm not planning to use this in any production code just for pure experimentation sake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文