我正在构建一个应用程序,其中一个页面有一个网页视图。当前,该页面具有一个可表示的and视图和一个视图。使用UIViewControllerRepresentable将Webview带到视图中。首先,我想知道当我点击登录按钮时,如何在LoginWebview中调用函数,反之亦然,如何从LoginWebview调用LoginView中的函数。我现在已经设置好了,所以当我点击登录时,它会切换导致updateUIView触发的状态,但是如何在其中调用一个定制的函数呢?反之亦然
为上面的长篇描述道歉,如果这是个愚蠢的问题
谢谢:)
LoginView:
import SwiftUI
import WebKit
struct LoginView: View {
@State public var email: String = ""
@State public var password: String = ""
let verticalPaddingForForm = 40
@State private var willMoveToNextScreen = true
@Binding var isLoggedIn: Bool
@State var showJSAlert = false
var body: some View {
LoginWebview(testdo: self.showJSAlert)
ZStack {
Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0)
VStack(alignment: .leading, spacing: 0) {
Image("logo")
.resizable()
.scaledToFit()
.padding(.top, 150)
.padding()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.ignoresSafeArea(.all)
VStack(spacing: CGFloat(verticalPaddingForForm)) {
VStack {
TextField("Email", text: $email)
.padding(.horizontal, 15).padding(.top, 50)
Divider()
.padding(.horizontal, 15)
SecureField("Password", text: $password)
.padding(.horizontal, 15).padding(.top, 20)
Divider()
.padding(.horizontal, 15)
}
.background(Color(.white))
Button(action: {
//Calls login webview and triggers update that calls the js
self.showJSAlert.toggle()
}) {
Text("Login")
.padding()
.font(.system(size: 20))
}
.background(Color.black)
.foregroundColor(Color.white)
.cornerRadius(10)
.padding(.top, 0)
.padding(.bottom, 20)
}
.padding(.horizontal, CGFloat(verticalPaddingForForm))
.background(Color(.white))
VStack{
Spacer()
Button(action: {
}) {
Text("Register")
.padding()
.font(.system(size: 40))
}
.background(Color(red: 20/225.0 ,green: 22/225.0 , blue: 25/225.0))
.foregroundColor(Color.white)
.cornerRadius(10)
.padding()
}
}.ignoresSafeArea()
};LoginWebview:
import SwiftUI
import WebKit
struct LoginWebview: UIViewRepresentable {
var testdo = false
func makeUIView(context: UIViewRepresentableContext<LoginWebview>) -> WKWebView {
let preferences = WKPreferences()
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let userContentController = WKUserContentController()
userContentController.add(context.coordinator, name:"observer")
configuration.userContentController = userContentController
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.navigationDelegate = context.coordinator
let url = URL(string: "https://www.google.co.uk")!
webView.load(URLRequest(url: url))
return webView
}
func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<LoginWebview>) {
print(testdo)
print("Controller")
uiView.evaluateJavaScript("sendComment();")
print(UserDefaults.standard.string(forKey: "Token") ?? "")
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func printstuff() {
print("printstuff")
}
typealias UIViewType = WKWebView
}
class Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
var control: LoginWebview
init(_ control: LoginWebview) {
self.control = control
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
showAlert(body: message.body)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
func sendjs(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
func showAlert(body: Any) {
print("working")
}
}发布于 2021-06-14 17:44:29
可以使用计算属性和闭包进行回调。
下面是示例代码。
struct LoginView: View {
// Other code
// Webview var
private var webView: LoginWebview {
LoginWebview(testdo: self.showJSAlert) {
// Here you will get the call back
print("Callback")
}
}
var body: some View {
webView // <-- Here
// Other Code和按钮动作
Button(action: {
//Calls login webview and triggers update that calls the js
self.showJSAlert.toggle()
// Access your function
self.webView.printstuff()
}) {
Text("Login")
.padding()
.font(.system(size: 20))
}在LoginWebview中
struct LoginWebview: UIViewRepresentable {
var testdo = false
var callBack: (() -> Void)? = nilclass Coordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler {
// Other code
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.control.callBack?() // < Example for calling callback
}
// Other code
}https://stackoverflow.com/questions/67974721
复制相似问题