我对 iOS swiftui 等还很陌生,所以我真的不知道自己做得怎么样。 但我有一个加载一些网页的网页视图,我需要在 willResignActiveNotification 和 willEnterForegroundNotification 时更改 url,然后关闭应用程序
我需要调用另一个 html 来断开 Web 门户
这是我的代码
import SwiftUI
import WebKit
struct ContentView: View {
@State private var urlString: String = "some url"
var body: some View {
VStack (spacing: 40){
WebView(url: URL(string: urlString)!)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
exit(0);
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
exit(0);
}
}
Spacer()
}
}
struct WebView: UIViewRepresentable{
var url: URL
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context){
let request = URLRequest(url: url)
uiView.load(request)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我真的不知道该怎么办
您只需设置 urlString 状态变量:
onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
urlString = "https://new_url.com"
}
但正如 Paulw11 在评论中提到的,你永远不应该调用 exit...它也可能成为应用程序审核中的拒绝原因。您可以重新考虑您的用户体验,假设将用户导航到新屏幕以显示他们的“注销”状态。