I have an attachment anchored to the head motion, and I put a WKWebView as the attachment. When I try to interact with the web view, the app crashes with the following errors:
*** Assertion failure in -[UIGestureGraphEdge initWithLabel:sourceNode:targetNode:directed:], UIGestureGraphEdge.m:28
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: targetNode'
*** First throw call stack:
(0x18e529340 0x185845e80 0x192c2283c 0x2433874d4 0x243382ebc 0x2433969a8 0x24339635c 0x243396088 0x243907760 0x2438e4c94 0x24397b488 0x24397e28c 0x243976a20 0x242d7fdc0 0x2437e6e88 0x2437e6254 0x18e4922ec 0x18e492230 0x18e49196c 0x18e48bf3c 0x18e48b798 0x1d3156090 0x2438c8530 0x2438cd240 0x19fde0d58 0x19fde0a64 0x19fa5890c 0x10503b0bc 0x10503b230 0x2572247b8)
libc++abi: terminating due to uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: targetNode'
*** First throw call stack:
(0x18e529340 0x185845e80 0x192c2283c 0x2433874d4 0x243382ebc 0x2433969a8 0x24339635c 0x243396088 0x243907760 0x2438e4c94 0x24397b488 0x24397e28c 0x243976a20 0x242d7fdc0 0x2437e6e88 0x2437e6254 0x18e4922ec 0x18e492230 0x18e49196c 0x18e48bf3c 0x18e48b798 0x1d3156090 0x2438c8530 0x2438cd240 0x19fde0d58 0x19fde0a64 0x19fa5890c 0x10503b0bc 0x10503b230 0x2572247b8)
terminating due to uncaught exception of type NSException
Message from debugger: killed
This is the code for the RealityView
struct ImmersiveView: View {
@Environment(AppModel.self) private var appModel
var body: some View {
RealityView { content, attachments in
let anchor = AnchorEntity(AnchoringComponent.Target.head)
if let sceneAttachment = attachments.entity(for: "test") {
sceneAttachment.position = SIMD3<Float>(0,0,-3.5)
anchor.addChild(sceneAttachment)
}
content.add(anchor)
} attachments: {
Attachment(id: "test") {
WebViewWrapper(webView: appModel.webViewModel.webView)
}
}
}
}
This is the appModel:
import SwiftUI
import WebKit
/// Maintains app-wide state
@MainActor
@Observable
class AppModel {
let immersiveSpaceID = "ImmersiveSpace"
enum ImmersiveSpaceState {
case closed
case inTransition
case open
}
var immersiveSpaceState = ImmersiveSpaceState.closed
public let webViewModel = WebViewModel()
}
@MainActor
final class WebViewModel {
let webView = WKWebView()
func loadViz(_ addressStr: String) {
guard let url = URL(string: addressStr) else { return }
webView.load(URLRequest(url: url))
}
}
struct WebViewWrapper: UIViewRepresentable {
let webView: WKWebView
func makeUIView(context: Context) -> WKWebView {
webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}
}
and finally the ContentView where I added a button to load the webpage:
struct ContentView: View {
@Environment(AppModel.self) private var appModel
var body: some View {
VStack {
ToggleImmersiveSpaceButton()
Button("Go") {
appModel.webViewModel.loadViz("http://apple.com")
}
}
.padding()
}
}