- 웹뷰 띄우기
import SwiftUI
import WebKit
struct CustomWebView: UIViewRepresentable {
@Binding var isShowingModal: Bool
@Binding var isSuccess: Bool
let request: URLRequest
let contentController = WKUserContentController()
func makeUIView(context: Context) -> WKWebView {
contentController.add(context.coordinator, name: "tossAuthWebView")
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = context.coordinator
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
func makeCoordinator() -> Coordinator {
Coordinator($isShowingModal, $isSuccess)
}
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
@Binding var isShowingModal: Bool
@Binding var isSuccess: Bool
init(_ isShowingModal: Binding<Bool>, _ isSuccess: Binding<Bool>) {
_isShowingModal = isShowingModal
_isSuccess = isSuccess
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let body = message.body as? String else { return }
switch body {
case "TOSS_AUTH_POPUP_ONLOAD":
print("TOSS_AUTH_POPUP_ONLOAD")
case "TOSS_AUTH_SUCCESS":
print("TOSS_AUTH_SUCCESS")
isShowingModal = false
isSuccess = true
case "TOSS_AUTH_FAIL":
print("TOSS_AUTH_FAIL")
case "TOSS_AUTH_CLICK_NAVBAR_CLOSE":
print("TOSS_AUTH_CLICK_NAVBAR_CLOSE")
default:
break
}
}
}
}
- 리퀘스트 생성
func makeRequest() -> URLRequest {
let txID = "txID"
let authURLString = "https://auth.cert.toss.im/start?serviceType=SIGN_USER_AUTH"
guard let url = URL(string: authURLString) else { fatalError("Invalid URL") }
var request = URLRequest(url: url)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = "txId=\(txID)".data(using: .utf8)
return request
}
- 뷰에서 처리
.onChange(of: isSuccess) { newValue in
if newValue {
navigationCoordinator.path.append(.login)
}
}
.sheet(isPresented: $isShowTossAuth) {
CustomWebView(isShowingModal: $isShowTossAuth, isSuccess:$isSuccess, request: makeRequest())
}