25

I am trying to add plane detection to a simple ARKit app. I want to put a picture on a vertical plane.

So first I need to detect the plane then I can add my object anchor which I created in RealityKit.

However the problem is I am not sure the right method in detecting a plane and adding it to my scene with ARKit 3 and Xcode 11.

It should be as simple as:

import ARKit
import RealityKit

@IBOutlet var arView: ARView!

override func viewDidLoad() {
    super.viewDidLoad()

    let arConfiguration = ARWorldTrackingConfiguration()
    arConfiguration.planeDetection = .horizontal
    arView.session.run(arConfiguration)
} 

But I get the following error:

Value of type 'ARView' has no member 'session'

I even tried the following which was used as an example by Apple from their WWDC demo (4:27),

Apple Demo!

let anchor = AnchorEntity(plane: .verticle, minimumBounds: [0.2, 0.2])
arView.scene.addAnchor(anchor)

but I get the following error when trying to create an AnchorEntity

Expression type 'AnchorEntity' is ambiguous without more context

import UIKit
import RealityKit
import ARKit

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func addFrame() {
        // Load the "Box" scene from the "Experience" Reality File
        let boxAnchor = try! Experience.loadBox()

        // Add the box anchor to the scene
        arView.scene.anchors.append(boxAnchor)
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Jaz King
  • 353
  • 3
  • 5

4 Answers4

52

You get this error if your build destination is set to a Simulator. You need to select either an Actual Device or Generic iOS Device.

I've banged my head on this a couple of time and now add the following comment

    // If the following line fails to compile "Value of type 'ARView' has no member 'session'"
    // You need to select a Real Device or Generic iOS Device and not a simulator
    arView.session.run(configuration)
GayleDDS
  • 4,443
  • 22
  • 23
  • what if I have selected a real device and still get the error? – colin Jan 15 '21 at 08:29
  • How are you creating your RealityKit ARView, in code or using a storyboard? – GayleDDS Jan 16 '21 at 17:06
  • code. the error came up after I moved back from Xcode 12.3 to 12.2. Due to many freezing issues in version 12.3 I had to downgrade. Everything is working as expected, except that realitykit error. – colin Jan 17 '21 at 13:46
  • 1
    Did you do a clean and rebuild of everything? – GayleDDS Jan 18 '21 at 06:32
  • I cleaned the build folder if you mean that. when I am building the project everything is working as expected, but the canvas is always throwing errors in the scheme. Creating a new scheme and running that is fixing the issue neither. – colin Jan 18 '21 at 18:58
  • 1
    I've had weird issues like this with previews. The one thing you can try is deleting the directories in DerivedData. To open your DerivedData directory do the following. Menu Xcode - Preferences... - Locations (Tab) - Derived Data section click the arrow. – GayleDDS Jan 21 '21 at 00:08
  • that solved the issue. thank you very much! – colin Jan 22 '21 at 11:05
4

If you use iOS Simulator for AR app based on Interface Builder Storyboard or if you use iOS SwiftUI Preview – then you can't implement session-oriented properties and anchoring components for ARView. As you can see there are a lot of errors in this case.

When Simulator is chosen in Xcode Active Scheme.

enter image description here


The only way to use session-oriented properties for your AR app is to choose a real iOS or iPadOS device in the Xcode Active Scheme drop-down menu.

When real Apple device is chosen for build.

enter image description here


The same is true for installGestures() method

iOS Simulator:

enter image description here

Real Device:

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • However it runs fine using ARSCNView, not sure what the issue is though – Jaz King Jul 10 '19 at 15:50
  • @JazKing, `ARSCNView` is symbiotic view based on two ingredients – ARKit and SceneKit, and as you said, it definitely runs fine. ARSession inside ARSCNView (that must be set by developer), ARFrames and ARAnchors here come from ARKit, models – from SceneKit. However, `ARView` is a view where session is automatically managed by RealityKit's app, and any anchor here can change the settings of session. The reason why it "can't run fine" in `ARView` lies in its nature – Xcode's Simulator doesn't support anchoring and doesn't support gestures for anchored models. RealityKit models must be anchored. – Andy Jazz Apr 21 '21 at 11:32
2

I managed to eliminate this problem by using conditional compilation.

#if !targetEnvironment(simulator)
    return ARView(frame: .zero)
#else
    return UIView(frame: .zero)
#endif

The compilation issues stopped, but I will admit that I still have problems with previews. But I think that is more related to code complexity than to this error.

Owen Godfrey
  • 3,361
  • 2
  • 22
  • 18
0

Elaborating on the answer by Owen I can add that for me in SwiftUI the whole UIViewRepresentable struct became a compiler war zone! So many errors. I found this way to fix it, maybe it can help others:


struct ARVideoPlayerContainer: UIViewRepresentable {
    // If I am on a real device execute my code
    #if !targetEnvironment(simulator)
    [...]
    #else
    // on the simulator execute a fake conformance to UIViewRepresentable... 
    func makeUIView(context: Context) -> UIView { UIView() }
    func updateUIView(_ uiView: UIView, context: Context) {}
    #endif
multitudes
  • 2,898
  • 2
  • 22
  • 29