ARKit & RealityKit with SwiftUI : AR with iOS (Part-II)

Shiru99
3 min readApr 29, 2023

--

Augmented Reality (AR) is a technology that blends virtual objects and information with the real-world environment, enhancing the user experience. With the launch of ARKit and RealityKit, Apple has made it easier for iOS developers to create AR apps that can transform how we interact with the world. SwiftUI, Apple’s modern declarative UI framework, can be used to build the user interface for AR apps, making it easier to create engaging AR experiences. In this article, we will explore how to use SwiftUI, ARKit, and RealityKit to create AR apps for iOS devices.

Initial Setup

  1. Open Xcode: Launch Xcode from the Applications folder or by searching for it in Spotlight.
  2. Create a new project: Click on “Create a new Xcode project” on the welcome screen or go to “File” > “New” > “Project” from the menu bar.
  3. Choose a template: Choose the template that best matches your project needs, such as “iOS App” > “Augmented Reality App” > Next
  4. Configure the project: Provide a name for your project, choose a team, and select a location to save the project.
Initial Code Template

Default Code Explanation

import SwiftUI
import RealityKit

struct ContentView : View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}

struct ARViewContainer: UIViewRepresentable {

func makeUIView(context: Context) -> ARView {

let arView = ARView(frame: .zero)

// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBox()

// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)

return arView

}

func updateUIView(_ uiView: ARView, context: Context) {}

}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif

ARViewContainer is a UIViewRepresentable struct that is used to create and manage an ARView instance. UIViewRepresentable is a protocol that allows SwiftUI to interoperate with UIKit views and controls. ARViewContainer acts as a bridge between the SwiftUI framework and the RealityKit framework, allowing you to create 3D AR experiences in your SwiftUI app.

makeUIView is a required method of the UIViewRepresentable protocol. It's called when the SwiftUI view is being created for the first time. In this method, you create and configure your ARView instance, load any 3D models or assets you need, and add them to the scene. Finally, you return the ARView instance you created.

updateUIView is another required method of the UIViewRepresentable protocol. It's called whenever the SwiftUI view needs to be updated based on changes to its state or environment. In this method, you update the ARView instance to reflect any changes in the state of your SwiftUI view.

More about updateUIView

struct ARViewContainer: UIViewRepresentable {
@Binding var isModelDisplayed: Bool

func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
return arView
}

func updateUIView(_ uiView: ARView, context: Context) {
if isModelDisplayed {
// Load and add the 3D model to the AR scene
let boxAnchor = try! Experience.loadBox()

// Add the box anchor to the scene
uiView.scene.anchors.append(boxAnchor)
} else {
// Remove the 3D model from the AR scene
uiView.scene.anchors.removeAll()
}
}
}

In this example, updateUIView is used to add or remove a 3D model from the AR scene based on the value of the isModelDisplayed Boolean state variable. If the value of isModelDisplayed is true, the method loads and adds a 3D box model to the AR scene. Otherwise, it removes all anchors (including the 3D model) from the AR scene.

This method is called whenever the value of the isModelDisplayed state variable is changed, and it allows the ARView instance to be updated accordingly. However, if you don’t do anything in the updateUIView method to respond to changes in that state variable, the ARViewContainer view won't be affected.

--

--

No responses yet