Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to port WKWebView over to SwiftUI. Here's my code:

import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        WebViewWrapper()
    }
}

/**
 WKWebView ported over to SwiftUI with `UIViewRepresentable`.
 */
final class WebViewWrapper: UIViewRepresentable {
    
    /// `UIViewRepresentable` required function #1.
    func makeUIView(context: Context) -> WKWebView  {
        print("make")
        let webView = WKWebView() /// EXC_BREAKPOINT error here
        return webView
    }
      
    /// `UIViewRepresentable` required function #2
    func updateUIView(_ uiView: WKWebView, context: Context) {
    }
}

That's it. I created a new SwiftUI project and pasted it in. However, I get this error:

Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

... with nothing printed in the console. This happened for both iOS 13.0 and iOS 13.1.

Crashing on iOS 13

But, on iOS 14.2, it works fine. The crash also seems to happen only for WKWebView. For example, if I replace it with UITextView, it runs without problems.

import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        TextViewWrapper()
    }
}

/**
 UITextView ported over to SwiftUI with `UIViewRepresentable`.
 */
final class TextViewWrapper: UIViewRepresentable {
    
    /// `UIViewRepresentable` required function #1.
    func makeUIView(context: Context) -> UITextView  {
        print("make")
        let textView = UITextView() /// no error, works fine
        return textView
    }
      
    /// `UIViewRepresentable` required function #2
    func updateUIView(_ uiView: UITextView, context: Context) {
    }
}

UITextView has no problems

I'm running Big Sur 11.0.1 on an M1 Mac, but I don't think that should be a problem. My Xcode version is 12.2 (12B45b).

Edit: Big Sur / M1 might be the problem.

I just ran it on the same version of Xcode on my Intel Mac, Catalina 10.15.5, and it works fine.

enter image description here

like image 638
aheze Avatar asked Dec 09 '20 20:12

aheze


People also ask

Is WKWebView the same as Safari?

WKWebView is acting different (outdated browser) than Safari (iOS version of Safari ofc). If I visit adidas.com/yeezy on Safari, there is no problem. But when I visit this site in WKWebView, it says that the browser is not modern enough. I really need to fix this otherwise my app can't work.

What is the difference between UIWebView and WKWebView?

Difference Between UIWebview and WKWebView UIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


3 Answers

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.

like image 112
Norman Avatar answered Oct 11 '22 00:10

Norman


I found a simple workaround.
Setting any of the following diagnostic options in the scheme settings will prevent the crash.

  • Address Sanitizer
  • Thread Sanitizer
  • Malloc Scribble
  • Malloc Guard Edges
  • Guard Malloc
  • Malloc Stack Logging

Diagnostic Options

I verified this with Xcode 12.4 (12D4e), iOS 13.7 Simulator, macOS Big Sur 11.2.3(20D91) and M1 Apple Silicon Mac.

like image 3
Koze Avatar answered Oct 11 '22 00:10

Koze


Your UIViewRepresentable should be a struct not a class

struct WebViewWrapper: UIViewRepresentable {
^^^^^^
like image 1
Casper Zandbergen Avatar answered Oct 10 '22 22:10

Casper Zandbergen