Skip to content

Commit 7173f1f

Browse files
committed
add post
1 parent bd7118c commit 7173f1f

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

_posts/2024/05/2024-05-08-ios-wkwebview-webpage-communication-2-control-flow.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tags: [iOS, WKWebView, Javascript, if, switch, statement]
55
---
66
{% include JB/setup %}
77

8-
이전 글에서 `WKScriptMessageHandler` 프로토콜의 메소드 `func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)`를 구현하여 웹 페이지에서 iOS 앱으로 데이터를 전달하는 방법을 살펴보았습니다.
8+
이전 글에서 `WKScriptMessageHandler` 프로토콜의 메소드인 `func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)`를 구현하여 웹 페이지에서 iOS 앱으로 데이터를 전달하는 방법을 살펴보았습니다.
99

1010
이번 글에서는 `userContentController` 메소드에서 구현되는 if 문과 switch 문과 같은 흐름제어(Control Flow) 코드로 인해 `WKWebView`와 다른 도메인과의 강한 결합 관계가 형성되는 것을 살펴보려고 합니다.
1111

@@ -15,17 +15,19 @@ Swift는 if, switch 문과 같이 제어문을 제공합니다. 이는 프로그
1515

1616
제어한다는 의미는 어떤 조건에 따라 코드의 실행을 중단하거나, 코드의 실행을 계속할지 결정하는 것을 의미합니다. 여기에서 각 도메인에서 필요한 Action을 처리하고 전달해야한다면 `userContentController` 는 많은 도메인과 강한 결합 관계를 형성합니다.
1717

18-
다음 코드는 웹페이지에서 전달받은 message의 action을 구분하여 처리하는 코드입니다.
18+
다음 코드는 웹페이지에서 전달받은 message의 action을 구분하여 처리하는 코드입니다:
1919

2020
```swift
2121
// WKScriptMessageHandler 프로토콜 메서드 구현
2222
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
23+
// 메시지의 이름과 body 추출
2324
guard
2425
message.name == "actionHandler",
2526
let messageBody = message.body as? [String: Any],
2627
let action = messageBody["action"] as? String
2728
else { return }
2829

30+
// Action에 따라 처리하는 switch 문
2931
switch action {
3032
case "loading": loading(body: messageBody)
3133
case "openCard": openCard(body: messageBody)
@@ -34,18 +36,23 @@ func userContentController(_ userContentController: WKUserContentController, did
3436
default: break
3537
}
3638

39+
// loading Action을 처리하는 함수
3740
func loading(body: [String: Any]) {
38-
guard let value = body["show"] as? Bool else { return }
41+
guard
42+
let value = body["show"] as? Bool
43+
else { return }
3944

4045
switch value {
4146
case true: ShowLoading()
4247
case false: HideLoading()
4348
}
4449
}
50+
51+
// payment Action을 처리하는 함수
4552
func payment(body: [String: Any]) {
4653
guard
47-
let id = body["paymentId"] as? String
48-
let info = body["paymentInfo"] as? [String: String]
54+
let id = body["paymentId"] as? String
55+
let info = body["paymentInfo"] as? [String: String]
4956
else { return }
5057

5158
cardPayment(paymentId: id, paymentInfo: info)
@@ -61,18 +68,18 @@ func userContentController(_ userContentController: WKUserContentController, did
6168

6269
### Inject Action Handler
6370

64-
`Action`을 Key로 사용하고, `messageBody`를 받아 수행하는 `Closure`를 가지는 Dictonary을 만들 수 있을 수 있습니다.
71+
`Action`을 Key로 사용하고, `messageBody`를 받아 수행하는 `Closure`를 가지는 Dictionary를 만들 수 있습니다.
6572

6673
```swift
67-
struct WKScriptMessageActioHandler {
74+
struct WKScriptMessageActionHandler {
6875
let closure: (_ body: [String: Any], _ webView: WKWebView?) -> Void
6976
}
7077

7178
class ViewController: UIViewController, WKScriptMessageHandler {
72-
var actionHandlers: [String: ActioHandler] = [:]
79+
let actionHandlers: [String: WKScriptMessageActionHandler]
7380
var webView: WKWebView?
7481

75-
init(actionHandlers: [String : ActioHandler]) {
82+
init(actionHandlers: [String : WKScriptMessageActionHandler] = [:]) {
7683
self.actionHandlers = actionHandlers
7784

7885
super.init(nibName: nil, bundle: nil)
@@ -92,8 +99,8 @@ class ViewController: UIViewController, WKScriptMessageHandler {
9299
}
93100
```
94101

95-
`userContentController` 메소드에서 `actionHandlers` Dictionary에 등록되어 있는 `WKScriptMessageActioHandler`를 찾아 `closure`를 호출합니다. 이전과 같이 switch 문을 통해 action을 구분하여 처리하지 않기 때문에, 웹페이지에서 전달받은 action이 많아지더라도, 비즈니스 로직을 처리하는 코드가 없기 때문에 유지보수가 쉬워집니다.
102+
`userContentController` 메소드에서 `actionHandlers` Dictionary에 등록되어 있는 `WKScriptMessageActionHandler`를 찾아 `closure`를 호출합니다. 이전과 같이 switch 문을 통해 action을 구분하여 처리하지 않기 때문에, 웹페이지에서 전달받은 action이 많아지더라도, 비즈니스 로직을 처리하는 코드가 없기 때문에 유지보수가 쉬워집니다.
96103

97104
`WKScriptMessageActioHandler`는 Closure만 가지고 있어, 각 ActionHandler가 messageBody의 값을 검증하여 유효한 값인 경우에만 Closure를 호출할 수 있도록 코드를 좀 더 구조화할 수 있지 않을까요?
98105

99-
그 내용은 다음편에서 다루겠습니다.
106+
그 내용은 다음편에서 다루겠습니다.

0 commit comments

Comments
 (0)