我遇到了一个问题,我的应用程序在 7 月份运行得很好,但现在当我尝试构建它时却不再运行了。
Xcode 16.1 中不再调用某些方法,例如下面的方法。
func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet)
alertController.addAction(
UIAlertAction(title: "Ok".localized, style: .default, handler: { (action) in completionHandler() })
)
if let controller = topMostViewController() {
controller.present(alertController, animated: true, completion: nil)
}
}
我无法从 Apple 找到任何相关信息,但我尝试使用这些方法的异步版本,并且成功了。
所以,看来您必须使用这些类型方法的异步版本:
func webView(
_ webView: WKWebView,
runJavaScriptAlertPanelWithMessage message: String,
initiatedByFrame frame: WKFrameInfo
) async {
let alertController = UIAlertController(
title: nil,
message: message,
preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet
)
alertController.addAction(UIAlertAction(title: "Ok".localized, style: .default, handler: nil))
if let controller = topMostViewController() {
await MainActor.run {
controller.present(alertController, animated: true, completion: nil)
}
}
}