|
| 1 | +import UIKit |
| 2 | +import WebKit |
| 3 | + |
| 4 | +/// Configuration for Inertia.js integration |
| 5 | +public struct InertiaConfig { |
| 6 | + public typealias WebViewBlock = (_ configuration: WKWebViewConfiguration) -> WKWebView |
| 7 | + |
| 8 | + /// Set a custom user agent application prefix for every WKWebView instance. |
| 9 | + /// |
| 10 | + /// The library will automatically append a substring to your prefix |
| 11 | + /// which includes: |
| 12 | + /// - "Inertia Native iOS;" |
| 13 | + /// - "bridge-components: [your bridge components];" |
| 14 | + /// |
| 15 | + /// WKWebView's default user agent string will also appear at the |
| 16 | + /// beginning of the user agent. |
| 17 | + public var applicationUserAgentPrefix: String? = nil |
| 18 | + |
| 19 | + /// When enabled, adds a `UIBarButtonItem` of type `.done` to the left |
| 20 | + /// navigation bar button item on screens presented modally. |
| 21 | + public var showDoneButtonOnModals = false |
| 22 | + |
| 23 | + /// Sets the back button display mode of `InertiaWebViewController`. |
| 24 | + public var backButtonDisplayMode = UINavigationItem.BackButtonDisplayMode.default |
| 25 | + |
| 26 | + /// Enable or disable debug logging for Inertia visits and bridge elements |
| 27 | + /// connecting, disconnecting, receiving/sending messages, and more. |
| 28 | + public var debugLoggingEnabled = false { |
| 29 | + didSet { |
| 30 | + HotwireLogger.debugLoggingEnabled = debugLoggingEnabled |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// Gets the user agent that the library builds to identify the app |
| 35 | + /// and its registered bridge components. |
| 36 | + /// |
| 37 | + /// The user agent includes: |
| 38 | + /// - Your (optional) custom `applicationUserAgentPrefix` |
| 39 | + /// - "Native iOS; Inertia Native iOS;" |
| 40 | + /// - "bridge-components: [your bridge components];" |
| 41 | + public var userAgent: String { |
| 42 | + get { |
| 43 | + return UserAgent.build( |
| 44 | + applicationPrefix: applicationUserAgentPrefix, |
| 45 | + componentTypes: Inertia.bridgeComponentTypes |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Optionally customize the web views used by each Inertia Session. |
| 51 | + /// Ensure you return a new instance each time. |
| 52 | + public var makeCustomWebView: WebViewBlock = { (configuration: WKWebViewConfiguration) in |
| 53 | + WKWebView.debugInspectable(configuration: configuration) |
| 54 | + } |
| 55 | + |
| 56 | + /// Set a custom JSON encoder when parsing bridge payloads. |
| 57 | + /// The custom encoder can be useful when you need to apply specific |
| 58 | + /// encoding strategies, like snake case vs. camel case |
| 59 | + public var jsonEncoder = JSONEncoder() |
| 60 | + |
| 61 | + /// Set a custom JSON decoder when parsing bridge payloads. |
| 62 | + /// The custom decoder can be useful when you need to apply specific |
| 63 | + /// decoding strategies, like snake case vs. camel case |
| 64 | + public var jsonDecoder = JSONDecoder() |
| 65 | + |
| 66 | + // MARK: - Internal |
| 67 | + |
| 68 | + public func makeWebView() -> WKWebView { |
| 69 | + let configuration = makeWebViewConfiguration() |
| 70 | + let webView = makeCustomWebView(configuration) |
| 71 | + |
| 72 | + if !Inertia.bridgeComponentTypes.isEmpty { |
| 73 | + Bridge.initialize(webView) |
| 74 | + } |
| 75 | + |
| 76 | + return webView |
| 77 | + } |
| 78 | + |
| 79 | + // MARK: - Private |
| 80 | + |
| 81 | + private let sharedProcessPool = WKProcessPool() |
| 82 | + |
| 83 | + // A method (not a property) because we need a new instance for each web view. |
| 84 | + private func makeWebViewConfiguration() -> WKWebViewConfiguration { |
| 85 | + let configuration = WKWebViewConfiguration() |
| 86 | + configuration.defaultWebpagePreferences?.preferredContentMode = .mobile |
| 87 | + configuration.applicationNameForUserAgent = userAgent |
| 88 | + configuration.processPool = sharedProcessPool |
| 89 | + |
| 90 | + // Inject the Inertia bridge script |
| 91 | + return Inertia.injectBridge(into: configuration) |
| 92 | + } |
| 93 | +} |
0 commit comments