Reputation: 21
I've got some trouble having a preview in Xcode 11.4. My code is working when my phone is plugged, so it's not a code problem, but when unplugged, build always failed. I'd like to be able to work on my project, on the other files not using AR, without this error. When I resume the preview on those other files, I'm blocked because of this error. I've already put some strings in the info.plist file (privacy camera usage and required device capabilities) but still not working. Have an idea ?
import SwiftUI
import RealityKit
struct ContentView : View {
var body: some View {
return ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
arView.enablePlacement()
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
extension ARView {
func enablePlacement() {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
self.addGestureRecognizer(tapGestureRecognizer)
}
@objc func handleTap(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self)
let results = self.raycast(from: location, allowing: .estimatedPlane, alignment: .vertical)
if let firstResult = results.first {
let mesh = MeshResource.generateBox(width: 0.5, height: 0.02, depth: 0.2)
var material = SimpleMaterial()
material.baseColor = try! MaterialColorParameter.texture(TextureResource.load(named: "glacier"))
let modelEntity = ModelEntity(mesh: mesh,materials: [material])
let anchorEntity = AnchorEntity(world: firstResult.worldTransform)
anchorEntity.addChild(modelEntity)
self.backgroundColor = .orange
self.scene.addAnchor(anchorEntity)
}else{
print("No Surface detected - move around device")
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
error value of type 'ARView' has no member 'raycast'.
Cannot infer contextual base in reference to member 'estimatedPlane'.
Cannot infer contextual base in reference to member 'vertical'.
Upvotes: 2
Views: 614
Reputation: 33
This error occurs because you have a device selected in the simulator and the simulator cannot be used with AR apps. Change the dropdown next to the play and stop buttons at the top of Xcode to Any iOS Device (arm64)
You will need to connect a physical device to Xcode or push your app to the AppStoreConnect and use Testflight to test your code instead of the simulator.
Upvotes: 0
Reputation: 3554
A lot of RealityKit symbols are not available in the Simulator. I think your only solution is to remove them from Simulator builds by using
#if !targetEnvironment(simulator)
/ ** /
#endif
Upvotes: 2