3

I have an ARSCNView that can occasionally pause its session depending on the situation. Is there a way to check if its session is running?

Something like this:

class myARView: ARSCNView {
    ...
    func foo() {

        if(session.running) {
            // do stuff
        }
    }
    ...
}
MuhsinFatih
  • 1,891
  • 2
  • 24
  • 31
  • One way would be to set a boolean whenever you pause/resume the session and check that boolean in your function. – M Reza May 02 '18 at 10:46
  • Right, I thought about that but the ARSCNView is exposed so if I/someone else forget(s) to set that boolean or use the custom start/pause methods it could result in unwanted behavior. If there is no built-in way though then post it as answer, I will accept. If you do so please also mention the absence of this API :) – MuhsinFatih May 02 '18 at 10:54
  • I'm not 100% sure if there's really no way to achieve this using the API, I'm checking it right now. :) – M Reza May 02 '18 at 11:13

1 Answers1

5

At this moment, it seems there isn't a way to check if the session is running, by ARSession object itself. However, by implementing ARSCNViewDelegate, you can get notified whenever your session is interrupted or the interruption ends.

One way to achieve your goal is to set a boolean and update it whenever you pause/resume the session, and check its value in your functions.

class ViewController: UIViewController, ARSCNViewDelegate {

    var isSessionRunning: Bool

    func foo() {
        if self.isSessionRunning {
            // do stuff
        }
    }

    func pauseSession() {
        self.sceneView.session.pause()
        self.isSessionRunning = false
    }

    func runSession() {
        let configuration = ARWorldTrackingConfiguration()
        sceneView.session.run(configuration)
        self.isSessionRunning = true
    }

    // ARSCNViewDelegate:
    func sessionWasInterrupted(_ session: ARSession) {
        self.isSessionRunning = false
    }

    func sessionInterruptionEnded(_ session: ARSession) {
        self.isSessionRunning = true
    }
}
M Reza
  • 18,350
  • 14
  • 66
  • 71
  • 1
    ok so when trying to reproduce this, I found out that `session.pause()` doesn't pause anything at all and also that I forgot to mention I was trying to use that function :D. Since dear Apple has forgotten pausing the scene when pause() is called, using a boolean and keeping track of it as you said seems to be the only reliable way (I tried it just now, I can confirm that after calling this ARSession's stupid built-in `session.pause()` function, my device was still tracking motion). – MuhsinFatih May 02 '18 at 12:35
  • 1
    To clarify, question was not on how to pause, but what I meant is that you answer solves both the tracking (as asked), and when trying it I also figured the boolean is necessary even when not tracking the state because `session.pause()` doesn't even work anyways – MuhsinFatih May 02 '18 at 12:43