ios How to Change url on web查看swiftUI WKWebView

mbyulnm0  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(94)

我真的是新的iOS的游戏等等。所以我真的不知道我在做什么。但我有一个网页视图加载一些网页,我需要改变的网址时,willResignActiveNotification和willEnterForegroundNotification,然后关闭应用程序
我需要调用另一个html来使门户网站断开连接
这是我的代码

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()
    }
}

我真的不知道该怎么做

eqqqjvef

eqqqjvef1#

你只需要设置urlString状态变量:

onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
    urlString = "https://new_url.com"                
}

但是正如Paulw11在评论中提到的,你永远不应该调用exit...它也可以是应用程序评论中的拒绝原因。你可以重新考虑你的用户体验,让我们说导航用户到一个新的屏幕,以显示他们的“注销”状态。

相关问题