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?
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:
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.)