import SwiftUI import XuqmWebViewSDK #if canImport(UIKit) import UIKit #endif struct WebViewDemoView: View { @Binding var path: NavigationPath private enum WebViewMode: String, CaseIterable, Identifiable { case embedded = "嵌入式" case page = "页面模式" var id: String { rawValue } } @State private var url = "https://example.com" @State private var title = "示例网页" @State private var mode: WebViewMode = .embedded @State private var statusVersion = 0 private var config: XWebViewConfig { XWebViewConfig( url: url.trimmingCharacters(in: .whitespacesAndNewlines), title: title.trimmingCharacters(in: .whitespacesAndNewlines) ) } private var currentUrl: String { let controllerUrl = XWebViewBridge.shared.currentController()?.currentUrl() return (controllerUrl?.isEmpty == false ? controllerUrl : nil) ?? config.url } var body: some View { ScrollView { VStack(alignment: .leading, spacing: 16) { Text("XWebView") .font(.title2) .fontWeight(.bold) Text("可输入 URL,并在嵌入式组件和独立页面之间切换。") .foregroundStyle(.secondary) VStack(alignment: .leading, spacing: 12) { TextField("URL", text: $url) TextField("标题", text: $title) Picker("模式", selection: $mode) { ForEach(WebViewMode.allCases) { item in Text(item.rawValue).tag(item) } } .pickerStyle(.segmented) } VStack(alignment: .leading, spacing: 12) { Text("嵌入式组件") .font(.headline) if mode == .embedded { XWebViewView(config: config) .frame(height: 320) .clipShape(RoundedRectangle(cornerRadius: 16)) } else { Text("当前处于页面模式,点击下面按钮会 push 到独立页面。") .foregroundStyle(.secondary) } } VStack(alignment: .leading, spacing: 12) { Text("页面控制") .font(.headline) Text("当前 URL: \(currentUrl.isEmpty ? "未加载" : currentUrl)") .font(.caption) .foregroundStyle(.secondary) HStack(spacing: 12) { Button { XWebViewBridge.shared.currentController()?.goBack() statusVersion += 1 } label: { Text("后退") .frame(maxWidth: .infinity) .padding(.vertical, 10) .background(Color.gray.opacity(0.12)) .clipShape(RoundedRectangle(cornerRadius: 10)) } .disabled(!(XWebViewBridge.shared.currentController()?.canGoBack() ?? false)) Button { XWebViewBridge.shared.currentController()?.goForward() statusVersion += 1 } label: { Text("前进") .frame(maxWidth: .infinity) .padding(.vertical, 10) .background(Color.gray.opacity(0.12)) .clipShape(RoundedRectangle(cornerRadius: 10)) } .disabled(!(XWebViewBridge.shared.currentController()?.canGoForward() ?? false)) } HStack(spacing: 12) { Button { XWebViewBridge.shared.currentController()?.reload() statusVersion += 1 } label: { Text("刷新") .frame(maxWidth: .infinity) .padding(.vertical, 10) .background(Color.gray.opacity(0.12)) .clipShape(RoundedRectangle(cornerRadius: 10)) } Button { #if canImport(UIKit) UIPasteboard.general.string = currentUrl #endif statusVersion += 1 } label: { Text("复制 URL") .frame(maxWidth: .infinity) .padding(.vertical, 10) .background(Color.gray.opacity(0.12)) .clipShape(RoundedRectangle(cornerRadius: 10)) } } Button { XWebViewBridge.shared.currentController()?.load(url: url.trimmingCharacters(in: .whitespacesAndNewlines)) statusVersion += 1 } label: { Text("重新加载输入 URL") .frame(maxWidth: .infinity) .padding() .background(Color.accentColor) .foregroundStyle(.white) .clipShape(RoundedRectangle(cornerRadius: 12)) } } Button { openXWebView(config) path.append(AppRoute.webView) } label: { Text("打开页面模式") .frame(maxWidth: .infinity) .padding() .background(Color.accentColor) .foregroundStyle(.white) .clipShape(RoundedRectangle(cornerRadius: 12)) } } .padding() } .navigationTitle("网页") } }