我await UIApplication.shared.open(settingsURL)
在 Swift 内部调用,Task
但收到 Xcode 运行时警告:
UIApplication.open(_:options:completionHandler:) 只能在主线程中使用
Task {
guard let settingsURL = await URL(string: UIApplication.openSettingsURLString) else {
return
}
await UIApplication.shared.open(settingsURL) // <-- runtime warning when called
}
SDK 显示了这些方法:
@available(iOS 10.0, *)
open func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:], completionHandler completion: ((Bool) -> Void)? = nil)
@available(iOS 10.0, *)
open func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:]) async -> Bool
错误消息暗示它认为我正在使用第一个非异步的(因为提到了完成处理程序),但我不是。UIApplication
被标记,MainActor
所以我的理解是通过实例调用的所有内容都UIApplication
自动在主线程上运行。这适用于我在其他地方调用的UIApplication
其他内容,例如isRegisteredForRemoteNotifications
.
该应用程序运行良好,没有崩溃或任何事情,只是来自 Xcode 的警告。
Xcode 只是在这里感到困惑还是我实际上做错了什么?
截屏:
编辑:对于那些感到困惑的人MainActor
,这是一个有用的阅读:https ://www.swiftbysundell.com/articles/the-main-actor-attribute/
我可以换行UIApplication.shared.open(settingsURL)
,await MainActor.run
但我仍然很困惑为什么MainActor
在这里不起作用/为什么 Xcode 认为调用了错误的方法。