7

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

aheze
  • 6,076
  • 3
  • 6
  • 40
  • 1
    What happens when you use `init(frame: CGRect, configuration: WKWebViewConfiguration)` and a non zero frame. ? – Warren Burton Dec 11 '20 at 19:46
  • @WarrenBurton thanks for the suggestion, but nothing changed. [screenshot](https://raw.githubusercontent.com/aheze/DeveloperAssets/master/Screen%20Shot%202020-12-11%20at%2011.54.11%20AM.png) – aheze Dec 11 '20 at 19:56
  • 1
    Works fine with Xcode 12.1 / iOS 13.4. Which one did you use? – Asperi Dec 12 '20 at 04:21
  • @Asperi I'm on Xcode 12.2 (12B45b) and iOS 13.0. Also tested on iOS 13.1 – aheze Dec 12 '20 at 05:00

6 Answers6

7

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.

Norman
  • 1,856
  • 14
  • 17
3

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.

Koze
  • 31
  • 2
  • Nice! Does this have any drawbacks? Like hidden console messages or something? – aheze Mar 09 '21 at 16:11
  • 1
    @aheze As far as I know, there are no drawbacks so far. – Koze Mar 10 '21 at 14:46
  • Sounds great. I'll leave the check mark on the current answer, because it's most likely a bug on Apple's part. I voted for your answer though. – aheze Mar 10 '21 at 16:21
  • sorry, i don't understand, we need to check or uncheck the settings in the scheme? – xhinoda Mar 11 '21 at 14:23
  • @xhinoda Yes, it is. You can avoid the crash by checking any one of the items in the list. Please refer to the linked image of "Diagnostic Options". – Koze Mar 11 '21 at 16:41
  • Thanks @Koze , the error go away, but now show only a black screen :( – xhinoda Mar 11 '21 at 17:40
1

As not reproducible, just guessing... try different constructors, like

let wkWebConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: wkWebConfiguration)

or even with some defined frame (anyway it does not matter later for SwiftUI view hierarchy)

let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 100, height: 100), 
                        configuration: wkWebConfiguration)
Asperi
  • 123,447
  • 8
  • 131
  • 245
  • Thanks for the suggestions! I already tried them though, with @Warren Burton's [comment](https://stackoverflow.com/questions/65224739/swiftui-ios-13-uiviewrepresentable-of-wkwebview-gets-thread-1-exc-breakpoint#comment115369493_65224739). I also just tested on my Intel Mac, and my original code works. I think the problem is probably Big Sur or Apple Silicon. – aheze Dec 12 '20 at 18:15
1

As Norman mentioned it's true if you are using xcode 12 and running simulator having ios version less than 14 it will break the application, it's problem with M1 chip. I my self test the application on 2 systems one on older mac and one on newer mac with m1 chip

Geetanshu Gulati
  • 414
  • 5
  • 11
1

I think this problem is solved with BigSur 11.3

0

Your UIViewRepresentable should be a struct not a class

struct WebViewWrapper: UIViewRepresentable {
^^^^^^
Casper Zandbergen
  • 2,667
  • 20
  • 37
  • I changed it but am still getting the error: [screenshot](https://raw.githubusercontent.com/aheze/DeveloperAssets/master/Screen%20Shot%202020-12-10%20at%208.12.05%20AM.png) – aheze Dec 10 '20 at 16:13