Skip to content

Commit 4e2e11e

Browse files
committed
feat: custom headers
1 parent 5e1ecef commit 4e2e11e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

OSInAppBrowserLib/RouterAdapters/OSIABWebViewRouterAdapter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class OSIABWebViewRouterAdapter: NSObject, OSIABRouter {
77

88
/// Object that contains the value to format the visual presentation.
99
private let options: OSIABWebViewOptions
10+
/// Custom headers to be used by the WebView.
11+
private let customHeaders: [String: String]?
1012
/// Object that manages the browser's cache
1113
private let cacheManager: OSIABCacheManager
1214
/// Object that manages all the callbacks available for the WebView.
@@ -15,14 +17,17 @@ public class OSIABWebViewRouterAdapter: NSObject, OSIABRouter {
1517
/// Constructor method.
1618
/// - Parameters:
1719
/// - options: Object that contains the value to format the visual presentation.
20+
/// - customHeaders: Custom headers to be used by the WebView. `nil` is provided in case of no value.
1821
/// - cacheManager: Object that manages the browser's cache
1922
/// - callbackHandler: Object that manages all the callbacks available for the WebView.
2023
public init(
2124
_ options: OSIABWebViewOptions,
25+
_ customHeaders: [String: String]? = nil,
2226
cacheManager: OSIABCacheManager,
2327
callbackHandler: OSIABWebViewCallbackHandler
2428
) {
2529
self.options = options
30+
self.customHeaders = customHeaders
2631
self.cacheManager = cacheManager
2732
self.callbackHandler = callbackHandler
2833
}
@@ -36,6 +41,7 @@ public class OSIABWebViewRouterAdapter: NSObject, OSIABRouter {
3641

3742
let viewModel = OSIABWebViewModel(
3843
url: url,
44+
customHeaders: self.customHeaders,
3945
self.options.toConfigurationModel().toWebViewConfiguration(),
4046
self.options.allowOverScroll,
4147
self.options.customUserAgent,

OSInAppBrowserLib/WebView/OSIABWebViewModel.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class OSIABWebViewModel: NSObject, ObservableObject {
2020
/// Indicates if first load is already done. This is important in order to trigger the `browserPageLoad` event.
2121
private var firstLoadDone: Bool = false
2222

23+
/// Custom headers to be used by the WebView.
24+
private let customHeaders: [String: String]?
25+
2326
/// The current URL being displayed
2427
@Published private(set) var url: URL
2528
/// Indicates if the URL is being loaded into the screen.
@@ -46,6 +49,7 @@ class OSIABWebViewModel: NSObject, ObservableObject {
4649
/// - callbackHandler: Object that manages all the callbacks available for the WebView.
4750
init(
4851
url: URL,
52+
customHeaders: [String: String]? = nil,
4953
_ webViewConfiguration: WKWebViewConfiguration,
5054
_ scrollViewBounces: Bool = true,
5155
_ customUserAgent: String? = nil,
@@ -54,6 +58,7 @@ class OSIABWebViewModel: NSObject, ObservableObject {
5458
callbackHandler: OSIABWebViewCallbackHandler
5559
) {
5660
self.url = url
61+
self.customHeaders = customHeaders
5762
self.webView = .init(frame: .zero, configuration: webViewConfiguration)
5863
self.closeButtonText = uiModel.closeButtonText
5964
self.callbackHandler = callbackHandler
@@ -133,7 +138,13 @@ class OSIABWebViewModel: NSObject, ObservableObject {
133138

134139
/// Loads the URL within the WebView. Is the first operation to be performed when the view is displayed.
135140
func loadURL() {
136-
self.webView.load(.init(url: self.url))
141+
var request = URLRequest(url: self.url)
142+
if let headers = self.customHeaders {
143+
for (key, value) in headers {
144+
request.setValue(value, forHTTPHeaderField: key)
145+
}
146+
}
147+
self.webView.load(request)
137148
}
138149

139150
/// Signals the WebView to move forward. This is performed as a reaction to a button click.

0 commit comments

Comments
 (0)