Bạn có thể vô hiệu hóa bộ đếm thời gian nhàn rỗi như sau;
Trong Mục tiêu-C:
[UIApplication sharedApplication].idleTimerDisabled = YES;
Trong Swift:
UIApplication.sharedApplication().idleTimerDisabled = true
Trong Swift 3.0 & Swift 4.0:
UIApplication.shared.isIdleTimerDisabled = true
Đặt nó trở lại NOhoặc falseđể kích hoạt lại chế độ ngủ.
Ví dụ: nếu bạn cần nó cho đến khi bạn rời khỏi chế độ xem, bạn có thể đặt lại nó bằng cách ghi đè viewWillDisappear:
override func viewWillDisappear(_ animated: Bool) {
UIApplication.shared.isIdleTimerDisabled = false
}
Tìm hiểu thêm về lớp học UIApplication .