Swiftui - iOS 13 Uiviewrepresentable of Wkwebview Gets Thread 1: Exc_Breakpoint Crash

SwiftUI - iOS 13 UIViewRepresentable of WKWebView gets Thread 1: EXC_BREAKPOINT crash

The problem is due to a combination of running on an M1 Mac and using an iOS version prior to 14. The problem is known to Apple.

macOS app crash on WKWebView() with EXC_BREAKPOINT on M1

I have the same problem. The only workaround that works for me is to run XCode under Rosetta (in /Applications open settings of XCode and set Rosetta checkbox) and then build and run app on target My mac (Rosetta)

In this case all code will compile for x86_64 which doesn't have the problem with WKWebView.

SwiftUI dissmiss sheet with WKWebView UIViewRepresentable

A button with dismiss() won't work. Try this code implementation:

.sheet(isPresented: $showWebViewSheet) {
ZStack {
// webview
WebView(url: URL(string: webViewURL))

// dissmiss
Button(action: {
showWebViewSheet = false
}) {
Image(systemName: "xmark")
.padding(10)
.background(Color.gray.opacity(0.5))
.cornerRadius(20)
.padding(30)
}
}
}

iOS SwiftUI - closing view / dismiss() causes crash Thread 23: EXC_BREAKPOINT

I solved it based on a comment here:

func closeLogin(){
DispatchQueue.main.async {
self.dismiss()
}
}

How do I display WKWebView url?

To keep track of the URL of the WKWebView, you'll need to use a WKNavigationDelegate.

You can use a Coordinator in your UIViewRepresentable for the WKNavigationDelegate and an ObservableObject with a @Published value to communicate between the WebView and your parent view:

class NavigationState : ObservableObject {
@Published var url : URL?
}

struct WebView : UIViewRepresentable {

let request: URLRequest
var navigationState : NavigationState

func makeCoordinator() -> Coordinator {
return Coordinator()
}

func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
context.coordinator.navigationState = navigationState
webView.navigationDelegate = context.coordinator
webView.load(request)
return webView
}

func updateUIView(_ uiView: WKWebView, context: Context) {

}

class Coordinator : NSObject, WKNavigationDelegate {
var navigationState : NavigationState?

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
navigationState?.url = webView.url
}
}

}

struct ContentView: View {
@StateObject var navigationState = NavigationState()

var body: some View {
VStack(){
Text(navigationState.url?.absoluteString ?? "(none)")
WebView(request: URLRequest(url: URL(string: "https://www.google.com")!), navigationState: navigationState)
}
}
}

Update, based on comments:

class NavigationState : NSObject, ObservableObject {
@Published var url : URL?
let webView = WKWebView()
}

extension NavigationState : WKNavigationDelegate {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
self.url = webView.url
}
}

struct WebView : UIViewRepresentable {

let request: URLRequest
var navigationState : NavigationState

func makeUIView(context: Context) -> WKWebView {
let webView = navigationState.webView
webView.navigationDelegate = navigationState
webView.load(request)
return webView
}

func updateUIView(_ uiView: WKWebView, context: Context) { }
}

struct ContentView: View {
@StateObject var navigationState = NavigationState()

var body: some View {
VStack(){
Text(navigationState.url?.absoluteString ?? "(none)")
WebView(request: URLRequest(url: URL(string: "https://www.google.com")!), navigationState: navigationState)
HStack {
Button("Back") {
navigationState.webView.goBack()
}
Button("Forward") {
navigationState.webView.goForward()
}
}
}
}
}


Related Topics



Leave a reply



Submit