-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
64 lines (51 loc) · 2.01 KB
/
ContentView.swift
File metadata and controls
64 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
// Configuration du contrôleur de contenu pour gérer les messages de JavaScript
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=494e020b-7cc8-4aed-815b-4674d10d4f30&exercise=squat&difficulty=easy&width=350&height=350&progression=true")!, viewModel: viewModel)
.edgesIgnoringSafeArea(.all)
}
}
}