简体   繁体   中英

Replacement for ARKit in iOS10

I want to include ARKit in an app designed for iOS10+ where I replace ARKit with SceneKit if the iOS version is <11.

Unfortunately it seems like there is no way to currently do this?

Depending on how you've set your expectations, it is possible to use SceneKit as a "fallback" from ARKit -- specifically, from ARSCNView .

What you can easily do is create a 3D content experience that appears "in the real world" via AR when running on an ARKit capable device, and in an entirely virtual setting (that is, rendered in 3D without a camera feed as background) when running without ARKit.

Note: ARKit support isn't just an iOS 11 thing -- you also need an A9 device. So you might think about a fallback experience for older hardware on iOS 11, not just for older iOS versions.

To do that, you'll need to be able to swap out your view class at runtime. That means not creating ARSCNView in a storyboard, but initializing one and adding it to your view controller's root view in code.

override func viewDidLoad() {
    super.viewDidLoad()
    let sceneView = ARSCNView(frame: view.bounds)
    sceneView.scene = // ... set up your SceneKit scene ...
    view.addSubview(sceneView)
}

Once you're doing something like that, you can wrap the critical part in a conditional that uses ARSCNView when available and SCNView otherwise. Actually, you might want to set up a handy function for that...

var canUseARKit: Bool {
    if #available(iOS 11.0, *) {
        return ARWorldTrackingSessionConfiguration.isSupported
    } else {
        return false
    }
}

Then you can conditionalize your view setup:

override func viewDidLoad() {
    super.viewDidLoad()
    let sceneView: SCNView
    if canUseARKit {
        sceneView = ARSCNView(frame: view.bounds)
    } else {
        sceneView = SCNView(frame: view.bounds)
    }
    sceneView.scene = // ... set up your SceneKit scene ...
    view.addSubview(sceneView)
}

Of course, there's still more to do after that:

  • When you're using ARKit, you get camera control "for free"; when you're using SceneKit on its own you'll have to think about where to place the camera and how to let the user control it. (Check the WWDC17 SceneKit session for some tips on new iOS 11 camera controls.)
  • When you're using SceneKit on its own, your 3D content defines "the world" that the user interacts with -- that is, you place cameras and such in relation to your content. When using ARKit, you have to think about where and how to place your content relative to the real world.

But aside from such concerns, working with SceneKit content within ARSCNView is no different from working with SceneKit content in SCNView , so for the rest of your app/game you can share a lot of code and assets between AR and non-AR user experiences.

You can conditionally choose to include features using the following syntax:

if #available(iOS 11.0, *) {
    // Use ARKit
} else {
   // Use SceneKit
}

Like any framework, ARKit exists in the system version(s) where it exists, and in no other system versions. ARKit exists in iOS 11, not in iOS 10 or before. An iOS 10 user can never use ARKit (without upgrading to iOS 11).

(And even then, ARKit will be usable only on a subset of devices running iOS 11, as its features are highly hardware-dependent.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM