Don’t forget to share it with your network!
KrunaL Chunibhai Parvadiya
CEO, Softices
Augmented Reality
06 October, 2022
KrunaL Chunibhai Parvadiya
CEO, Softices
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.
To create this project in Xcode, go to File > New > Project and select the Augmented Reality App template.
Set your app name and Language in select Swift, then Content Technology in select RealityKit.
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.
Here we have added two dummy animation files (file type: .usdz) of AR Object, AR Quick Look for downloading animation files.
Then next we have added one viewcontroller, and set one button inside the viewcontroller.
And finally here is the code of the AR object showing viewcontroller, in this screen we have present QLPreviewController for AR object animation showing.
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
}
}