I have written a webview using UIViewRepresentable and WKWebView in SwiftUI.
The app is currently published in the app store. For some users, the app is crashing and we are not able to reproduce it.
Below is the crash log:
Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x000000010455c388
0 uMove 0x28388 specialized WebView.makeUIView(context:) + 31 (WebView.swift:31)
1 uMove 0x27b48 protocol witness for UIViewRepresentable.makeUIView(context:) in conformance WebView + 4329765704 (<compiler-generated>:4329765704)
2 SwiftUI 0xaa4bc OUTLINED_FUNCTION_326 + 6512
3 SwiftUI 0x9ac5c OUTLINED_FUNCTION_1106 + 1428
4 SwiftUI 0xca31a8 OUTLINED_FUNCTION_1 + 8712
5 SwiftUI 0xca5868 OUTLINED_FUNCTION_1 + 18632
6 SwiftUI 0x89200 OUTLINED_FUNCTION_513 + 11236
7 SwiftUI 0xca306c OUTLINED_FUNCTION_1 + 8396
8 SwiftUI 0xca24b8 OUTLINED_FUNCTION_1 + 5400
9 SwiftUI 0x53eb00 OUTLINED_FUNCTION_5 + 26880
10 AttributeGraph 0x3ef8 AG::Graph::UpdateStack::update() + 512
Below is the code snippet where it is crashing:The crash is happening while returning WKWebView instance from makeUIView() function.
struct WebView: UIViewRepresentable {
@Binding var url: String
private let wKWebView = WKWebView()
func makeUIView(context: Context) -> WKWebView {
configure(context: context)
print(LOG_TAG, "makeUIView Webview loading url \(url)")
if(!url.isEmpty) {
wKWebView.load(URLRequest(url: URL(string:url)!))
}
return wKWebView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}
func makeCoordinator() -> WebViewNavigationDelegate {
WebViewNavigationDelegate(self)
}
// Our own methods
func configure(context: Context) {
wKWebView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = true
//wKWebView.configuration.preferences.javaScriptEnabled = true
wKWebView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
wKWebView.allowsBackForwardNavigationGestures = true
wKWebView.allowsLinkPreview = true
wKWebView.navigationDelegate = context.coordinator
wKWebView.uiDelegate = context.coordinator
}
.....
....
....
}
Initially I thought the probem might be due to loading the URL inside the makeUIView()
function considering the view might not be ready and we are trying to load. So I moved the loading URL logic to the updateUIView
function, but no luck.