简体   繁体   English

Swift UIApplication 不在 scope

[英]Swift UIApplication not in scope

I want to change isIdleTimerDisabled when a specific view appears.我想在出现特定视图时更改 isIdleTimerDisabled。 In SwiftUI I use .onAppear { UIApplication.shared.isIdleTimerDisabled = true }.onDisappear { UIApplication.shared.isIdleTimerDisabled = false } but even with import UIKit I get the warning "Cannot find 'UIApplication' in scope".在 SwiftUI 中,我使用.onAppear { UIApplication.shared.isIdleTimerDisabled = true }.onDisappear { UIApplication.shared.isIdleTimerDisabled = false }但即使使用import UIKit我也会收到警告“无法在范围内找到‘UIApplication’”。 How can I fix that?我该如何解决? I didn't find a solution after searching for more than an hour.找了一个多小时也没找到解决办法。

You should not need to import UIKit , it should work with SwiftUI .您不需要导入UIKit ,它应该与SwiftUI一起使用。

The problem may be your target.问题可能是您的目标。 Is the file used only on an iOS or iPadOS application target?该文件是否仅用于 iOS 或 iPadOS 应用程序目标?

Or is it also used, for instance, in an extension, like ShareExtension, and so on?或者它是否也用于 ShareExtension 等扩展中?

If it is, you cannot access UIApplication within your code.如果是,则无法在代码中访问UIApplication If this code is shared between extensions and app, you should use conditional compilation to avoid this problem.如果此代码在扩展程序和应用程序之间共享,则应使用条件编译来避免此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM