I have a question regarding AR Quick Look, I have a simple view with an ARQLView showing a .usdz file of a Nike shoe. This code works perfectly fine on my iPad if I sideload the app using Xcode. The problem is, when I try to use the same code in Swift Playgrounds on the iPad, it gives the same view but with the "AR" mode grayed out, with the "Object" mode the only option. So I can pan and zoom the 3D model but I cannot see it in AR. Is this a limitation of Playgrounds, or is Playgrounds itself encountering a bug (which isn't uncommon in my experience).
I referred to this article describing how to use AR Quick Look with SwiftUI with the following code in my Xcode project and Swift Playground:
Here's the ARQL View:
import UIKit
import QuickLook
import ARKit
class ARQLViewController: UIViewController, QLPreviewControllerDataSource {
let assetName = "sneaker_airforce"
let assetType = "usdz"
let allowsContentScaling = true
let canonicalWebPageURL = URL(string: "")
override func viewDidAppear(_ animated: Bool) {
let previewController = QLPreviewController()
previewController.dataSource = self
present(previewController, animated: true, completion: nil)
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
guard let path = Bundle.main.path(forResource: assetName, ofType: assetType) else {
fatalError("Couldn't find the supported asset file.")
}
let url = URL(fileURLWithPath: path)
let previewItem = ARQuickLookPreviewItem(fileAt: url)
previewItem.allowsContentScaling = allowsContentScaling // default = true
previewItem.canonicalWebPageURL = canonicalWebPageURL // default = nil
return previewItem
}
}
Then that's wrapped up for use in SwiftUI:
import SwiftUI
struct ARQLView: UIViewControllerRepresentable {
typealias UIViewControllerType = ARQLViewController
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIViewController(context: Context) -> ARQLViewController {
let viewController = ARQLViewController()
return viewController
}
func updateUIViewController(_ uiViewController: ARQLViewController, context: Context) {
}
class Coordinator: NSObject {
var parent: ARQLView
init(_ parent: ARQLView) {
self.parent = parent
}
}
}
Then I simply call the view when the user presses the button.