I'm trying to have a list of NavigationLinks that show a website in full screen. But when I tap on the NavigationLink I get the warning: "This method should not be called on the main thread as it may lead to UI unresponsiveness."
Here is a small example to reproduce the error:
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("Apple") {
WebViewScreen(url: URL(string: "https://www.apple.com")!)
}
}
}
}
}
struct WebViewScreen: View {
let url: URL
@State private var isWebViewLoading = true
var body: some View {
ZStack {
WebViewRepresentable(
isLoading: $isWebViewLoading,
url: url
)
if isWebViewLoading {
ProgressView()
.scaleEffect(1.5)
.tint(.accentColor)
}
}
}
}
struct WebViewRepresentable: UIViewRepresentable {
@Binding var isLoading: Bool
let url: URL
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView(frame: .zero)
webView.navigationDelegate = context.coordinator
let request = URLRequest(url: url)
webView.load(request)
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(isLoading: $isLoading)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
@Binding var isLoading: Bool
init(isLoading: Binding<Bool>) {
_isLoading = isLoading
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
isLoading = true
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
isLoading = false
}
}
The warning does not appear without if I only show the WebViewScreen.