Last active
May 19, 2023 17:07
-
-
Save elpsk/2d01da837aefc81f3e4b386e04a4b972 to your computer and use it in GitHub Desktop.
Swift - Proximity sensor (shut down screen if near ear)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ProximityVC.swift | |
// Created by Pasca Alberto, IT on 10/08/18. | |
// Copyright © 2018 albertopasca.it. All rights reserved. | |
// | |
import UIKit | |
class ProximityVC: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
activateProximitySensor() | |
} | |
func activateProximitySensor() { | |
let device = UIDevice.current | |
device.isProximityMonitoringEnabled = true | |
if device.isProximityMonitoringEnabled { | |
NotificationCenter.default.addObserver( | |
self, | |
selector: #selector(proximityChanged), | |
name: UIDevice.proximityStateDidChangeNotification, | |
object: device | |
) | |
} | |
} | |
@objc func proximityChanged(notification: NSNotification) { | |
if let device = notification.object as? UIDevice { | |
print("proximity: \(device.proximityState)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Please update code