atw*_*lsh 219
用这个:
Objective-C的:
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
Run Code Online (Sandbox Code Playgroud)
Swift(遗产):
UIApplication.sharedApplication().idleTimerDisabled = true
Run Code Online (Sandbox Code Playgroud)
迅捷3/4:
UIApplication.shared.isIdleTimerDisabled = true
Run Code Online (Sandbox Code Playgroud)
确保导入UIKit.
以下是apple.developer.com文档的链接.
Cra*_*lot 17
对于Swift 3.0,这里有两个选项,具体取决于您要调用代码的位置:
在AppDelegate.swift内:
application.idleTimerDisabled = true
在AppDelegate.swift之外:
UIApplication.shared().isIdleTimerDisabled = true
kav*_*hmb 11
斯威夫特4
在AppDelegate.swift文件中,在应用程序函数中添加以下行:
application.isIdleTimerDisabled = true
Run Code Online (Sandbox Code Playgroud)
SwiftUI 代码:
struct ContentView: View {
var body: some View {
VStack {
Text("Prevent Screen Sleep")
}
.onAppear {
// Disable the idle timer when the view appears
UIApplication.shared.isIdleTimerDisabled = true
}
.onDisappear {
// Re-enable the idle timer when the view disappears
UIApplication.shared.isIdleTimerDisabled = false
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40413 次 |
| 最近记录: |