PoseTracker API
  • PoseTracker integration
  • Webview/iframe tracking in app
    • â„šī¸How does it work ?
    • đŸ’ģTry it yourself
    • âš™ī¸Integration tutorials
      • 📱Expo React Native example
      • đŸ’ģVanilla HTML & JS example
      • 🆕iOS SwiftUI example
      • 🆕Android Kotlin example
      • 🆕Flutter example
      • 🔜More to come...
    • 👨‍đŸ’ģTracking Endpoint params
      • 🔐TOKEN (required)
      • đŸ”ĨBLAZEPOSE (optional)
      • 📱WIDTH (optinal)
      • 📱HEIGHT (optinal)
      • đŸĨ”DIFFICULTY (optinal)
      • đŸ’ģ[developer plan only] KEYPOINTS
      • đŸ’ģ[developer plan only] ANGLES
      • đŸ’ĒEXERCISE (optional)
        • Squat
        • Lunge
        • Plank
        • Push Up
        • Balance on left leg
        • Balance on right leg
        • Split
        • Needle
        • đŸĻ•More to come...
      • đŸ–Ĩī¸SKELETON (optinal)
    • â„šī¸Tracking Endpoint Message to handle
      • đŸŽĻExercise Placement
      • đŸ”ĸExercise Counter
      • đŸ”ĸ[developer plan] Exercise Progression
      • đŸ”ĸ[developer plan] Tracking keypoints positions
      • đŸ”ĸ[developer plan] Tracking angles during movements
      • đŸ”ĸ[developer plan] Real-time motion feedbacks and recommandations
  • Pixel tracking
    • How does it work ?
    • First steps
    • Integration tutorials
      • 📱React Native example with react-native-vision-camera
      • đŸ’ģVanilla HTML & JS example
      • 🔜More to come...
    • 👨‍đŸ’ģPixel Endpoint params
    • â„šī¸Pixel message to handle
      • đŸŽĻExercise Placement
      • đŸ”ĸExercise Counter
      • đŸ”ĸ[developer plan] Exercise Progression
      • đŸ”ĸ[developer plan] Tracking keypoints positions
      • đŸ”ĸ[developer plan] Tracking angles during movements
      • đŸ”ĸ[developer plan] Real-time motion feedbacks and recommandations
    • đŸ› ī¸Support
Powered by GitBook
On this page
  • TODO :
  • 1. Create a new iOS app in xcode
  • 2. Use this code
  • 3. How does it works ?
  • 4. Important, the webview is using the device webcam so you need to add configuration to the webview
  • 5. 🟧 Important point : Data exchange between PoseTracker and your web app 🟧
  • 6. Then you can handle data received :
  1. Webview/iframe tracking in app
  2. Integration tutorials

iOS SwiftUI example

How to use PoseTracker with SwiftUI ?

PreviousVanilla HTML & JS exampleNextAndroid Kotlin example

Last updated 9 months ago

Base on the schema on How does it work ? You need to call our URL 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 API_KEY :

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
    }
}

Result

You can find source files here

You can find all the informations returned by

âš™ī¸
🆕
https://app.posetracker.com/pose_tracker/tracking
PoseTracker here.
GitHub - Movelytics/SwiftUIPoseTrackerDemoGitHub
Logo