Search code examples
iosarkitarscnview

Programmatically created ARSCNView doesn't work


I have created ARSCNView programmatically as the below code. The app runs on my real iPhone (without crashing) but shows a blank/black screen. It doesn't ask for accessing the camera either (I have the line "Privacy - Camera Usage Description" in Info.plist already). With the commented line sceneView.backgroundColor = .green I am sure the ARSCNView is created, added, and full screen.

import ARKit

class ViewController: UIViewController {
    let sceneView = ARSCNView()

    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.frame = self.view.frame
        self.view.addSubview(sceneView)
        // make sure the sceneView is added and full screen
        //sceneView.backgroundColor = .green
    }
}


Solution

  • You need to add SCNScene to your ARSCNView

    let scene = SCNScene()
    sceneView.scene = scene
    

    And start ARWorldTrackingConfiguration in viewWillAppear

    //Inside view will appear
    let config = ARWorldTrackingConfiguration()
    self.sceneView.session.run(config)