Skip to content

Commit

Permalink
feat: 웹 뷰 메세지 핸들링
Browse files Browse the repository at this point in the history
  • Loading branch information
enebin committed Aug 17, 2023
1 parent 29a710d commit 07940f8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public struct KeymeTestsView: View {
WithViewStore(store, observe: { $0 }) { viewStore in
ZStack {
KeymeWebView(url: viewStore.url)
.onCloseWebView {
print("close")
}
.onTestSubmitted { testResultId in
print(testResultId)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
Expand Down
67 changes: 62 additions & 5 deletions Projects/Features/Sources/KeymeTests/KeymeWeb/KeymeWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,33 @@
import SwiftUI
import WebKit

final class KeymeWebViewOption {
var onCloseWebView: () -> Void
var onTestSubmitted: (_ testResultId: Int) -> Void

init(
onCloseWebView: @escaping () -> Void = {},
onTestSubmitted: @escaping (_ testResultId: Int) -> Void = { _ in }
) {
self.onCloseWebView = onCloseWebView
self.onTestSubmitted = onTestSubmitted
}
}

public struct KeymeWebView: UIViewRepresentable {
private let option: KeymeWebViewOption
public let url: String

init(url: String) {
self.option = .init()
self.url = url
}

public func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView(frame: CGRect.zero, configuration: WKWebViewConfiguration())
let webView = WKWebView(frame: CGRect.zero, configuration: WKWebViewConfiguration())

// This is the important part
webView.configuration.userContentController.add(context.coordinator, name: "appInterface")
webView.backgroundColor = .init(white: 1, alpha: 0.3)

if let url = URL(string: url) {
Expand All @@ -28,18 +46,57 @@ public struct KeymeWebView: UIViewRepresentable {
}

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

}

public func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
Coordinator(parent: self, option: option)
}

public class Coordinator: NSObject {
public class Coordinator: NSObject, WKScriptMessageHandler {
let parent: KeymeWebView
let option: KeymeWebViewOption

init(parent: KeymeWebView) {
init(parent: KeymeWebView, option: KeymeWebViewOption) {
self.parent = parent
self.option = option
}

public func userContentController(
_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage
) {
if message.name == "appInterface",
let messageBody = message.body as? [String: Any],
let command = messageBody["command"] as? String {

switch command {
case "CLOSE_WEBVIEW":
option.onCloseWebView()

case "SEND_TEST_RESULT":
if let testResultId = messageBody["data"] as? Int {
option.onTestSubmitted(testResultId)
}

default:
print("Unknown command: \(command)")
}

print("Received message from the web: \(message.body)")
}
}
}
}

public extension KeymeWebView {
func onCloseWebView(_ handler: @escaping () -> Void) -> Self {
self.option.onCloseWebView = handler
return self
}

func onTestSubmitted(_ handler: @escaping (_ testResultId: Int) -> Void) -> Self {
self.option.onTestSubmitted = handler
return self
}
}

0 comments on commit 07940f8

Please sign in to comment.