はじめに
アプリ内でWebページを見せる
環境
Xcode 13.3.1
Code
MainView
struct MainView: View {
var body: some View {
NavigationView{
VStack(spacing: 30){
NavigationLink(destination:
WebView(urlToLoad: "https://www.google.com")) {
Text("Google")
.foregroundColor(Color.white)
.padding()
.background(Color.blue)
.cornerRadius(20)
}
NavigationLink(destination:
WebView(urlToLoad: "https://qiita.com/")){
Text("Qiita")
.foregroundColor(Color.white)
.padding()
.background(Color.green)
.cornerRadius(20)
}
}
}
}
}
WebView
import WebKit
struct WebView: UIViewRepresentable {
var urlToLoad : String
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: self.urlToLoad) else {
return WKWebView()
}
let webview = WKWebView()
webview.load(URLRequest(url: url))
return webview
}
func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WebView>) {
}
}