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
What Is Prefix.Pch File in Xcode
Possible to Handle Your Own Http Url Schemes in iOS
Warning: the Copy Bundle Resources Build Phase Contains This Target's Info.Plist File
Uitabbar Items Jumping on Back Navigation on iOS 12.1
How Does Square's Cardcase App Automatically Populate the User's Details from the Address Book
Uicollectionview Decoration View
Uiviewcontentmodescaleaspectfill Not Clipping
Send a File as Attachment in Objective C
Autolayout: Add Constraint to Superview and Not Top Layout Guide
First App Update, User Data Lost (Was Stored in Documents Directory)
Implementing Google Custom Search API in iOS
Swift:How to Change Language Inside App
How to Show a Collectionview Like Facebook Upload Image Using Swift 4
In Swift, How to Let Other Apps Continue to Play Audio When My App Is Open
Exc_Bad_Access Error When Trying to Change Bool Property
How to Render a Whole Uitableview as an Uiimage in iOS
How to Create a User with Multiple Attributes in Firebase with Swift