diff --git a/Source/Bridge/Bridge.swift b/Source/Bridge/Bridge.swift index b454c43..2f0798f 100644 --- a/Source/Bridge/Bridge.swift +++ b/Source/Bridge/Bridge.swift @@ -25,6 +25,7 @@ public final class Bridge: Bridgable { public static func initialize(_ webView: WKWebView) { if getBridgeFor(webView) == nil { initialize(Bridge(webView: webView)) + webView.customUserAgent = Hotwire.config.userAgent } } diff --git a/Source/Hotwire.swift b/Source/Hotwire.swift index 432109e..496ecfe 100644 --- a/Source/Hotwire.swift +++ b/Source/Hotwire.swift @@ -17,6 +17,8 @@ public enum Hotwire { Bridge.initialize(webView) return webView } + + NotificationCenter.default.postDidRegisterBridgeComponents() } static var bridgeComponentTypes = [BridgeComponent.Type]() diff --git a/Source/HotwireNotifications.swift b/Source/HotwireNotifications.swift new file mode 100644 index 0000000..144ac16 --- /dev/null +++ b/Source/HotwireNotifications.swift @@ -0,0 +1,31 @@ +import Foundation +import UIKit + +extension NSNotification.Name { + static let didRegisterBridgeComponents = NSNotification.Name("HotwireNativeDidRegisterBridgeComponents") +} + +extension NotificationCenter { + func removeObservation(_ observationToken: NSObjectProtocol?) { + guard let observationToken else { return } + removeObserver(observationToken) + } + + func removeObservations(_ observationTokens: [NSObjectProtocol?]) { + for observationToken in observationTokens.compactMap({ $0 }) { + removeObserver(observationToken) + } + } +} + +extension NotificationCenter { + func postDidRegisterBridgeComponents() { + post(name: .didRegisterBridgeComponents, object: nil) + } + + func observeDidRegisterBridgeComponents(reaction: @escaping () -> Void) -> NSObjectProtocol { + addObserver(forName: .didRegisterBridgeComponents, object: nil, queue: .main) { _ in + reaction() + } + } +} diff --git a/Source/Turbo/Navigator/Navigator.swift b/Source/Turbo/Navigator/Navigator.swift index 3a5e635..8b669aa 100644 --- a/Source/Turbo/Navigator/Navigator.swift +++ b/Source/Turbo/Navigator/Navigator.swift @@ -135,9 +135,18 @@ public class Navigator { self.session.delegate = self self.modalSession.delegate = self - self.webkitUIDelegate = WKUIController(delegate: self) + webkitUIDelegate = WKUIController(delegate: self) session.webView.uiDelegate = webkitUIDelegate modalSession.webView.uiDelegate = webkitUIDelegate + + didRegisterBridgeComponentsObservationToken = NotificationCenter.default.observeDidRegisterBridgeComponents { + Bridge.initialize(session.webView) + Bridge.initialize(modalSession.webView) + } + } + + deinit { + NotificationCenter.default.removeObservation(didRegisterBridgeComponentsObservationToken) } // MARK: Private @@ -146,6 +155,7 @@ public class Navigator { private let navigatorDelegate = DefaultNavigatorDelegate() private var backgroundTerminatedWebViewSessions = [Session]() private var appInBackground = false + private var didRegisterBridgeComponentsObservationToken: NSObjectProtocol? private func controller(for proposal: VisitProposal) -> UIViewController? { switch delegate.handle(proposal: proposal) { @@ -223,7 +233,7 @@ extension Navigator: NavigationHierarchyControllerDelegate { case .modal: modalSession.visit(controller, options: options) } } - + func refreshVisitable(navigationStack: NavigationHierarchyController.NavigationStackType, newTopmostVisitable: any Visitable) { switch navigationStack { case .main: