Create your First AR App with RealityKit, QLPreviewController and Swift
To create your first RealityKit app with Swift, you will need to have updated to MacOS Catalina. Neither of these new frameworks will work on MacOS Mojave or any earlier version. You should also make sure you have the latest version of Xcode by checking the Mac App Store and checking that there are no updates for Xcode that you have not yet installed. Having the latest version of Xcode and MacOS is essential to making sure this app works correctly.
You will also need an iPad, as AR can’t be emulated by the iOS Simulator.
Step 1 :-
To create this project in Xcode, go to File > New > Project and select the Augmented Reality App template.
Step 2 :-
Set your app name and Language in select Swift, then Content Technology in select RealityKit.
Step 3 :-
The app needs access to the camera: Open the target configuration and add a Privacy - Camera Usage Description key to the Info.plist. Set a description why the camera is needed.
Step 4 :-
Here we have added two dummy animation files (file type : .usdz) of AR Object,
AR Quick Look for downloading animation files.
Step 5 :-
Then next we have added one viewcontroller, and set one button inside the viewcontroller.
Step 6 :-
And finally here is the code of the AR object showing viewcontroller, in this screen we have present
QLPreviewController for AR object animation showing.
Finally Result is Here ?:-
ViewController File Code :-
import UIKit
import RealityKit
import QuickLook
class ViewController: UIViewController {
//MARK: ViewController Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: Button Action
@IBActionfuncClick_PressHere(_ sender: UIButton) {
let previewController = QLPreviewController()
previewController.delegate = self
previewController.dataSource = self
self.present(previewController, animated: true, completion: nil)
}
}
//MARK: QLPreviewController Delegate & DataSource Methods
extension ViewController: QLPreviewControllerDelegate, QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
guard let path = Bundle.main.path(forResource: "toy_drummer", ofType: "usdz") else { fatalError("Couldn't find a model") }
let url = URL(fileURLWithPath: path)
return url as QLPreviewItem
}
}