Proximity sensor in Swift (from Objective-C)

9.5k views Asked by At

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 near the iPhone.

So I found this code in Objective-C that worked, but I need it in Swift. I have tried some ways, but any worked. So here is the code I need:

- (void) activateProximitySensor {
    UIDevice *device = [UIDevice currentDevice];
    device.proximityMonitoringEnabled = YES;
    if (device.proximityMonitoringEnabled == YES) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:device];
    }
}

- (void) proximityChanged:(NSNotification *)notification {
    UIDevice *device = [notification object];
    NSLog(@"Detectat");

    //DO WHATEVER I WANT
}

EDIT 1: What I tried was this:

override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.currentDevice().proximityMonitoringEnabled = true;

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(proximityStateDidChange()), name:UIDeviceProximityStateDidChangeNotification, object: nil);
}

and the function:

func proximityStateDidChange() {
        //DO WHATEVER I WANT
}

What I put in the function it's executed always when the app is executed.

EDIT 2: Trying the code of Eric D. comment

let sensor = MySensor() //declared in the VC but globally

override func viewDidLoad() {
        super.viewDidLoad()
        sensor.activateProximitySensor()
}

Throws me the exception: exception xcode throw me

Hope someone can help,

Thanks in advance!

5

There are 5 answers

7
Eric Aya On BEST ANSWER

Here's my take on this.

func activateProximitySensor() {
    let device = UIDevice.currentDevice()
    device.proximityMonitoringEnabled = true
    if device.proximityMonitoringEnabled {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "proximityChanged:", name: "UIDeviceProximityStateDidChangeNotification", object: device)
    }
}

func proximityChanged(notification: NSNotification) {
    if let device = notification.object as? UIDevice {
        println("\(device) detected!")
    }
}
1
Dani G. On

Finally I get it working with the answer of Eric D.

Here is the code:

func proximityChanged(notification: NSNotification) {
  if let device = notification.object as? UIDevice {
    println("\(device) detected!")
  }
}
        
func activateProximitySensor() {
  let device = UIDevice.currentDevice()
  device.proximityMonitoringEnabled = true
  if device.proximityMonitoringEnabled {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "proximityChanged:", name: "UIDeviceProximityStateDidChangeNotification", object: device)
    }
  }
}

and in the viewDidLoad:

override func viewDidLoad() {
  super.viewDidLoad()
  activateProximitySensor()
}

Hope it helps!

0
Shakeel Ahmed On

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)
2
Ivan Kholod On

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)
    }
}
0
Warpling On

Swift 3 Version

(Based on Eric Aya's answer)

func setProximitySensorEnabled(_ enabled: Bool) {
    let device = UIDevice.current
    device.isProximityMonitoringEnabled = enabled
    if device.isProximityMonitoringEnabled {
        NotificationCenter.default.addObserver(self, selector: #selector(proximityChanged), name: .UIDeviceProximityStateDidChange, object: device)
    } else {
        NotificationCenter.default.removeObserver(self, name: .UIDeviceProximityStateDidChange, object: nil)
    }
}

func proximityChanged(_ notification: Notification) {
    if let device = notification.object as? UIDevice {
        print("\(device) detected!")
    }
}