Skip to content

Instantly share code, notes, and snippets.

@jonahsiegle
Created November 14, 2017 17:53
Show Gist options
  • Save jonahsiegle/ffb955b3ff88067fa499a0e6d70bf470 to your computer and use it in GitHub Desktop.
Save jonahsiegle/ffb955b3ff88067fa499a0e6d70bf470 to your computer and use it in GitHub Desktop.
Get Signal Strength on the iPhone X
func getSignalStrengthiPhoneX() -> Int {
let application = UIApplication.shared
let statusBarView = application.value(forKey: "statusBar") as! UIView
let statusBar = statusBarView.value(forKey: "statusBar") as! UIView
let foregroundView = statusBar.value(forKey: "foregroundView") as! UIView
for subview in foregroundView.subviews {
if subview.subviews.count > 0 {
for child in subview.subviews {
if child.isKind(of: NSClassFromString("_UIStatusBarCellularSignalView")!) {
return child.value(forKey: "numberOfActiveBars") as? Int ?? 0
}
}
}
return 0
}
//For non iPhone X Devices
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
break
} else {
return 0 //NO SERVICE
}
}
return dataNetworkItemView.value(forKey: "signalStrengthBars") as! Int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment