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.2
Activate or deactivate ProximitySensor:
func activateProximitySensor(isOn: Bool) {
let device = UIDevice.current
device.isProximityMonitoringEnabled = isOn
if isOn {
NotificationCenter.default.addObserver(self, selector: #selector(proximityStateDidChange), name: UIDevice.proximityStateDidChangeNotification, object: device)
} else {
NotificationCenter.default.removeObserver(self, name: UIDevice.proximityStateDidChangeNotification, object: device)
}
}
Selector:
@objc func proximityStateDidChange(notification: NSNotification) {
if let device = notification.object as? UIDevice {
print(device)
}
}