# iOS SwiftUI example

Base on the schema on [how-does-it-work](https://posetracker.gitbook.io/posetracker-api/use-posetracker-on-real-time-camera-webcam/how-does-it-work "mention") You need to call our URL <https://app.posetracker.com/pose_tracker/tracking> through an iOS **webview.**

### TODO :

## 1. Create a new iOS app in xcode

## 2. Use this code

Copy/past this in your ContentView and modify your <mark style="color:red;">API\_KEY</mark> :

```
import SwiftUI
import WebKit

struct WebView: UIViewRepresentable {
    var url: URL
    @ObservedObject var viewModel: ViewModel

    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        
        let userContentController = WKUserContentController()
        userContentController.add(context.coordinator, name: "iosListener")
        webConfiguration.userContentController = userContentController

        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.load(URLRequest(url: url))
        return webView
    }

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

    func makeCoordinator() -> Coordinator {
        Coordinator(viewModel: viewModel)
    }

    class Coordinator: NSObject, WKScriptMessageHandler {
        var viewModel: ViewModel

        init(viewModel: ViewModel) {
            self.viewModel = viewModel
        }

        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            if let data = message.body as? String {
                DispatchQueue.main.async {
                    self.viewModel.updateData(with: data)
                }
            }
        }
    }
}

class ViewModel: ObservableObject {
    @Published var info: String = "Waiting for data..."

    func updateData(with data: String) {
        self.info = data
    }
}

struct ContentView: View {
    @StateObject var viewModel = ViewModel()

    var body: some View {
        VStack {
            Text(viewModel.info)
                .padding()
            WebView(url: URL(string: "https://app.posetracker.com/pose_tracker/tracking?token=API_KEY&exercise=squat&difficulty=easy&width=350&height=350&progression=true")!, viewModel: viewModel)
                .edgesIgnoringSafeArea(.all)
        }
    }
}
```

## 3. How does it works ?

First we have the webview :

```
WebView(url: URL(string: "https://app.posetracker.com/pose_tracker/tracking?token=
API_KEY&exercise=squat&difficulty=easy&width=350&height=350&progression=true")!, viewModel: viewModel)
                .edgesIgnoringSafeArea(.all)
```

REPLACE the token in url params ="<https://...?token=API_KEY>" with your own API\_KEY

## 4. Important, the webview is using the device webcam so you need to add configuration to the webview

```
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
```

## 5. 🟧 Important point : Data exchange between PoseTracker and your web app 🟧

PoseTracker page will send information with a iosListener.poseMessage so you can handle then this way:

```
let userContentController = WKUserContentController()
userContentController.add(context.coordinator, name: "iosListener")
webConfiguration.userContentController = userContentController

let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.load(URLRequest(url: url))
```

## 6. Then you can handle data received :

```
func makeCoordinator() -> Coordinator {
  Coordinator(viewModel: viewModel)
}

class Coordinator: NSObject, WKScriptMessageHandler {
    var viewModel: ViewModel

    init(viewModel: ViewModel) {
        self.viewModel = viewModel
    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if let data = message.body as? String {
            DispatchQueue.main.async {
                self.viewModel.updateData(with: data)
            }
        }
    }
}
    
    &&
    
class ViewModel: ObservableObject {
    @Published var info: String = "Waiting for data..."

    func updateData(with data: String) {
        self.info = data
    }
}
```

You can find all the informations returned by [PoseTracker here.](https://posetracker.gitbook.io/posetracker-api/use-posetracker-on-real-time-camera-webcam/tracking-endpoint-message-to-handle)

#### Result

{% embed url="<https://youtube.com/shorts/XtXge7IEttM>" %}

You can find source files here

This repo is kept as a reference implementation on GitHub.

We’ll clean and stabilize it over time.

It will remain available as sample code for integration.

{% embed url="<https://github.com/Movelytics/SwiftUIPoseTrackerDemo>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://posetracker.gitbook.io/posetracker-api/use-posetracker-on-real-time-camera-webcam/integration-tutorials/ios-swiftui-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
