I\'m a relatively new user to swift and now, I need to take advantage of the proximity sensor of an iPhone. I don\'t matter the distance, but I want to know when something is ne
Swift 4
//MARK:- Sensor deduct when hide and show the screen when call
func activateProximitySensor() {
let device = UIDevice.current
device.isProximityMonitoringEnabled = true
if device.isProximityMonitoringEnabled {
NotificationCenter.default.addObserver(self, selector: #selector(proximityChanged(notification:)), name: NSNotification.Name(rawValue: "UIDeviceProximityStateDidChangeNotification"), object: device)
}
}
@objc func proximityChanged(notification: NSNotification) {
if let device = notification.object as? UIDevice {
print("\(device) detected!")
}
}
For Removing Observer
NotificationCenter.default.removeObserver(self, name: .UIDeviceProximityStateDidChange, object: nil)