Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import UIKit
  2.  
  3.  
  4. class ViewController: UIViewController {
  5.  
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9.  
  10. activateProximitySensor()
  11.  
  12.  
  13. }
  14.  
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. // Dispose of any resources that can be recreated.
  18. }
  19.  
  20. func proximityChanged(notification: NSNotification) {
  21. if let device = notification.object as? UIDevice {
  22. print("\(device) detected!")
  23. }
  24. }
  25.  
  26.  
  27. func activateProximitySensor() {
  28. let device = UIDevice.current
  29. device.isProximityMonitoringEnabled = true
  30. if device.isProximityMonitoringEnabled {
  31. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.proximityChanged), name: NSNotification.Name(rawValue: "ProximitySensorDidChangeNotification"), object: device)
  32. }
  33. }
  34.  
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement