topWebview
webviewExample
composeWebview
description
basicList1
basicList1-1
basicList2
basicList2-1
basicList2-2
swift
// blp_sample_app_iosApp.swift
import SwiftUI
@main
struct blp_sample_app_iosApp: App {
// 앱 세로모드 잠금 처리
(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return .portrait
}
}
step1title
step1subtitle1
step1subtitle2
swift
// ContentView.swift
import SwiftUI
// 최상위 View
struct ContentView: View {
private var navigationModel = NavigationModel()
var body: some View {
NavigationStack(path: $navigationModel.path) {
HomeContentView(navigationModel: navigationModel)
.navigationDestination(for: NavigationDestination.self) { destination in
switch destination {
case .launcher:
// 런처 화면
LauncherContentView(navigationModel: navigationModel)
}
}
}
}
}
#Preview {
ContentView()
}
step1subtitle3
swift
// NavigationModel.swift
import Foundation
// 화면 전환 case들 (ContentView)
enum NavigationDestination: String, Identifiable {
case launcher
var id: String { rawValue }
}
class NavigationModel: ObservableObject {
var path: [NavigationDestination] = []
}
step2title
step2subtitle1
step2subtitle1-1
step2subtitle1-2
swift
// LauncherWebView.swift
import WebKit
import SwiftUI
// UIViewRepresentable를 사용하여 WKWebView를 wrapping해서 사용
struct LauncherWebView: UIViewRepresentable {
let url: URL
// 뒤로가기 처리
let onCloseLauncher: () -> Void
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
var parent: LauncherWebView
init(parent: LauncherWebView) {
self.parent = parent
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// WebView finished loading
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
// 런처 뒤로가기 이벤트 수신 시 처리
if message.name == "closeLauncher" {
parent.onCloseLauncher()
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
let contentController = webView.configuration.userContentController
// javascript에서 전달하는 이벤트 등록
contentController.add(context.coordinator, name: "closeLauncher")
webView.navigationDelegate = context.coordinator
let request = URLRequest(url: url)
webView.load(request)
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {
//
}
static func dismantleUIView(_ uiView: WKWebView, coordinator: Self.Coordinator) {
uiView.configuration.userContentController.removeScriptMessageHandler(forName: "closeLauncher")
}
}
step3title
step3subtitle1
step3subtitle1-1
step3subtitle1-2
step3subtitle1-2-1
step3subtitle1-2-2
step3subtitle1-3
swift
// LauncherContentView.swift
import SwiftUI
import WebKit
struct LauncherContentView: View {
// navigation model observer
var navigationModel: NavigationModel
var body: some View {
LauncherWebView(
url: URL(string: "런처 URL 정보"),
onCloseLauncher: {
// 런처 뒤로가기 처리
handleCloseLauncher()
},
)
.navigationBarBackButtonHidden(true)
}
// 뒤로가기 처리
private func handleCloseLauncher() {
navigationModel.path.removeLast()
}
}
#Preview {
LauncherContentView(navigationModel: NavigationModel())
}
footerTitle
"[Client Admin] footerMessage
v1.0.6