Reputation: 11
I have tried all the possible way of getting the wifi and cellular network signal strength.
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
}
}
the code crashed at application.value(forKey: "statusBar") so I tried the extension but now it crashed at statusBarView?.value(forKey: "foregroundView"), no key pair value for foregroundView. Is there a better to get wifi or cellular signal?
Upvotes: 0
Views: 3000
Reputation: 21
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
Upvotes: 2