-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTailboneApp.swift
84 lines (72 loc) · 2.11 KB
/
TailboneApp.swift
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import SwiftUI
import WebKit
struct ContentView: View {
var webview = Webview.init(url: Bundle.main.url(
forResource: "index",
withExtension: "html"
)!)
var body: some View {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
webview
}
}
}
class ViewController: UIViewController, WKScriptMessageHandler {
func userContentController(
_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage
) {
// Do nothing. This is only here to give the JS something to detect
// so that it can skip the web version's start menu.
}
}
struct Webview: UIViewRepresentable {
let url: URL
func makeUIView(
context: UIViewRepresentableContext<Webview>
) -> WKWebView {
let config = WKWebViewConfiguration()
// enable audio playback to begin immediately
config.mediaTypesRequiringUserActionForPlayback = []
// ensure window.webkit.messageHandlers is present for index.html to
// detect the platform correctly and skip the web-only menu screen
let contentController = WKUserContentController()
contentController.add(
ViewController.init(),
name: "placeholder"
)
config.userContentController = contentController
let webview = WKWebView(
frame: .zero,
configuration: config
)
// prevent white flash during load
webview.isOpaque = false
webview.backgroundColor = UIColor.clear
let request = URLRequest(
url: self.url,
cachePolicy: .returnCacheDataElseLoad
)
webview.load(request)
return webview
}
func updateUIView(
_ webview: WKWebView,
context: UIViewRepresentableContext<Webview>
) {
let request = URLRequest(
url: self.url,
cachePolicy: .returnCacheDataElseLoad
)
webview.load(request)
}
}
@main
struct TailboneApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}