Build an AR Image Tracking iOS Application

Build an AR Image Tracking iOS Application

In less than 10 minutes

Prerequisites: you should be familiar with Swift and XCode.

  1. Select Create New XCode project

Screen Shot 2021-03-07 at 6.08.51 PM.png

  1. Select the Augmented Reality App option

Screen Shot 2021-03-07 at 6.31.16 PM.png

  1. Name your Project

Screen Shot 2021-03-07 at 6.32.56 PM.png

  1. Add a video file to your project to be shown instead of the real card live photo

Screen Shot 2021-03-07 at 8.29.02 PM.png

Screen Shot 2021-03-07 at 8.35.36 PM.png

  1. Import SceneKit Catalog, then navigate to art.scnassets and create a new file .scn

Screen Shot 2021-03-07 at 10.08.04 PM.png

  1. Add the plane object for your video and 3D text

Screen Shot 2021-03-07 at 10.59.07 PM.png

Screen Shot 2021-03-07 at 10.05.05 PM.png

  1. Import UIKit SceneKit ARKit

  2. You need to add ARSCNViewDelegate protocol

  3. Add an image file, name it, put fit measurements.

  4. Then add this simple code

*

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the view's delegate
    sceneView.delegate = self

    // Show statistics such as fps and timing information
    sceneView.showsStatistics = true

    // Create a new scene
    let scene = SCNScene(named: "art.scnassets/card.scn")!

    // Set the scene to the view
    sceneView.scene = scene
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Create a session configuration
    let configuration = ARImageTrackingConfiguration()
    guard let arReferenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }
    configuration.trackingImages = arReferenceImages
    sceneView.session.run(configuration)

    // Run the view's session
    sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Pause the view's session
    sceneView.session.pause()
}

// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard anchor is ARImageAnchor else { return }

    guard let card = sceneView.scene.rootNode.childNode(withName: "card", recursively: false) else { return }
    card.removeFromParentNode()
    node.addChildNode(card)

    card.isHidden = false

    let videoURL = Bundle.main.url(forResource: "pirate", withExtension: "mp4")!
    let videoPlayer = AVPlayer(url: videoURL)

    let videoScene = SKScene(size: CGSize(width: 720.0, height: 1280.0))

    let videoNode = SKVideoNode(avPlayer: videoPlayer)
    videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
    videoNode.size = videoScene.size
    videoNode.yScale = -1
    videoNode.play()

    videoScene.addChild(videoNode)

    guard let video = card.childNode(withName: "video", recursively: true) else { return }
    video.geometry?.firstMaterial?.diffuse.contents = videoScene

}


func session(_ session: ARSession, didFailWithError error: Error) {
    // Present an error message to the user

}

func sessionWasInterrupted(_ session: ARSession) {
    // Inform the user that the session has been interrupted, for example, by presenting an overlay

}

func sessionInterruptionEnded(_ session: ARSession) {
    // Reset tracking and/or remove existing anchors if consistent tracking is required

}

*

Finally. Unfortunately, I couldn't run the project because my ios 12.4.8 . this version is lower than the fit one, thank you for following these steps.