Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Pass pre-built WKWebView instance to makeCustomWebView #41

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Pass pre-built WKWebView instance to makeCustomWebView
Instead of forcing applications to make an all-or-nothing decision on
whether or not they use the pre-built `WKWebView` provided by the
package or roll their own, this commit introduces a mechanism for
applications to _either_ change the pre-built configuration, or build a
fresh instance of their own.

For example, an application might want to configure the WebView without
needing to generate the process pool or re-implement the User Agent
generation:

```swift
TurboConfig.shared.makeCustomWebView = { (configuration: WKWebViewConfiguration) in
    configuration.preferences.javaScriptCanOpenWindowsAutomatically = true

    let webView = WKWebView(frame: .zero, configuration: configuration)

    if #available(iOS 16.4, *) {
        webView.isInspectable = true
    }

    return webView
}
```
  • Loading branch information
seanpdoyle committed Sep 1, 2023
commit bcef7e2dc550468f1033443116df91f7488fb158
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,14 @@ TurboConfig.shared.userAgent = "Custom (Turbo Native)"

### Customize the web view and web view configuration

A block is used because a new instance is needed for each web view.

Don't forget to set user agent and use a shared process pool on the configuration.
A closure is used because a new instance is needed for each web view. The closure has a `WKWebViewConfiguration` argument that's pre-built and ready to be customized and assigned to a new web view.

```swift
TurboConfig.shared.makeCustomWebView = {
let configuration = WKWebViewConfiguration()
// Customize configuration.

let webView = WKWebView(frame: .zero, configuration: configuration)
// Customize web view.
TurboConfig.shared.makeCustomWebView = { (configuration: WKWebViewConfiguration) in
// Customize the WKWebViewConfiguration instance
// ...

return webView
return WKWebView(frame: .zero, configuration: configuration)
}
```

Expand Down
8 changes: 5 additions & 3 deletions Sources/TurboConfig.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WebKit

public class TurboConfig {
public typealias WebViewBlock = () -> WKWebView
public typealias WebViewBlock = (_ configuration: WKWebViewConfiguration) -> WKWebView

public static let shared = TurboConfig()

Expand All @@ -11,12 +11,14 @@ public class TurboConfig {

/// Optionally customize the web views used by each Turbo Session.
/// Ensure you return a new instance each time.
public var makeCustomWebView: WebViewBlock?
public var makeCustomWebView: WebViewBlock = { (configuration: WKWebViewConfiguration) in
WKWebView(frame: .zero, configuration: configuration)
}

// MARK: - Internal

func makeWebView() -> WKWebView {
makeCustomWebView?() ?? WKWebView(frame: .zero, configuration: makeWebViewConfiguration())
makeCustomWebView(makeWebViewConfiguration())
}

// MARK: - Private
Expand Down