NCWebBrowserView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // NCWebBrowserView.swift
  3. // Nextcloud
  4. //
  5. // Created by Aditya Tyagi on 04/03/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Aditya Tyagi <adityagi02@yahoo.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import SwiftUI
  24. @preconcurrency import WebKit
  25. /// Returns a WebView preferably for Sheets in SwiftUI, using a UIViewRepresentable struct with WebKit library
  26. ///
  27. /// - Parameters:
  28. /// - isPresented: A Bool value which initiates the webView in the parentView
  29. /// - urlBase: A URL value to which our view will open initially
  30. /// - browserTitle: A String value to show as the title of the webView
  31. struct NCBrowserWebView: View {
  32. var urlBase: URL
  33. var browserTitle: String
  34. @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
  35. var body: some View {
  36. VStack(spacing: 0) {
  37. HStack {
  38. HStack(alignment: .center) {
  39. Text(browserTitle)
  40. .font(.title3)
  41. .foregroundColor(Color(NCBrandColor.shared.textColor))
  42. .padding(.leading, 8)
  43. }
  44. .padding()
  45. Spacer()
  46. Button(action: {
  47. presentationMode.wrappedValue.dismiss()
  48. }) {
  49. ZStack {
  50. Image(systemName: "xmark")
  51. .renderingMode(.template)
  52. .resizable()
  53. .scaledToFit()
  54. .font(Font.system(.body).weight(.light))
  55. .frame(width: 14, height: 14)
  56. .foregroundColor(Color(NCBrandColor.shared.iconImageColor))
  57. }
  58. }
  59. .padding()
  60. }
  61. Divider()
  62. WebView(url: urlBase)
  63. }
  64. .navigationBarTitle(Text(""), displayMode: .inline) // Empty title to hide default navigation bar title
  65. }
  66. }
  67. struct WebView: UIViewRepresentable {
  68. let url: URL
  69. func makeUIView(context: Context) -> WKWebView {
  70. let webView = WKWebView()
  71. webView.navigationDelegate = context.coordinator
  72. return webView
  73. }
  74. func updateUIView(_ webView: WKWebView, context: Context) {
  75. let request = URLRequest(url: url)
  76. webView.load(request)
  77. }
  78. func makeCoordinator() -> Coordinator {
  79. Coordinator(self)
  80. }
  81. class Coordinator: NSObject, WKNavigationDelegate {
  82. var parent: WebView
  83. init(_ parent: WebView) {
  84. self.parent = parent
  85. }
  86. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  87. if let serverTrust = challenge.protectionSpace.serverTrust {
  88. completionHandler(.useCredential, URLCredential(trust: serverTrust))
  89. } else {
  90. completionHandler(.useCredential, nil)
  91. }
  92. }
  93. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  94. decisionHandler(.allow)
  95. }
  96. }
  97. }