0

I need to access the ambient light sensor and get the carrier strength on iOS 11. I know that IOKit provides this information, but how?

jscs
  • 63,694
  • 13
  • 151
  • 195
Gianclè Monna
  • 387
  • 1
  • 5
  • 12

1 Answers1

1

To get the signal strength you could try this function:

func carrierSignalStrength() -> Int? {
    let statusBarView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    let foreground = statusBarView.value(forKey: "foregroundView") as! UIView
    var signalStrengthItem: UIView?

    for view in foreground.subviews {
        if view.isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
            signalStrengthItem = view
            break
        }
    }

    if let strength = signalStrengthItem?.value(forKey: "signalStrengthBars") as? Int {
        return strength
    } else {
        return nil
    }
}

Note that this function reads the signal strength from the status bar, if it´s hidden then it will return nil. If you don´t have a service it will also return nil.

To read the ambient light sensor you can use this library, which works good for this purpose.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • thank you for your advice, carrier strength measurement worked, while LightKit is a library designed only for Mac OS X. – Gianclè Monna Feb 11 '18 at 17:33
  • Read [this](https://forums.developer.apple.com/thread/39144) post, I don´t think Apple has an API for that yet. If you find a private one be carful so that you don´t violate their App Store guidelines. – Rashwan L Feb 12 '18 at 07:37