Search code examples
iosswiftaugmented-realityarkitrealitykit

ARKit Plane Detection - Value of type 'ARView' has no member 'session'


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)
    }
}

Solution

  • 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)