Last active
June 29, 2022 13:34
-
-
Save akardas16/28812f441c903539d9102b1cc41c263c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// WebViewExample.swift | |
// CombineApp | |
// | |
// Created by Abdullah Kardas on 29.06.2022. | |
// | |
import SwiftUI | |
import UIKit | |
import WebKit | |
struct WebViewExample: View { | |
@ObservedObject var webViewModel = WebViewModel(url: "https://fit-meapp.web.app/") | |
var body: some View { | |
NavigationView { | |
VStack { | |
ScrollView { | |
Text("This is is a text").font(.title) | |
WebViewContainer(webViewModel: webViewModel).frame(height: webViewModel.containerHeight) | |
Button { | |
} label: { | |
Text("Custom Button").foregroundColor(.white).padding(.vertical,16).frame(width: 200).background(Color.red.cornerRadius(12)) | |
} | |
Image(systemName: "heart").font(.largeTitle).foregroundColor(.blue) | |
}.listStyle(.plain) | |
if webViewModel.isLoading { | |
ProgressView() | |
.frame(height: 30) | |
} | |
} | |
.navigationBarHidden(true) | |
} | |
} | |
} | |
struct WebViewExample_Previews: PreviewProvider { | |
static var previews: some View { | |
WebViewExample() | |
} | |
} | |
class WebViewModel: ObservableObject { | |
@Published var isLoading: Bool = false | |
@Published var canGoBack: Bool = true | |
@Published var shouldGoBack: Bool = false | |
@Published var title: String = "" | |
@Published var containerHeight:CGFloat = 100 | |
var url: String | |
init(url: String) { | |
self.url = url | |
} | |
} | |
struct WebViewContainer: UIViewRepresentable { | |
@ObservedObject var webViewModel: WebViewModel | |
func makeCoordinator() -> WebViewContainer.Coordinator { | |
Coordinator(self, webViewModel) | |
} | |
func makeUIView(context: Context) -> WKWebView { | |
guard let url = URL(string: self.webViewModel.url) else { | |
return WKWebView() | |
} | |
let request = URLRequest(url: url) | |
let webView = WKWebView() | |
webView.navigationDelegate = context.coordinator | |
webView.load(request) | |
webView.scrollView.isScrollEnabled = false | |
return webView | |
} | |
func updateUIView(_ uiView: WKWebView, context: Context) { | |
if webViewModel.shouldGoBack { | |
uiView.goBack() | |
webViewModel.shouldGoBack = false | |
} | |
} | |
} | |
extension WebViewContainer { | |
class Coordinator: NSObject, WKNavigationDelegate { | |
@ObservedObject private var webViewModel: WebViewModel | |
private let parent: WebViewContainer | |
init(_ parent: WebViewContainer, _ webViewModel: WebViewModel) { | |
self.parent = parent | |
self.webViewModel = webViewModel | |
} | |
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { | |
webViewModel.isLoading = true | |
} | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | |
webViewModel.isLoading = false | |
webViewModel.title = webView.title ?? "" | |
webViewModel.canGoBack = webView.canGoBack | |
print("*****\(webView.scrollView.contentSize.height)") | |
// height = webView.scrollView.contentSize.height | |
webViewModel.containerHeight = webView.scrollView.contentSize.height | |
} | |
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { | |
webViewModel.isLoading = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment