Since the status bar on the iPhone X is different from other iPhones, the way you get WiFi information is different.
Here is a workaround on how to get it through undocumented properties, "which means that Apple may change these properties in the future without informing us. The application will crash if Apple changes the undocumented properties , but we have not adjusted our code accordingly. Therefore we must handle NSException in Swift.
Create a header file and add it to your Bridging-Header.h. You can find the file here: https://gist.github.com/zhihuitang/6d3de0963d96a552d47721a598ca79c8
// // OCCatch.h // // #ifndef OCCatch_h #define OCCatch_h // add the code below to your -Bridging-Header.h /**
On iPhoneX, we can get WiFi numberOfActiveBars , which ranges from 0 to 3. On other iPhones, except iPhoneX, we can get WiFi RSSI.
On iPhoneX, use getWiFiNumberOfActiveBars() :
private func getWiFiNumberOfActiveBars() -> Int? { let app = UIApplication.shared var numberOfActiveBars: Int? let exception = tryBlock { guard let containerBar = app.value(forKey: "statusBar") as? UIView else { return } guard let statusBarMorden = NSClassFromString("UIStatusBar_Modern"), containerBar .isKind(of: statusBarMorden), let statusBar = containerBar.value(forKey: "statusBar") as? UIView else { return } guard let foregroundView = statusBar.value(forKey: "foregroundView") as? UIView else { return } for view in foregroundView.subviews { for v in view.subviews { if let statusBarWifiSignalView = NSClassFromString("_UIStatusBarWifiSignalView"), v .isKind(of: statusBarWifiSignalView) { if let val = v.value(forKey: "numberOfActiveBars") as? Int { numberOfActiveBars = val break } } } if let _ = numberOfActiveBars { break } } } if let exception = exception { print("getWiFiNumberOfActiveBars exception: \(exception)") } return numberOfActiveBars }
On iPhone devices other than iPhoneX, use getWiFiRSSI() :
private func getWiFiRSSI() -> Int? { let app = UIApplication.shared var rssi: Int? let exception = tryBlock { guard let statusBar = app.value(forKey: "statusBar") as? UIView else { return } if let statusBarMorden = NSClassFromString("UIStatusBar_Modern"), statusBar .isKind(of: statusBarMorden) { return } guard let foregroundView = statusBar.value(forKey: "foregroundView") as? UIView else { return } for view in foregroundView.subviews { if let statusBarDataNetworkItemView = NSClassFromString("UIStatusBarDataNetworkItemView"), view .isKind(of: statusBarDataNetworkItemView) { if let val = view.value(forKey: "wifiStrengthRaw") as? Int { rssi = val break } } } } if let exception = exception { print("getWiFiRSSI exception: \(exception)") } return rssi }
So far, I have not found a way to get WiFi RSSI on iPhoneX. If you guys know how to do this, please let me know. thanks.
here is a demo project on github