LoginSignup
0
1

More than 1 year has passed since last update.

【SwiftUI】アプリ内でWebサイトを表示する方法

Posted at

はじめに

アプリ内で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>) {
        
    }
}

Sep-02-2022 18-32-54.gif

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1