내 batteryStateDidChange 핸들러가 호출되지 않습니다 (Simulator 또는 iPhone에서). 이 사이트에서 몇 가지 예 (일반적으로 오래된 문서)를 발견했으며 Apple 설명서를 읽었습니다. 또한 pid 오류가 발생합니다. 빌드 할 때 "시스템 무결성 보호로 인해 프로세스에 연결할 수 없습니다."라는 메시지가 표시됩니다. 1970 년대부터 프로그래밍을 해왔지만 Swift 나 Xcode를 사용한 적은 한번도 없습니다. 스위프트 4와 Xcode 9.1을 사용하고 있습니다. 도움을 주시면 감사하겠습니다.처리기가 batteryState 알림을 수신하지 않습니다.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Tell UIDevice that we want battery notifications
UIDevice.current.isBatteryMonitoringEnabled = true
// Register to listen for notifications
NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange(_notification:)), name: Notification.Name.UIDeviceBatteryStateDidChange, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var batteryState: UIDeviceBatteryState {
return UIDevice.current.batteryState
}
// Handle the batteryStateDidChange notification <---- This never gets called
@objc func batteryStateDidChange(_notification: Notification) {
switch batteryState {
case .unplugged, .unknown:
print("not charging")
case .charging, .full:
print("charging or full")
}
}
}
** 우수 ** (내 코드를 수정 해 주셔서 감사합니다 그 성가신 공간!). 1 주일 동안 머리카락을 꺼내면 코드가 수정되었습니다. 베테랑 프로그래머로서 어쨌든 머리카락이 거의 남아 있지 않습니다 ...;) – SixtyDegreesNorth